jlk@eisx.UUCP (Joe Klein) (09/18/84)
Can anyone tell me how I can determine from within a C program
whether that program is being executed via the 'cron' or not ?
thanx
Joe Klein
{eisx!jlk}jab@uokvax.UUCP (09/27/84)
/***** uokvax:net.lang.c / eisx!jlk / 3:57 am Sep 22, 1984 */
Can anyone tell me how I can determine from within a C program
whether that program is being executed via the 'cron' or not ?
/* ---------- */
If you're very, very lucky, "cron" is run from "/etc/rc" and as a result,
doesn't have a controlling tty. As a result, "/dev/tty" won't make sense,
so an
open("/dev/tty", 1)
should fail with some version-specific error. (For example, on the 4.1c system
I'm on, it's ENXIO.)
Jeff Bowles
Lisle, ILdave@utcsrgv.UUCP (Dave Sherman) (09/30/84)
In article <784@eisx.UUCP> jlk@eisx.UUCP (Joe Klein) writes:
~|
~| Can anyone tell me how I can determine from within a C program
~| whether that program is being executed via the 'cron' or not ?
Sure. On many systems, cron does a setuid(1). If you're lucky, UID 1
is never actually used as a login ID (it might be called sys, bin,
daemon or a number of other things). A getuid() will therefore help
you out, for practical purposes.
Another route is to do a getppid() [get parent's process ID], if
that system call is supported. However, this requires predicting the
PID which cron will have based on the sequence of calls in /etc/rc,
and makes your code unportable and likely to break as soon as someone
changes /etc/rc.
The best solution is to put in a flag (call it -c), to be used when
the program is called from /etc/rc. Normal people type "foo", and
/etc/rc will contain "foo -c". Voila.
Dave Sherman
Toronto
--
{ allegra cornell decvax ihnp4 linus utzoo }!utcsrgv!dave