Back to index

glibc  2.9
hurdinit.c
Go to the documentation of this file.
00001 /* Copyright (C) 1992,93,94,95,96,97,98,99,2000,01
00002        Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <sys/stat.h>
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <unistd.h>
00024 #include <hurd.h>
00025 #include <hurd/port.h>
00026 #include "set-hooks.h"
00027 #include "hurdmalloc.h"            /* XXX */
00028 
00029 
00030 int _hurd_exec_flags;
00031 struct hurd_port *_hurd_ports;
00032 unsigned int _hurd_nports;
00033 mode_t _hurd_umask;
00034 sigset_t _hurdsig_traced;
00035 
00036 char **__libc_argv;
00037 int __libc_argc;
00038 
00039 
00040 error_t
00041 _hurd_ports_use (int which, error_t (*operate) (mach_port_t))
00042 {
00043   if (__builtin_expect (_hurd_ports == NULL, 0))
00044     /* This means that _hurd_init has not been called yet, which is
00045        normally only the case in the bootstrap filesystem, and there
00046        only in the early phases of booting.  */
00047     return EGRATUITOUS;
00048 
00049   return HURD_PORT_USE (&_hurd_ports[which], (*operate) (port));
00050 }
00051 
00052 DEFINE_HOOK (_hurd_subinit, (void));
00053 
00054 __typeof (_hurd_proc_init) _hurd_new_proc_init;  /* below */
00055 
00056 /* Initialize the library data structures from the
00057    ints and ports passed to us by the exec server.
00058 
00059    PORTARRAY and INTARRAY are vm_deallocate'd.  */
00060 
00061 void
00062 _hurd_init (int flags, char **argv,
00063            mach_port_t *portarray, size_t portarraysize,
00064            int *intarray, size_t intarraysize)
00065 {
00066   size_t i;
00067 
00068   _hurd_exec_flags = flags;
00069 
00070   _hurd_ports = malloc (portarraysize * sizeof (*_hurd_ports));
00071   if (_hurd_ports == NULL)
00072     __libc_fatal ("Can't allocate _hurd_ports\n");
00073   _hurd_nports = portarraysize;
00074 
00075   /* See what ports we were passed.  */
00076   for (i = 0; i < portarraysize; ++i)
00077     _hurd_port_init (&_hurd_ports[i], portarray[i]);
00078 
00079   /* When the user asks for the bootstrap port,
00080      he will get the one the exec server passed us.  */
00081   __task_set_special_port (__mach_task_self (), TASK_BOOTSTRAP_PORT,
00082                         portarray[INIT_PORT_BOOTSTRAP]);
00083 
00084   if (intarraysize > INIT_UMASK)
00085     _hurd_umask = intarray[INIT_UMASK] & 0777;
00086   else
00087     _hurd_umask = CMASK;
00088 
00089   if (intarraysize > INIT_TRACEMASK)
00090     _hurdsig_traced = intarray[INIT_TRACEMASK];
00091 
00092   /* Tell the proc server we exist, if it does.  */
00093   if (portarray[INIT_PORT_PROC] != MACH_PORT_NULL)
00094     _hurd_new_proc_init (argv, intarray, intarraysize);
00095 
00096   /* All done with init ints and ports.  */
00097   __vm_deallocate (__mach_task_self (),
00098                  (vm_address_t) intarray,
00099                  intarraysize * sizeof (int));
00100   __vm_deallocate (__mach_task_self (),
00101                  (vm_address_t) portarray,
00102                  portarraysize * sizeof (mach_port_t));
00103 
00104   if (flags & EXEC_SECURE)
00105     /* XXX if secure exec, elide environment variables
00106        which the library uses and could be security holes.
00107        CORESERVER, COREFILE
00108        */ ;
00109 
00110   /* Call other things which want to do some initialization.  These are not
00111      on the __libc_subinit hook because things there like to be able to
00112      assume the availability of the POSIX.1 services we provide.  */
00113   RUN_HOOK (_hurd_subinit, ());
00114 }
00115 
00116 #include <hurd/signal.h>
00117 
00118 /* The user can do "int _hide_arguments = 1;" to make
00119    sure the arguments are never visible with `ps'.  */
00120 int _hide_arguments, _hide_environment;
00121 
00122 /* Hook for things which should be initialized as soon as the proc
00123    server is available.  */
00124 DEFINE_HOOK (_hurd_proc_subinit, (void));
00125 
00126 /* Do startup handshaking with the proc server just installed in _hurd_ports.
00127    Call _hurdsig_init to set up signal processing.  */
00128 
00129 void
00130 _hurd_new_proc_init (char **argv,
00131                    const int *intarray, size_t intarraysize)
00132 {
00133   mach_port_t oldmsg;
00134   struct hurd_userlink ulink;
00135   process_t procserver;
00136 
00137   /* Initialize the signal code; Mach exceptions will become signals.  */
00138   _hurdsig_init (intarray, intarraysize);
00139 
00140   /* The signal thread is now prepared to receive messages.
00141      It is safe to give the port to the proc server.  */
00142 
00143   procserver = _hurd_port_get (&_hurd_ports[INIT_PORT_PROC], &ulink);
00144 
00145   /* Give the proc server our message port.  */
00146   __proc_setmsgport (procserver, _hurd_msgport, &oldmsg);
00147   if (oldmsg != MACH_PORT_NULL)
00148     /* Deallocate the old msg port we replaced.  */
00149     __mach_port_deallocate (__mach_task_self (), oldmsg);
00150 
00151   /* Tell the proc server where our args and environment are.  */
00152   __proc_set_arg_locations (procserver,
00153                          _hide_arguments ? 0 : (vm_address_t) argv,
00154                          _hide_environment ? 0 : (vm_address_t) __environ);
00155 
00156   _hurd_port_free (&_hurd_ports[INIT_PORT_PROC], &ulink, procserver);
00157 
00158   /* Initialize proc server-assisted fault recovery for the signal thread.  */
00159   _hurdsig_fault_init ();
00160 
00161   /* Call other things which want to do some initialization.  These are not
00162      on the _hurd_subinit hook because things there assume that things done
00163      here, like _hurd_pid, are already initialized.  */
00164   RUN_HOOK (_hurd_proc_subinit, ());
00165 
00166   /* XXX This code should probably be removed entirely at some point.  This
00167      conditional should make it reasonably usable with old gdb's for a
00168      while.  Eventually it probably makes most sense for the exec server to
00169      mask out EXEC_SIGTRAP so the debugged program is closer to not being
00170      able to tell it's being debugged.  */
00171   if (!__sigisemptyset (&_hurdsig_traced)
00172 #ifdef EXEC_SIGTRAP
00173       && !(_hurd_exec_flags & EXEC_SIGTRAP)
00174 #endif
00175       )
00176     /* This process is "traced", meaning it should stop on signals or exec.
00177        We are all set up now to handle signals.  Stop ourselves, to inform
00178        our parent (presumably a debugger) that the exec has completed.  */
00179     __msg_sig_post (_hurd_msgport, SIGTRAP, 0, __mach_task_self ());
00180 }
00181 
00182 #include <shlib-compat.h>
00183 versioned_symbol (libc, _hurd_new_proc_init, _hurd_proc_init, GLIBC_2_1);
00184 
00185 /* Called when we get a message telling us to change our proc server port.  */
00186 
00187 error_t
00188 _hurd_setproc (process_t procserver)
00189 {
00190   error_t err;
00191   mach_port_t oldmsg;
00192 
00193   /* Give the proc server our message port.  */
00194   if (err = __proc_setmsgport (procserver, _hurd_msgport, &oldmsg))
00195     return err;
00196   if (oldmsg != MACH_PORT_NULL)
00197     /* Deallocate the old msg port we replaced.  */
00198     __mach_port_deallocate (__mach_task_self (), oldmsg);
00199 
00200   /* Tell the proc server where our args and environment are.  */
00201   if (err = __proc_set_arg_locations (procserver,
00202                                   _hide_arguments ? 0 :
00203                                   (vm_address_t) __libc_argv,
00204                                   _hide_environment ? 0 :
00205                                   (vm_address_t) __environ))
00206     return err;
00207 
00208   /* Those calls worked, so the port looks good.  */
00209   _hurd_port_set (&_hurd_ports[INIT_PORT_PROC], procserver);
00210 
00211   {
00212     pid_t oldpgrp = _hurd_pgrp;
00213 
00214     /* Call these functions again so they can fetch the
00215        new information from the new proc server.  */
00216     RUN_HOOK (_hurd_proc_subinit, ());
00217 
00218     if (_hurd_pgrp != oldpgrp)
00219       {
00220        /* Run things that want notification of a pgrp change.  */
00221        DECLARE_HOOK (_hurd_pgrp_changed_hook, (pid_t));
00222        RUN_HOOK (_hurd_pgrp_changed_hook, (_hurd_pgrp));
00223       }
00224   }
00225 
00226   return 0;
00227 }