(modules): Add CSN_369103, CWI, DEC-MCS, ECMA-CYRILLIC, GOST_19768-74, GREEK-CCITT...
[glibc.git] / hurd / report-wait.c
blobe8f4f1af4a01b1d0205eb2854a36bbf907825384
1 /* Report on what a thread in our task is waiting for.
2 Copyright (C) 1996, 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <hurd.h>
21 #include <hurd/signal.h>
22 #include <hurd/fd.h>
23 #include <string.h>
24 #include <assert.h>
25 #include <hurd/msg_server.h>
26 #include "thread_state.h"
27 #include "intr-msg.h"
29 static void
30 describe_number (string_t description, const char *flavor, unsigned long int i)
32 unsigned long int j;
33 char *p = __stpcpy (description, flavor);
35 /* Allocate space for the number at the end of DESCRIPTION. */
36 for (j = i; j >= 10; j /= 10)
37 p++;
38 p[1] = '\0';
42 *p-- = '0' + i % 10;
43 i /= 10;
44 } while (i != 0);
45 assert (*p == '#');
48 static void
49 describe_port (string_t description, mach_port_t port)
51 int i;
53 if (port == __mach_task_self ())
55 strcpy (description, "task-self");
56 return;
59 for (i = 0; i < _hurd_nports; ++i)
60 if (port == _hurd_ports[i].port)
62 describe_number (description, "init#", i);
63 return;
66 if (_hurd_init_dtable)
68 for (i = 0; i < _hurd_init_dtablesize; ++i)
69 if (port == _hurd_init_dtable[i])
71 describe_number (description, "fd#", i);
72 return;
75 else if (_hurd_dtable)
77 for (i = 0; i < _hurd_dtablesize; ++i)
78 if (_hurd_dtable[i] == NULL)
79 continue;
80 else if (port == _hurd_dtable[i]->port.port)
82 describe_number (description, "fd#", i);
83 return;
85 else if (port == _hurd_dtable[i]->ctty.port)
87 describe_number (description, "bgfd#", i);
88 return;
92 describe_number (description, "port#", port);
96 /* We want _HURD_ITIMER_THREAD, but don't want to link in the itimer code
97 unnecessarily. */
98 #if 0 /* libc.so.0.0 needs this defined, so make it a weak alias for now. */
99 extern thread_t _hurd_itimer_thread; /* XXX */
100 weak_extern (_hurd_itimer_thread)
101 #else
102 static thread_t default_hurd_itimer_thread;
103 weak_alias (default_hurd_itimer_thread, _hurd_itimer_thread)
104 #endif
106 kern_return_t
107 _S_msg_report_wait (mach_port_t msgport, thread_t thread,
108 string_t description, int *msgid)
110 *msgid = 0;
112 if (thread == _hurd_msgport_thread)
113 /* Cute. */
114 strcpy (description, "msgport");
115 else if (&_hurd_msgport_thread && thread == _hurd_itimer_thread)
116 strcpy (description, "itimer");
117 else
119 /* Make sure this is really one of our threads. */
121 struct hurd_sigstate *ss;
123 __mutex_lock (&_hurd_siglock);
124 for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
125 if (ss->thread == thread)
126 break;
127 __mutex_unlock (&_hurd_siglock);
128 if (ss == NULL)
129 /* To hell with you. */
130 return EINVAL;
132 if (ss->suspended != MACH_PORT_NULL)
133 strcpy (description, "sigsuspend");
134 else
136 /* Examine the thread's state to see if it is blocked in an RPC. */
138 struct machine_thread_state state;
139 mach_msg_type_number_t count = MACHINE_THREAD_STATE_COUNT;
140 error_t err;
142 err = __thread_get_state (thread, MACHINE_THREAD_STATE_FLAVOR,
143 (integer_t *) &state, &count);
144 if (err)
145 return err;
146 assert (count == MACHINE_THREAD_STATE_COUNT);
147 if (SYSCALL_EXAMINE (&state, msgid))
149 /* Blocked in a system call. */
150 if (*msgid == -25)
151 /* mach_msg system call. Examine its parameters. */
152 describe_port (description, MSG_EXAMINE (&state, msgid));
153 else
154 strcpy (description, "kernel");
156 else
157 description[0] = '\0';
161 __mach_port_deallocate (__mach_task_self (), thread);
162 return 0;
165 kern_return_t
166 _S_msg_describe_ports (mach_port_t msgport, mach_port_t refport,
167 mach_port_t *ports, mach_msg_type_number_t nports,
168 char **desc, mach_msg_type_number_t *desclen)
170 char *p, *end;
172 if (__USEPORT (AUTH, msgport != port))
173 return EPERM;
175 end = *desc + *desclen;
176 p = *desc;
177 while (nports-- > 0)
179 char this[200];
180 describe_port (this, *ports++);
181 p = __stpncpy (p, this, end - p);
182 if (p == end && p[-1] != '\0')
183 return ENOMEM;
186 *desclen = p - *desc;
187 return 0;