Hurd: hurdsock: reject negative domains
[platform/upstream/glibc.git] / hurd / intr-msg.c
1 /* Replacement for mach_msg used in interruptible Hurd RPCs.
2    Copyright (C) 1995,96,97,98,99,2000,2001,2002,2005
3         Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, see
18    <http://www.gnu.org/licenses/>.  */
19
20 #include <mach.h>
21 #include <mach/mig_errors.h>
22 #include <mach/mig_support.h>
23 #include <hurd/signal.h>
24 #include <assert.h>
25
26 #include "intr-msg.h"
27
28 #ifdef NDR_CHAR_ASCII           /* OSF Mach flavors have different names.  */
29 # define mig_reply_header_t     mig_reply_error_t
30 #endif
31
32 error_t
33 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
34                          mach_msg_option_t option,
35                          mach_msg_size_t send_size,
36                          mach_msg_size_t rcv_size,
37                          mach_port_t rcv_name,
38                          mach_msg_timeout_t timeout,
39                          mach_port_t notify)
40 {
41   error_t err;
42   struct hurd_sigstate *ss;
43   const mach_msg_option_t user_option = option;
44   const mach_msg_timeout_t user_timeout = timeout;
45
46   struct clobber
47   {
48 #ifdef NDR_CHAR_ASCII
49     NDR_record_t ndr;
50 #else
51     mach_msg_type_t type;
52 #endif
53     error_t err;
54   };
55   union msg
56   {
57     mach_msg_header_t header;
58     mig_reply_header_t reply;
59     struct
60     {
61       mach_msg_header_t header;
62 #ifdef NDR_CHAR_ASCII
63       NDR_record_t ndr;
64 #else
65       int type;
66 #endif
67       int code;
68     } check;
69     struct
70     {
71       mach_msg_header_t header;
72       struct clobber data;
73     } request;
74   };
75   union msg *const m = (void *) msg;
76   mach_msg_bits_t msgh_bits;
77   mach_port_t remote_port;
78   mach_msg_id_t msgid;
79   struct clobber save_data;
80
81   if ((option & (MACH_SEND_MSG|MACH_RCV_MSG)) != (MACH_SEND_MSG|MACH_RCV_MSG)
82       || _hurd_msgport_thread == MACH_PORT_NULL)
83     {
84       /* Either this is not an RPC (i.e., only a send or only a receive),
85          so it can't be interruptible; or, the signal thread is not set up
86          yet, so we cannot do the normal signal magic.  Do a normal,
87          uninterruptible mach_msg call instead.  */
88       return __mach_msg (&m->header, option, send_size, rcv_size, rcv_name,
89                          timeout, notify);
90     }
91
92   ss = _hurd_self_sigstate ();
93
94   /* Save state that gets clobbered by an EINTR reply message.
95      We will need to restore it if we want to retry the RPC.  */
96   msgh_bits = m->header.msgh_bits;
97   remote_port = m->header.msgh_remote_port;
98   msgid = m->header.msgh_id;
99   assert (rcv_size >= sizeof m->request);
100   save_data = m->request.data;
101
102   /* Tell the signal thread that we are doing an interruptible RPC on
103      this port.  If we get a signal and should return EINTR, the signal
104      thread will set this variable to MACH_PORT_NULL.  The RPC might
105      return EINTR when some other thread gets a signal, in which case we
106      want to restart our call.  */
107   ss->intr_port = m->header.msgh_remote_port;
108
109   /* A signal may arrive here, after intr_port is set, but before the
110      mach_msg system call.  The signal handler might do an interruptible
111      RPC, and clobber intr_port; then it would not be set properly when we
112      actually did send the RPC, and a later signal wouldn't interrupt that
113      RPC.  So, _hurd_setup_sighandler saves intr_port in the sigcontext,
114      and sigreturn restores it.  */
115
116  message:
117
118   /* XXX
119      At all points here (once SS->intr_port is set), the signal thread
120      thinks we are "about to enter the syscall", and might mutate our
121      return-value register.  This is bogus.
122    */
123
124   if (ss->cancel)
125     {
126       /* We have been cancelled.  Don't do an RPC at all.  */
127       ss->intr_port = MACH_PORT_NULL;
128       ss->cancel = 0;
129       return EINTR;
130     }
131
132   /* Note that the signal trampoline code might modify our OPTION!  */
133   err = INTR_MSG_TRAP (msg, option, send_size,
134                        rcv_size, rcv_name, timeout, notify);
135
136   switch (err)
137     {
138     case MACH_RCV_TIMED_OUT:
139       if (user_option & MACH_RCV_TIMEOUT)
140         /* The real user RPC timed out.  */
141         break;
142       else
143         /* The operation was supposedly interrupted, but still has
144            not returned.  Declare it interrupted.  */
145         goto interrupted;
146
147     case MACH_SEND_INTERRUPTED: /* RPC didn't get out.  */
148       if (!(option & MACH_SEND_MSG))
149         {
150           /* Oh yes, it did!  Since we were not doing a message send,
151              this return code cannot have come from the kernel!
152              Instead, it was the signal thread mutating our state to tell
153              us not to enter this RPC.  However, we are already in the receive!
154              Since the signal thread thought we weren't in the RPC yet,
155              it didn't do an interrupt_operation.
156              XXX */
157           goto retry_receive;
158         }
159       /* FALLTHROUGH */
160
161       /* These are the other codes that mean a pseudo-receive modified
162          the message buffer and we might need to clean up the port rights.  */
163     case MACH_SEND_TIMED_OUT:
164     case MACH_SEND_INVALID_NOTIFY:
165 #ifdef MACH_SEND_NO_NOTIFY
166     case MACH_SEND_NO_NOTIFY:
167 #endif
168 #ifdef MACH_SEND_NOTIFY_IN_PROGRESS
169     case MACH_SEND_NOTIFY_IN_PROGRESS:
170 #endif
171       if (MACH_MSGH_BITS_REMOTE (msg->msgh_bits) == MACH_MSG_TYPE_MOVE_SEND)
172         {
173           __mach_port_deallocate (__mach_task_self (), msg->msgh_remote_port);
174           msg->msgh_bits
175             = (MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
176                                MACH_MSGH_BITS_LOCAL (msg->msgh_bits))
177                | MACH_MSGH_BITS_OTHER (msg->msgh_bits));
178         }
179       if (msg->msgh_bits & MACH_MSGH_BITS_COMPLEX)
180         {
181 #ifndef MACH_MSG_PORT_DESCRIPTOR
182           /* Check for MOVE_SEND rights in the message.  These hold refs
183              that we need to release in case the message is in fact never
184              re-sent later.  Since it might in fact be re-sent, we turn
185              these into COPY_SEND's after deallocating the extra user ref;
186              the caller is responsible for still holding a ref to go with
187              the original COPY_SEND right, so the resend copies it again.  */
188
189           mach_msg_type_long_t *ty = (void *) (msg + 1);
190           while ((void *) ty < (void *) msg + msg->msgh_size)
191             {
192               mach_msg_type_name_t name;
193               mach_msg_type_size_t size;
194               mach_msg_type_number_t number;
195
196               inline void clean_ports (mach_port_t *ports, int dealloc)
197                 {
198                   mach_msg_type_number_t i;
199                   switch (name)
200                     {
201                     case MACH_MSG_TYPE_MOVE_SEND:
202                       for (i = 0; i < number; i++)
203                         __mach_port_deallocate (__mach_task_self (), *ports++);
204                       if (ty->msgtl_header.msgt_longform)
205                         ty->msgtl_name = MACH_MSG_TYPE_COPY_SEND;
206                       else
207                         ty->msgtl_header.msgt_name = MACH_MSG_TYPE_COPY_SEND;
208                       break;
209                     case MACH_MSG_TYPE_COPY_SEND:
210                     case MACH_MSG_TYPE_MOVE_RECEIVE:
211                       break;
212                     default:
213                       if (MACH_MSG_TYPE_PORT_ANY (name))
214                         assert (! "unexpected port type in interruptible RPC");
215                     }
216                   if (dealloc)
217                     __vm_deallocate (__mach_task_self (),
218                                      (vm_address_t) ports,
219                                      number * sizeof (mach_port_t));
220                 }
221
222               if (ty->msgtl_header.msgt_longform)
223                 {
224                   name = ty->msgtl_name;
225                   size = ty->msgtl_size;
226                   number = ty->msgtl_number;
227                   ty = (void *) ty + sizeof (mach_msg_type_long_t);
228                 }
229               else
230                 {
231                   name = ty->msgtl_header.msgt_name;
232                   size = ty->msgtl_header.msgt_size;
233                   number = ty->msgtl_header.msgt_number;
234                   ty = (void *) ty + sizeof (mach_msg_type_t);
235                 }
236
237               if (ty->msgtl_header.msgt_inline)
238                 {
239                   clean_ports ((void *) ty, 0);
240                   /* calculate length of data in bytes, rounding up */
241                   ty = (void *) ty + (((((number * size) + 7) >> 3)
242                                        + sizeof (mach_msg_type_t) - 1)
243                                       &~ (sizeof (mach_msg_type_t) - 1));
244                 }
245               else
246                 {
247                   clean_ports (*(void **) ty,
248                                ty->msgtl_header.msgt_deallocate);
249                   ty = (void *) ty + sizeof (void *);
250                 }
251             }
252 #else  /* Untyped Mach IPC flavor. */
253           mach_msg_body_t *body = (void *) (msg + 1);
254           mach_msg_descriptor_t *desc = (void *) (body + 1);
255           mach_msg_descriptor_t *desc_end = desc + body->msgh_descriptor_count;
256           for (; desc < desc_end; ++desc)
257             switch (desc->type.type)
258               {
259               case MACH_MSG_PORT_DESCRIPTOR:
260                 switch (desc->port.disposition)
261                   {
262                   case MACH_MSG_TYPE_MOVE_SEND:
263                     __mach_port_deallocate (mach_task_self (),
264                                             desc->port.name);
265                     desc->port.disposition = MACH_MSG_TYPE_COPY_SEND;
266                     break;
267                   case MACH_MSG_TYPE_COPY_SEND:
268                   case MACH_MSG_TYPE_MOVE_RECEIVE:
269                     break;
270                   default:
271                     assert (! "unexpected port type in interruptible RPC");
272                   }
273                 break;
274               case MACH_MSG_OOL_DESCRIPTOR:
275                 if (desc->out_of_line.deallocate)
276                   __vm_deallocate (__mach_task_self (),
277                                    (vm_address_t) desc->out_of_line.address,
278                                    desc->out_of_line.size);
279                 break;
280               case MACH_MSG_OOL_PORTS_DESCRIPTOR:
281                 switch (desc->ool_ports.disposition)
282                   {
283                   case MACH_MSG_TYPE_MOVE_SEND:
284                     {
285                       mach_msg_size_t i;
286                       const mach_port_t *ports = desc->ool_ports.address;
287                       for (i = 0; i < desc->ool_ports.count; ++i)
288                         __mach_port_deallocate (__mach_task_self (), ports[i]);
289                       desc->ool_ports.disposition = MACH_MSG_TYPE_COPY_SEND;
290                       break;
291                     }
292                   case MACH_MSG_TYPE_COPY_SEND:
293                   case MACH_MSG_TYPE_MOVE_RECEIVE:
294                     break;
295                   default:
296                     assert (! "unexpected port type in interruptible RPC");
297                   }
298                 if (desc->ool_ports.deallocate)
299                   __vm_deallocate (__mach_task_self (),
300                                    (vm_address_t) desc->ool_ports.address,
301                                    desc->ool_ports.count
302                                    * sizeof (mach_port_t));
303                 break;
304               default:
305                 assert (! "unexpected descriptor type in interruptible RPC");
306               }
307 #endif
308         }
309       break;
310
311     case EINTR:
312       /* Either the process was stopped and continued,
313          or the server doesn't support interrupt_operation.  */
314       if (ss->intr_port != MACH_PORT_NULL)
315         /* If this signal was for us and it should interrupt calls, the
316            signal thread will have cleared SS->intr_port.
317            Since it's not cleared, the signal was for another thread,
318            or SA_RESTART is set.  Restart the interrupted call.  */
319         {
320           /* Make sure we have a valid reply port.  The one we were using
321              may have been destroyed by interruption.  */
322           m->header.msgh_local_port = rcv_name = __mig_get_reply_port ();
323           m->header.msgh_bits = msgh_bits;
324           option = user_option;
325           timeout = user_timeout;
326           goto message;
327         }
328       /* FALLTHROUGH */
329
330     case MACH_RCV_PORT_DIED:
331       /* Server didn't respond to interrupt_operation,
332          so the signal thread destroyed the reply port.  */
333       /* FALLTHROUGH */
334
335     interrupted:
336       err = EINTR;
337
338       /* The EINTR return indicates cancellation, so clear the flag.  */
339       ss->cancel = 0;
340       break;
341
342     case MACH_RCV_INTERRUPTED:  /* RPC sent; no reply.  */
343       option &= ~MACH_SEND_MSG; /* Don't send again.  */
344     retry_receive:
345       if (ss->intr_port == MACH_PORT_NULL)
346         {
347           /* This signal or cancellation was for us.  We need to wait for
348              the reply, but not hang forever.  */
349           option |= MACH_RCV_TIMEOUT;
350           /* Never decrease the user's timeout.  */
351           if (!(user_option & MACH_RCV_TIMEOUT)
352               || timeout > _hurd_interrupted_rpc_timeout)
353             timeout = _hurd_interrupted_rpc_timeout;
354         }
355       else
356         {
357           option = user_option;
358           timeout = user_timeout;
359         }
360       goto message;             /* Retry the receive.  */
361
362     case MACH_MSG_SUCCESS:
363       {
364         /* We got a reply.  Was it EINTR?  */
365 #ifdef MACH_MSG_TYPE_BIT
366         const union
367         {
368           mach_msg_type_t t;
369           int i;
370         } check =
371           { t: { MACH_MSG_TYPE_INTEGER_T, sizeof (integer_t) * 8,
372                  1, TRUE, FALSE, FALSE, 0 } };
373 #endif
374
375         if (m->reply.RetCode == EINTR &&
376             m->header.msgh_size == sizeof m->reply &&
377 #ifdef MACH_MSG_TYPE_BIT
378             m->check.type == check.i &&
379 #endif
380             !(m->header.msgh_bits & MACH_MSGH_BITS_COMPLEX))
381           {
382             /* It is indeed EINTR.  Is the interrupt for us?  */
383             if (ss->intr_port != MACH_PORT_NULL)
384               {
385                 /* Nope; repeat the RPC.
386                    XXX Resources moved? */
387
388                 assert (m->header.msgh_id == msgid + 100);
389
390                 /* We know we have a valid reply port, because we just
391                    received the EINTR reply on it.  Restore it and the
392                    other fields in the message header needed for send,
393                    since the header now reflects receipt of the reply.  */
394                 m->header.msgh_local_port = rcv_name;
395                 m->header.msgh_remote_port = remote_port;
396                 m->header.msgh_id = msgid;
397                 m->header.msgh_bits = msgh_bits;
398                 /* Restore the two words clobbered by the reply data.  */
399                 m->request.data = save_data;
400
401                 /* Restore the original mach_msg options.
402                    OPTION may have had MACH_RCV_TIMEOUT added,
403                    and/or MACH_SEND_MSG removed.  */
404                 option = user_option;
405                 timeout = user_timeout;
406
407                 /* Now we are ready to repeat the original message send.  */
408                 goto message;
409               }
410             else
411               /* The EINTR return indicates cancellation,
412                  so clear the flag.  */
413               ss->cancel = 0;
414           }
415       }
416       break;
417
418     default:                    /* Quiet -Wswitch-enum.  */
419       break;
420     }
421
422   ss->intr_port = MACH_PORT_NULL;
423
424   return err;
425 }