signal & kill, and how do you find the process name?

signal & kill, and how do you find the process name?

Post by Simon Marsha » Wed, 18 Sep 1991 23:34:46



        I'm writing some stuff to `kill (SIGUSR1, pid)', the process `pid'
     has done a `signal (func, SIGUSR1)' at some stage.

        Is there any way of knowing whether that process has decided that
     it wants to detect the signal by doing a signal() call?  In other
     words, can the process doing the kill() know if the process it is
     signalling has done something with it?

        Also, the easy question I'm sure, how can you get the process name
     (that is, its argv[0]) from the pid?  Is there a system call for
     this, I can't see one from doing man apropos.

        Please email/post if you think it's of general use to others.

        Thanx in advance, Simon.
_______________________________________________________________________________
Simon Marshall, Dept. of Computer Science, University of Hull, Hull HU6 7RX, UK
              "``La la la la la la la la la'' means I love you."

 
 
 

signal & kill, and how do you find the process name?

Post by Jonathan I. Kame » Sat, 21 Sep 1991 05:43:23



|>   Is there any way of knowing whether that process has decided that
|>      it wants to detect the signal by doing a signal() call?  In other
|>      words, can the process doing the kill() know if the process it is
|>      signalling has done something with it?

No.  The only thing the sending process can determine (without digging through
kernel structures) is whether or not the signal was successfully sent (i.e.
whether or not the receiving process exists and the sending process has
permission to send it the signal).

|>   Also, the easy question I'm sure, how can you get the process name
|>      (that is, its argv[0]) from the pid?  Is there a system call for
|>      this, I can't see one from doing man apropos.

No, not really an easy question.  There is no "system call" for this.  Some
platforms (e.g. SunOS's kvm_* routines) provide routines for reading kernel
structures, but many platforms do not, and in any case, the method of reading
stuff from the kernel varies widely from system to system.

Some platforms support a /proc filesystem whose "files" are PIDs that you can
read (if you have permission) to get data about processes, but again, the
precise methods vary widely from system to system.

There are various packages publicly available that do kernel mucking to find
out process information on a variety of different systems.  I think Dan
Bernstein posted some routines to do this on BSD-like system.  There's also
his "pff" program (I think that's what it's called) and the "ofiles" program
as well.  If you want to do this on a variety of different environments,
you're going to have to dig through these sources to see how things are done.

If you want to do it on only one platform, you'll have to tell us which
platform or we can't really help all that much with specific details :-).

--
Jonathan Kamens                               USnail:
MIT IS/Athena                                   11 Ashford Terrace

Office: 617-253-8085                          Home: 617-782-0710

 
 
 

signal & kill, and how do you find the process name?

Post by Simon Marsha » Sun, 22 Sep 1991 00:15:09


Quote:Jonathan I. Kamens writes:

|>    Also, the easy question I'm sure, how can you get the process name
|>      (that is, its argv[0]) from the pid?  Is there a system call for
|>      this, I can't see one from doing man apropos.

| No, not really an easy question.  There is no "system call" for this.  ...
| The method of reading stuff from the kernel varies widely from system to
| system.

        Well, I had to cover myself in case I'd missed something obvious!

| If you want to do it on only one platform, you'll have to tell us which
| platform or we can't really help all that much with specific details :-).


     things out.  Unfortunately, I need something as platform independent
     as can be - it's actually an X11 client - so hacking is out.  It's
     tough, I'll have to do without it.

        Thanks again, Simon.
_______________________________________________________________________________
Simon Marshall, Dept. of Computer Science, University of Hull, Hull HU6 7RX, UK
              "``La la la la la la la la la'' means I love you."

 
 
 

signal & kill, and how do you find the process name?

Post by Dan Bernste » Tue, 24 Sep 1991 07:49:05



Quote:> > Is there any way of knowing whether that process has decided that it
> > wants to detect the signal by doing a signal() call?
> Not if you want to stay even vaguely portable; you'd have to grub
> through kernel data structures on most (all?) systems.

But on lots of popular systems, including SunOS 4.0.3 and 4.1[.1],
Ultrix 4.*, Convex UNIX, and more, you can use my pff program with the
``full'' style. For instance:

  % pff -sfull $$ | grep '^signals'
  signals: d.C:C.i.d.d.d.d. d.d.d.d.d.d.d.i. d.d.i.d.C.i.i.d. C.C.d.d.d.d.d.d.

Not beautiful output, but it displays the desired information.

Quote:> > Also, the easy question I'm sure, how can you get the process name
> > (that is, its argv[0]) from the pid?
> System-specific, and almost always difficult (often requiring
> privilege).

This is such a common question that the next release of my kstuff
package includes a program which prints u.u_comm in a format that any
program can use. Below is a simplified version which works with the
previous release, kstuff 0.18, available from stealth.acf.nyu.edu in
pub/hier/kstuff.

---Dan

#include <stdio.h>
#include "proctable.h" /* for getproctable() */
#include "strerr.h" /* for strerr() */
#include "getuser.h" /* for getuser() */
#include "structuser.h" /* for u's elements, notably u_comm */

main()
{
 struct proc *p;
 struct user *u;
 int i;

 p = getproctable();
 if (!p) { fprintf(stderr,"pidcomms: fatal: cannot get process table: %s\n"
   ,strerr(proctablestrerr)); exit(1); }
 for (i = 0;i < mynproc;++i)
   if (p[i].p_pid)
    {
     u = getuser(p + i);
     /* we could complain with strerr(getuserstrerr) if !u */
     if (u) /* actually we should make sure u->u_comm has no newlines */
       printf("%9d %s\n",p[i].p_pid,u->u_comm);
    }

Quote:}

 
 
 

signal & kill, and how do you find the process name?

Post by der Mou » Tue, 24 Sep 1991 05:10:21



Quote:> I'm writing some stuff to `kill (SIGUSR1, pid)', the process `pid'
> has done a `signal (func, SIGUSR1)' at some stage.
> Is there any way of knowing whether that process has decided that it
> wants to detect the signal by doing a signal() call?

Not if you want to stay even vaguely portable; you'd have to grub
through kernel data structures on most (all?) systems.

Quote:> Also, the easy question I'm sure, how can you get the process name
> (that is, its argv[0]) from the pid?

System-specific, and almost always difficult (often requiring
privilege).

You also need to decide what you want: argv[0] at startup? argv[0] now?
u.u_comm, or something like it?  (The u.u_comm I'm talking about is the
first N characters of the last component of the pathname passed to
exec (not argv[0]), where N is typically about 16.)  On systems I'm
familiar with, the first is not possible and the other two require
grubbing through the kernel, which generally requires privilege...

                                        der Mouse

                        old: mcgill-vision!mouse