Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!ucbvax!agate!helios.ee.lbl.gov!ncis.llnl.gov!lll-winken!uunet!ncrlnk!ncr-sd!hp-sdd!hplabs!hpl-opus!hpccc!hp-ses!hpcea!hpausla!brianw From: brianw@hpausla.HP.COM (Brian Wallis) Newsgroups: comp.mail.elm Subject: Re: newmail also displays on console (bug) Message-ID: <890003@hpausla.HP.COM> Date: 16 Mar 89 03:59:35 GMT References: <316@wubios.wustl.edu> Organization: HP Australian Software Operation Lines: 95 > / hpausla:comp.mail.elm / randy@cctb.mn.org (Randy Orrison) / 3:17 pm Mar 13, 1989 / > In article <7310001@hpscdc.HP.COM> schmitz@hpscdc.HP.COM (John Schmitz) writes: > > Re: newmail not going away: > > Would it also be possible to record the id of the parent's shell and > > do a kill(parent_id, 0) to detect the parent shell going away? > > Why not just do a getppid() when it starts, record that, and if it ever > changes, exit()? I don't have my man pages handy, but I would be very > surprised if getppid() returned the old parent pid after it had exited, > in any version. This would also work in braindamaged configurations > where the process doesn't get a SIGHUP for some reason. This won't work when you use AUTO_BACKGROUND as we do, and in 2.1 version this is done if AUTO_BACKGROUND is not defined, actually it checks if getppid() returns 1, ie. init is parent. I changed it so that the parent gets it's parent's pid and then the child checks if it's parent's parent still exists using 'getpgrp2(pid)' which fails if the process no longer exists or is not of the same uid. I know, there is a very small hole there, but who cares? I don't. Here are the diffs for my version RCS file: RCS/newmail.c,v retrieving revision 2.1 rdiff -c -r2.1 newmail.c *** /tmp/,RCSt1a26904 Thu Mar 16 13:55:11 1989 --- newmail.c Thu Mar 16 11:34:46 1989 *************** *** 135,140 long lastsize, newsize; /* file size for comparison.. */ interval_time = DEFAULT_INTERVAL; opterr = 0; --- 135,144 ----- long lastsize, newsize; /* file size for comparison.. */ + #ifdef AUTO_BACKGROUND + int ppid=getppid(); /* parent's pid, probably a shell */ + #endif + interval_time = DEFAULT_INTERVAL; opterr = 0; *************** *** 179,185 #ifdef AUTO_BACKGROUND if (! in_window) { if (fork()) /* automatically puts this task in background! */ ! exit(0); signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); --- 183,189 ----- #ifdef AUTO_BACKGROUND if (! in_window) { if (fork()) /* automatically puts this task in background! */ ! exit(0); /* and the parent exits */ signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); *************** *** 194,199 #ifndef AUTO_BACKGROUND /* won't work if we're nested this deep! */ if (getppid() == 1) /* we've lost our shell! */ exit(); #endif if (! isatty(1)) /* we're not sending output to a tty any more */ exit(); --- 198,214 ----- #ifndef AUTO_BACKGROUND /* won't work if we're nested this deep! */ if (getppid() == 1) /* we've lost our shell! */ exit(); + #else + /* + do a getpgrp2() on our parent's parent. This was where we were started + from originally before AUTO_BACKGROUNDing, so if it goes away (and this + call returns an error) we can exit. It is possible that the process id + in ppid is a valid process again, but it is unlikely that it is ours. + Bad luck if it is! (life wasn't meant to be easy!) + */ + if (getpgrp2(ppid) == -1){ /* parent's parent has gone away! */ + exit(); + } #endif if (! isatty(1)) /* we're not sending output to a tty any more */ exit();