Back to index

glibc  2.9
setsid.c
Go to the documentation of this file.
00001 /* Copyright (C) 1993,94,95,97,99 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <unistd.h>
00021 #include <hurd.h>
00022 #include <hurd/port.h>
00023 #include <hurd/fd.h>
00024 
00025 /* Create a new session with the calling process as its leader.
00026    The process group IDs of the session and the calling process
00027    are set to the process ID of the calling process, which is returned.  */
00028 pid_t
00029 __setsid (void)
00030 {
00031   error_t err;
00032   unsigned int stamp;
00033 
00034   HURD_CRITICAL_BEGIN;
00035   __mutex_lock (&_hurd_dtable_lock);
00036 
00037   stamp = _hurd_pids_changed_stamp; /* Atomic fetch.  */
00038 
00039   /* Tell the proc server we want to start a new session.  */
00040   err = __USEPORT (PROC, __proc_setsid (port));
00041   if (err)
00042     __mutex_unlock (&_hurd_dtable_lock);
00043   else
00044     {
00045       /* Punt our current ctty, and update the dtable accordingly.  We hold
00046         the dtable lock from before the proc_setsid call through clearing
00047         the cttyid port and processing the dtable, so that we can be sure
00048         that it's all done by the time the signal thread processes the
00049         pgrp change notification.  */
00050       _hurd_locked_install_cttyid (MACH_PORT_NULL);
00051 
00052       /* Synchronize with the signal thread to make sure we have received
00053         and processed proc_newids before returning to the user.
00054         This is necessary to ensure that _hurd_pgrp (and thus the value
00055         returned by `getpgrp ()' in other threads) has been updated before
00056         we return.  */
00057       while (_hurd_pids_changed_stamp == stamp)
00058        {
00059 #ifdef noteven
00060          /* XXX we have no need for a mutex, but cthreads demands one.  */
00061          __condition_wait (&_hurd_pids_changed_sync, NULL);
00062 #else
00063          __swtch_pri (0);
00064 #endif
00065        }
00066     }
00067 
00068   HURD_CRITICAL_END;
00069 
00070   return err ? __hurd_fail (err) : _hurd_pgrp;
00071 }
00072 
00073 weak_alias (__setsid, setsid)