Blame hurd/hurdkill.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <sys/types.h>
Packit 6c4009
#include <signal.h>
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/port.h>
Packit 6c4009
#include <hurd/signal.h>
Packit 6c4009
#include <hurd/msg.h>
Packit 6c4009
Packit 6c4009
/* Send a `sig_post' RPC to process number PID.  If PID is zero,
Packit 6c4009
   send the message to all processes in the current process's process group.
Packit 6c4009
   If PID is < -1, send SIG to all processes in process group - PID.
Packit 6c4009
   SIG and REFPORT are passed along in the request message.  */
Packit 6c4009
error_t
Packit 6c4009
_hurd_sig_post (pid_t pid, int sig, mach_port_t arg_refport)
Packit 6c4009
{
Packit 6c4009
  int delivered = 0;		/* Set when we deliver any signal.  */
Packit 6c4009
  error_t err;
Packit 6c4009
  mach_port_t proc;
Packit 6c4009
  struct hurd_userlink ulink;
Packit 6c4009
Packit 6c4009
  inline void kill_pid (pid_t pid) /* Kill one PID.  */
Packit 6c4009
    {
Packit 6c4009
      err = HURD_MSGPORT_RPC (__proc_getmsgport (proc, pid, &msgport),
Packit 6c4009
			      (refport = arg_refport, 0), 0,
Packit 6c4009
			      /* If no message port we cannot send signals.  */
Packit 6c4009
			      msgport == MACH_PORT_NULL ? EPERM :
Packit 6c4009
			      __msg_sig_post (msgport, sig, 0, refport));
Packit 6c4009
      if (! err)
Packit 6c4009
	delivered = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  proc = _hurd_port_get (&_hurd_ports[INIT_PORT_PROC], &ulink);
Packit 6c4009
Packit 6c4009
  if (pid <= 0)
Packit 6c4009
    {
Packit 6c4009
      /* Send SIG to each process in pgrp (- PID).  */
Packit 6c4009
      mach_msg_type_number_t npids = 10, i;
Packit 6c4009
      pid_t pidsbuf[10], *pids = pidsbuf;
Packit 6c4009
Packit 6c4009
      err = __proc_getpgrppids (proc, - pid, &pids, &npids);
Packit 6c4009
      if (!err)
Packit 6c4009
	{
Packit 6c4009
	  int self = 0;
Packit 6c4009
	  for (i = 0; i < npids; ++i)
Packit 6c4009
	    if (pids[i] == _hurd_pid)
Packit 6c4009
	      /* We must do ourselves last so we are not suspended
Packit 6c4009
		 and fail to suspend the other processes in the pgrp.  */
Packit 6c4009
	      self = 1;
Packit 6c4009
	    else
Packit 6c4009
	      {
Packit 6c4009
		kill_pid (pids[i]);
Packit 6c4009
		if (err == ESRCH)
Packit 6c4009
		  /* The process died already.  Ignore it.  */
Packit 6c4009
		  err = 0;
Packit 6c4009
	      }
Packit 6c4009
	  if (pids != pidsbuf)
Packit 6c4009
	    __vm_deallocate (__mach_task_self (),
Packit 6c4009
			     (vm_address_t) pids, npids * sizeof (pids[0]));
Packit 6c4009
Packit 6c4009
	  if (self)
Packit 6c4009
	    kill_pid (_hurd_pid);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    kill_pid (pid);
Packit 6c4009
Packit 6c4009
  _hurd_port_free (&_hurd_ports[INIT_PORT_PROC], &ulink, proc);
Packit 6c4009
Packit 6c4009
  /* If we delivered no signals, but ERR is clear, this must mean that
Packit 6c4009
     every kill_pid call failed with ESRCH, meaning all the processes in
Packit 6c4009
     the pgrp died between proc_getpgrppids and kill_pid; in that case we
Packit 6c4009
     fail with ESRCH.  */
Packit 6c4009
  return delivered ? 0 : err ?: ESRCH;
Packit 6c4009
}
Packit 6c4009
weak_alias (_hurd_sig_post, hurd_sig_post)