491b566d04a04bee70be8306c73d8d3070781ae6
[platform/upstream/glibc.git] / hurd / report-wait.c
1 /* Report on what a thread in our task is waiting for.
2 Copyright (C) 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA.  */
19
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"
28
29 static void
30 describe_number (string_t description, const char *flavor, unsigned long int i)
31 {
32   unsigned long int j;
33   char *p = __stpcpy (description, flavor);
34
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';
39
40   do
41     {
42       *p-- = '0' + i % 10;
43       i /= 10;
44     } while (i != 0);
45   assert (*p == '#');
46 }
47
48 static void
49 describe_port (string_t description, mach_port_t port)
50 {
51   int i;
52
53   if (port == __mach_task_self ())
54     {
55       strcpy (description, "task-self");
56       return;
57     }
58
59   for (i = 0; i < _hurd_nports; ++i)
60     if (port == _hurd_ports[i].port)
61       {
62         describe_number (description, "init#", i);
63         return;
64       }
65
66   if (_hurd_init_dtable)
67     {
68       for (i = 0; i < _hurd_init_dtablesize; ++i)
69         if (port == _hurd_init_dtable[i])
70           {
71             describe_number (description, "fd#", i);
72             return;
73           }
74     }
75   else if (_hurd_dtable)
76     {
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)
81           {
82             describe_number (description, "fd#", i);
83             return;
84           }
85         else if (port == _hurd_dtable[i]->ctty.port)
86           {
87             describe_number (description, "bgfd#", i);
88             return;
89           }
90     }
91
92   describe_number (description, "port#", port);
93 }
94
95
96 /* Common defn so we don't link in the itimer code unnecessarily.  */
97 thread_t _hurd_itimer_thread; /* XXX */
98
99 kern_return_t
100 _S_msg_report_wait (mach_port_t msgport, thread_t thread,
101                     string_t description, int *msgid)
102 {
103   *msgid = 0;
104
105   if (thread == _hurd_msgport_thread)
106     /* Cute.  */
107     strcpy (description, "msgport");
108   else if (thread == _hurd_itimer_thread)
109     strcpy (description, "itimer");
110   else
111     {
112       /* Make sure this is really one of our threads.  */
113
114       struct hurd_sigstate *ss;
115
116       __mutex_lock (&_hurd_siglock);
117       for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
118         if (ss->thread == thread)
119           break;
120       __mutex_unlock (&_hurd_siglock);
121       if (ss == NULL)
122         /* To hell with you.  */
123         return EINVAL;
124
125       if (ss->suspended != MACH_PORT_NULL)
126         strcpy (description, "sigsuspend");
127       else
128         {
129           /* Examine the thread's state to see if it is blocked in an RPC.  */
130
131           struct machine_thread_state state;
132           mach_msg_type_number_t count = MACHINE_THREAD_STATE_COUNT;
133           error_t err;
134
135           err = __thread_get_state (thread, MACHINE_THREAD_STATE_FLAVOR,
136                                     (integer_t *) &state, &count);
137           if (err)
138             return err;
139           assert (count == MACHINE_THREAD_STATE_COUNT);
140           if (SYSCALL_EXAMINE (&state, msgid))
141             {
142               /* Blocked in a system call.  */
143               if (*msgid == -25)
144                 /* mach_msg system call.  Examine its parameters.  */
145                 describe_port (description, MSG_EXAMINE (&state, msgid));
146               else
147                 strcpy (description, "kernel");
148             }
149           else
150             description[0] = '\0';
151         }
152     }
153
154   __mach_port_deallocate (__mach_task_self (), thread);
155   return 0;
156 }
157
158 kern_return_t
159 _S_msg_describe_ports (mach_port_t msgport, mach_port_t refport,
160                        mach_port_t *ports, mach_msg_type_number_t nports,
161                        char **desc, mach_msg_type_number_t *desclen)
162 {
163   char *p, *end;
164
165   if (__USEPORT (AUTH, msgport != port))
166     return EPERM;
167
168   end = *desc + *desclen;
169   p = *desc;
170   while (nports-- > 0)
171     {
172       char this[200];
173       describe_port (this, *ports++);
174       p = __stpncpy (p, this, end - p);
175       if (p == end && p[-1] != '\0')
176         return ENOMEM;
177     }
178
179   *desclen = p - *desc;
180   return 0;
181 }