Sun May 12 22:52:22 1996 Bruce Elliott <belliott@accessone.com>
[glibc.git] / sysdeps / mach / hurd / kill.c
blob6dcc5b530fa9a6893618bedea4b4c9714ab062b6
1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <signal.h>
22 #include <hurd.h>
23 #include <hurd/port.h>
24 #include <hurd/signal.h>
25 #include <hurd/msg.h>
27 /* Send signal SIG to process number PID. If PID is zero,
28 send SIG to all processes in the current process's process group.
29 If PID is < -1, send SIG to all processes in process group - PID. */
30 int
31 __kill (pid_t pid, int sig)
33 int delivered = 0; /* Set when we deliver any signal. */
34 error_t err;
35 mach_port_t proc;
36 struct hurd_userlink ulink;
38 void kill_pid (pid_t pid) /* Kill one PID. */
40 /* SIGKILL is not delivered as a normal signal.
41 Sending SIGKILL to a process means to terminate its task. */
42 if (sig == SIGKILL)
43 /* Fetch the process's task port and terminate the task. We
44 loop in case the process execs and changes its task port.
45 If the old task port dies after we fetch it but before we
46 send the RPC, we get MACH_SEND_INVALID_DEST; if it dies
47 after we send the RPC request but before it is serviced, we
48 get MIG_SERVER_DIED. */
51 task_t refport;
52 err = __proc_pid2task (proc, pid, &refport);
53 /* Ignore zombies. */
54 if (!err && refport != MACH_PORT_NULL)
56 err = __task_terminate (refport);
57 __mach_port_deallocate (__mach_task_self (), refport);
59 } while (err == MACH_SEND_INVALID_DEST ||
60 err == MIG_SERVER_DIED);
61 else
63 error_t taskerr;
64 error_t kill_port (mach_port_t msgport, mach_port_t refport)
66 if (msgport != MACH_PORT_NULL)
67 /* Send a signal message to his message port. */
68 return __msg_sig_post (msgport, sig, 0, refport);
70 /* The process has no message port. Perhaps try direct
71 frobnication of the task. */
73 if (taskerr)
74 /* If we could not get the task port, we can do nothing. */
75 return taskerr;
77 if (refport == MACH_PORT_NULL)
78 /* proc_pid2task returned success with a null task port.
79 That means the process is a zombie. Signals
80 to zombies should return success and do nothing. */
81 return 0;
83 /* For user convenience in the case of a task that has
84 not registered any message port with the proc server,
85 translate a few signals to direct task operations. */
86 switch (sig)
88 /* The only signals that really make sense for an
89 unregistered task are kill, suspend, and continue. */
90 case SIGSTOP:
91 case SIGTSTP:
92 return __task_suspend (refport);
93 case SIGCONT:
94 return __task_resume (refport);
95 case SIGTERM:
96 case SIGQUIT:
97 case SIGINT:
98 return __task_terminate (refport);
99 default:
100 /* We have full permission to send signals, but there is
101 no meaningful way to express this signal. */
102 return EPERM;
105 err = HURD_MSGPORT_RPC (__proc_getmsgport (proc, pid, &msgport),
106 (taskerr = __proc_pid2task (proc, pid,
107 &refport)) ?
108 __proc_getsidport (proc, &refport) : 0, 1,
109 kill_port (msgport, refport));
111 if (! err)
112 delivered = 1;
115 proc = _hurd_port_get (&_hurd_ports[INIT_PORT_PROC], &ulink);
117 if (pid <= 0)
119 /* Send SIG to each process in pgrp (- PID). */
120 pid_t pidbuf[10], *pids = pidbuf;
121 mach_msg_type_number_t i, npids = sizeof (pidbuf) / sizeof (pidbuf[0]);
123 err = __proc_getpgrppids (proc, - pid, &pids, &npids);
124 if (!err)
126 for (i = 0; i < npids; ++i)
128 kill_pid (pids[i]);
129 if (err == ESRCH)
130 /* The process died already. Ignore it. */
131 err = 0;
133 if (pids != pidbuf)
134 __vm_deallocate (__mach_task_self (),
135 (vm_address_t) pids, npids * sizeof (pids[0]));
138 else
139 kill_pid (pid);
141 _hurd_port_free (&_hurd_ports[INIT_PORT_PROC], &ulink, proc);
143 /* If we delivered no signals, but ERR is clear, this must mean that
144 every kill_pid call failed with ESRCH, meaning all the processes in
145 the pgrp died between proc_getpgrppids and kill_pid; in that case we
146 fail with ESRCH. */
147 return delivered ? 0 : __hurd_fail (err ?: ESRCH);
150 weak_alias (__kill, kill)