(root)/
glibc-2.38/
sysdeps/
mach/
hurd/
kill.c
       1  /* Copyright (C) 1991-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       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     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <errno.h>
      19  #include <sys/types.h>
      20  #include <signal.h>
      21  #include <hurd.h>
      22  #include <hurd/port.h>
      23  #include <hurd/signal.h>
      24  #include <hurd/msg.h>
      25  
      26  /* Send signal SIG to process number PID.  If PID is zero,
      27     send SIG to all processes in the current process's process group.
      28     If PID is < -1, send SIG to all processes in process group - PID.  */
      29  int
      30  __kill (pid_t pid, int sig)
      31  {
      32    int delivered = 0;		/* Set when we deliver any signal.  */
      33    error_t err;
      34    mach_port_t proc;
      35    struct hurd_userlink ulink;
      36  
      37    void kill_pid (pid_t pid) /* Kill one PID.  */
      38      {
      39        /* SIGKILL is not delivered as a normal signal.
      40  	 Sending SIGKILL to a process means to terminate its task.  */
      41        if (sig == SIGKILL)
      42  	/* Fetch the process's task port and terminate the task.  We
      43  	   loop in case the process execs and changes its task port.
      44  	   If the old task port dies after we fetch it but before we
      45  	   send the RPC, we get MACH_SEND_INVALID_DEST; if it dies
      46  	   after we send the RPC request but before it is serviced, we
      47  	   get MIG_SERVER_DIED.  */
      48  	do
      49  	  {
      50  	    task_t refport;
      51  	    err = __proc_pid2task (proc, pid, &refport);
      52  	    /* Ignore zombies.  */
      53  	    if (!err && refport != MACH_PORT_NULL)
      54  	      {
      55  		err = __task_terminate (refport);
      56  		__mach_port_deallocate (__mach_task_self (), refport);
      57  	      }
      58  	  } while (err == MACH_SEND_INVALID_DEST
      59  		   || err == MIG_SERVER_DIED);
      60        else
      61  	{
      62  	  error_t taskerr;
      63  	  error_t kill_port (mach_port_t msgport, mach_port_t refport)
      64  	    {
      65  	      if (msgport != MACH_PORT_NULL)
      66  		/* Send a signal message to his message port.  */
      67  		return __msg_sig_post (msgport, sig, SI_USER, refport);
      68  
      69  	      /* The process has no message port.  Perhaps try direct
      70  		 frobnication of the task.  */
      71  
      72  	      if (taskerr)
      73  		/* If we could not get the task port, we can do nothing.  */
      74  		return taskerr;
      75  
      76  	      if (refport == MACH_PORT_NULL)
      77  		/* proc_pid2task returned success with a null task port.
      78  		   That means the process is a zombie.  Signals
      79  		   to zombies should return success and do nothing.  */
      80  		return 0;
      81  
      82  	      /* For user convenience in the case of a task that has
      83  		 not registered any message port with the proc server,
      84  		 translate a few signals to direct task operations.  */
      85  	      switch (sig)
      86  		{
      87  		  /* The only signals that really make sense for an
      88  		     unregistered task are kill, suspend, and continue.  */
      89  		case SIGSTOP:
      90  		case SIGTSTP:
      91  		  return __task_suspend (refport);
      92  		case SIGCONT:
      93  		  return __task_resume (refport);
      94  		case SIGTERM:
      95  		case SIGQUIT:
      96  		case SIGINT:
      97  		  return __task_terminate (refport);
      98  		default:
      99  		  /* We have full permission to send signals, but there is
     100  		     no meaningful way to express this signal.  */
     101  		  return EPERM;
     102  		}
     103  	    }
     104  	  err = HURD_MSGPORT_RPC (__proc_getmsgport (proc, pid, &msgport),
     105  				  (taskerr = __proc_pid2task (proc, pid,
     106  							      &refport))
     107  				  ? __proc_getsidport (proc, &refport) : 0, 1,
     108  				  kill_port (msgport, refport));
     109  	}
     110        if (! err)
     111  	delivered = 1;
     112      }
     113  
     114    proc = _hurd_port_get (&_hurd_ports[INIT_PORT_PROC], &ulink);
     115  
     116    if (pid <= 0)
     117      {
     118        /* Send SIG to each process in pgrp (- PID).  */
     119        pid_t pidbuf[10], *pids = pidbuf;
     120        mach_msg_type_number_t i, npids = sizeof (pidbuf) / sizeof (pidbuf[0]);
     121  
     122        err = __proc_getpgrppids (proc, - pid, &pids, &npids);
     123        if (!err)
     124  	{
     125  	  for (i = 0; i < npids; ++i)
     126  	    {
     127  	      kill_pid (pids[i]);
     128  	      if (err == ESRCH)
     129  		/* The process died already.  Ignore it.  */
     130  		err = 0;
     131  	    }
     132  	  if (pids != pidbuf)
     133  	    __vm_deallocate (__mach_task_self (),
     134  			     (vm_address_t) pids, npids * sizeof (pids[0]));
     135  	}
     136      }
     137    else
     138      kill_pid (pid);
     139  
     140    _hurd_port_free (&_hurd_ports[INIT_PORT_PROC], &ulink, proc);
     141  
     142    /* If we delivered no signals, but ERR is clear, this must mean that
     143       every kill_pid call failed with ESRCH, meaning all the processes in
     144       the pgrp died between proc_getpgrppids and kill_pid; in that case we
     145       fail with ESRCH.  */
     146    return delivered ? 0 : __hurd_fail (err ?: ESRCH);
     147  }
     148  
     149  libc_hidden_def (__kill)
     150  weak_alias (__kill, kill)