[comp.lang.c] logout

garath@ais.org (Belgarath) (02/05/91)

	Hi.  I've created a generic menu program for new users on our system.
I have everything working now except for a logout option.  Is there a way to
have to program kill  all processes/jobs and then log the user out or do they
need to quit the program and then exit?
	Please respond via e-mail.  Thanks.
P.S.  How could I have had this expire in, say 10 days?  I don't see expire at
the top as one of the options I could fill in.


-- 
+------------------+-----------------------------------------------+
| Scott            | The University of Michigan, Ann Arbor         |
| garath@ais.org   | Computer Science Major                        |
+------------------+-----------------------------------------------+

drh@duke.cs.duke.edu (D. Richard Hipp) (02/05/91)

In article <+W9+Y=A@irie.ais.org> garath@ais.org (Belgarath) writes:
>
>	Hi.  I've created a generic menu program for new users on our system.
>I have everything working now except for a logout option.  Is there a way to
>have to program kill  all processes/jobs and then log the user out or do they
>need to quit the program and then exit?
>	Please respond via e-mail.  Thanks.
>P.S.  How could I have had this expire in, say 10 days?  I don't see expire at
>the top as one of the options I could fill in.

Off the top of my head...

  system("ps -x | awk '$1!=\"PID\"{print $1}' | sort -n |"
         "while read name; do kill -9 $name; done;");

The above will kill off every process associated with your tty.  That
should probably be sufficient to log you out.  Notice, however, that
this technique is not especially efficient, nor is it very portable.
In fact, I can think of a pathological case where it won't even work.
(Specifically, if the process kills off itself before it gets your
login shell.)  Perhaps the creative use of options to "ps", or another
filter in the pipe can overcome this bug.

You also might try:

  kill(getppid(),9)

This will kill off the parent of the process running, which in most
cases will be your shell, but is by no means guarenteed to be.

I`ve never tried it, but according to my man-pages, the following should
do a more complete job of killing everyone off:

  killpg(getpgrp(0),9)

All of these solutions are, of course, specific to UNIX.

gwc@root.co.uk (Geoff Clare) (02/07/91)

[I have cross-posted to comp.unix.questions and directed followups there
as this topic is UNIX specific, and not appropriate for comp.lang.c]

In article <+W9+Y=A@irie.ais.org> garath@ais.org (Belgarath) writes:
>
>	Hi.  I've created a generic menu program for new users on our system.
>I have everything working now except for a logout option.  Is there a way to
>have to program kill  all processes/jobs and then log the user out or do they
>need to quit the program and then exit?
>	Please respond via e-mail.  Thanks.
>P.S.  How could I have had this expire in, say 10 days?  I don't see expire at
>the top as one of the options I could fill in.

In <665707935@romeo.cs.duke.edu> drh@duke.cs.duke.edu (D. Richard Hipp) writes:

[stuff deleted]

>I`ve never tried it, but according to my man-pages, the following should
>do a more complete job of killing everyone off:

>  killpg(getpgrp(0),9)

This works, but killpg() is not very portable.  On all UNIX systems I'm
familiar with, the same effect can be obtained by using kill() with a
process ID of 0.  Also, signal 9 (SIGKILL) is not a good choice of
signal, because it cannot be caught and so the processes will not be
able to clean up.  As a rule SIGKILL should only be used as a last
resort to kill processes that otherwise refuse to die.  In the case
under discussion (simulating a logout), the appropriate signal is
SIGHUP, which is what the processes would receive when a user logs off
leaving background jobs running.

So the best solution is

   kill(0, SIGHUP);

or from a shell

   kill -1 0

If you want to kill processes which have been run with "nohup", use
SIGKILL instead of SIGHUP.

-- 
Geoff Clare <gwc@root.co.uk>  (Dumb American mailers: ...!uunet!root.co.uk!gwc)
UniSoft Limited, London, England.   Tel: +44 71 729 3773   Fax: +44 71 729 3273

gwc@root.co.uk (Geoff Clare) (02/11/91)

In <2598@root44.co.uk> I wrote:

>If you want to kill processes which have been run with "nohup", use
>SIGKILL instead of SIGHUP.

Oops, bit of finger trouble there.  Of course, I meant "use SIGTERM
instead of SIGHUP".
-- 
Geoff Clare <gwc@root.co.uk>  (Dumb American mailers: ...!uunet!root.co.uk!gwc)
UniSoft Limited, London, England.   Tel: +44 71 729 3773   Fax: +44 71 729 3273