Added at/atrm/atq/batch from Linux as hacked by Chris Demetriou.

This commit is contained in:
Nate Williams 1994-01-05 01:09:14 +00:00
parent 31098bd48f
commit d78e98d22e
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=941
10 changed files with 1704 additions and 0 deletions

15
usr.bin/at/Makefile Normal file
View File

@ -0,0 +1,15 @@
# $Id: Makefile,v 1.1 1993/12/05 11:35:35 cgd Exp $
PROG= at
SRCS= at.c panic.c parsetime.c
LINKS= ${BINDIR}/at ${BINDIR}/atq \
${BINDIR}/at ${BINDIR}/atrm \
${BINDIR}/at ${BINDIR}/batch
MLINKS= at.1 batch.1 \
at.1 atq.1 \
at.1 atrm.1
BINOWN= root
BINMODE= 4555
.include <bsd.prog.mk>

216
usr.bin/at/at.1 Normal file
View File

@ -0,0 +1,216 @@
.\"
.\" Copyright (c) 1993 Christopher G. Demetriou
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\" 3. All advertising materials mentioning features or use of this software
.\" must display the following acknowledgement:
.\" This product includes software developed by Christopher G. Demetriou.
.\" 3. The name of the author may not be used to endorse or promote products
.\" derived from this software withough specific prior written permission
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
.\"
.\" $Id: at.1,v 1.1 1993/12/05 11:36:29 cgd Exp $
.\"
.Dd December 5, 1993
.Dt "AT" 1
.Os NetBSD 0.9a
.Sh NAME
.Nm at, batch, atq, atrm
.Nd queue, examine, or delete jobs for later execution
.\"
.Sh SYOPSIS
.Nm at
.Op Fl q Ar queue
.Op Fl f Ar file
.Op Fl m
.Ar time
.Pp
.Nm atq
.Op Fl q Ar queue
.Op Fl v
.Pp
.Nm atrm
.Ar job
.Op Ar job ...
.Pp
.Nm batch
.Op Fl f Ar file
.Op Fl m
.Ar time
.Sh DESCRIPTION
The
.Nm at
and
.Nm batch
utilities read commands from the standard input or a specified file
which are to be executed at a later time, using
.Xr sh 1 .
.Pp
The functions of the commands are as follows:
.Bl -tag -width indent
.It Nm at
Executes commands at a specified time.
.It Nm atq
Lists the user's pending jobs, unless the user is
the superuser. In that case, everybody's jobs are
listed.
.It Nm atrm
Deletes jobs.
.It Nm batch
executes commands when system load levels permit.
In other words, it executes the commands when the load
average drops below a specified level.
.El
.Pp
For both
.Nm at
and
.Nm batch ,
the working directory, environment (except for the variables
.Nm TERM ,
.Nm TERMCAP ,
.Nm DISPLAY ,
and
.Nm _ )
and the umask are retained from the time of invocation. The user
will be mailed the standard output and standard error from
his commands if any output is generated. If
.Nm at
is executed from a
.Xr su 1
shell, the owner of the login whell will receive the mail.
.Sh OPTIONS
.Bl -tag -width indent
The available options are as follows:
.It Fl q Ar queue
Use the specified queue. A queue designation consists
of a single letter; valid queue designation range from
.Ar a
to
.Ar l .
The
.Ar a
queue is the default, and
.Ar b
is the batch queue. Queues with higher letters run with
increased niceness. If
.Nm atq
is given a specific queue, it will only show jobs pending
in that queue.
.It Fl m
Send mail to the user when the job has completed, even if
there was no output.
.It Fl f Ar file
Reads the job from
.Ar file
rather than the standard input.
.It Fl v
Shows completed but not yet deleted jobs in the queue.
.Sh TIME SPECIFICATION
.Nm At
allows some moderately complex time specifications.
It accepts times of the form
.Ar HHMM
or
.Ar HH:MM
to run a job at a specific time of day. If
that time is already passed, the next day is assumed.
You may also specify
.Nm midnight ,
.Nm noon ,
or
.Nm teatime
(4PM) and you can give a time of day suffixed with
.Nm AM
or
.Nm PM
for running in the morning or the evening. You can
also specify the date on which the job will be run
by giving a date in the form
.Ar month-name day
with an optional
.Ar year ,
or giving a date of the form
.Ar MMDDYY ,
.Ar MM/DD/YY
or
.Ar DD.MM.YY .
You can also give times like
.Nm now +
.Ar count time-units ,
where the time units can be
.Nm minutes, hours, days,
or
.Nm weeks
You can suffix the time with
.Nm today
to run the job today, or
.Nm tomorrow
to run the job tomorrow.
.Pp
For example, to run a job at 4PM three days from now, you
would specify a time of
.Nm 4PM + 3 days .
To run a job at 10:00AM on on July 31, you would specify
a time of
.Nm 10AM Jul 31 .
Finally, to run a job at 1AM tomorrow, you would specify
a time of
.Nm 1AM tomorrow .
.Sh FILES
.Bl -tag -width /var/at/lockfile -compact
.It Pa /var/at/jobs
Directory containing job files
.It Pa /var/at/spool
Directory containing output spool files
.It Pa /var/at/lockfile
Job-creation lock file.
.It Pa /var/run/utmp
.El
.Sh SEE ALSO
.Xr crond 8 ,
.Xr nice 1 ,
.Xr sh 1 ,
.Xr atrun 8
.Sh AUTHOR
.Bl -tag
Thomas Koenig, ig25@rz.uni-karlsruhe.de
.El
.Sh BUGS
Traditional access control to
.Nm at
and
.Nm batch
via the files
.Pa /var/at/at.allow
and
.Pa /var/at/at.deny
is not implemented.
.Pp
If the file
.Pa /var/run/utmp
is not available or corrupted, or if the user is not
logged in at the time
.Nm at
is invoked, the mail is sent to the userid found in the
environment variable
.Nm LOGNAME .
If that is undefined or empty, the current userid is assumed.

562
usr.bin/at/at.c Normal file
View File

@ -0,0 +1,562 @@
/*
* at.c : Put file into atrun queue
* Copyright (C) 1993 Thomas Koenig
*
* Atrun & Atq modifications
* Copyright (C) 1993 David Parsons
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _USE_BSD 1
/* System Headers */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
#include <signal.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
/* Local headers */
#include "at.h"
#include "panic.h"
#include "parsetime.h"
#include "pathnames.h"
#define MAIN
#include "privs.h"
/* Macros */
#define ALARMC 10 /* Number of seconds to wait for timeout */
#define SIZE 255
#define TIMESIZE 50
/* File scope variables */
static char rcsid[] = "$Id: at.c,v 1.2 1993/12/06 04:10:42 cgd Exp $";
char *no_export[] =
{
"TERM", "TERMCAP", "DISPLAY", "_"
};
static send_mail = 0;
/* External variables */
extern char **environ;
int fcreated;
char *namep;
char atfile[FILENAME_MAX];
char *atinput = (char *) 0; /* where to get input from */
char atqueue = 0; /* which queue to examine for jobs (atq) */
char atverify = 0; /* verify time instead of queuing job */
/* Function declarations */
static void sigc __P((int signo));
static void alarmc __P((int signo));
static char *cwdname __P((void));
static void writefile __P((time_t runtimer, char queue));
static void list_jobs __P((void));
/* Signal catching functions */
static void
sigc(signo)
int signo;
{
/* If the user presses ^C, remove the spool file and exit
*/
if (fcreated) {
PRIV_START
unlink(atfile);
PRIV_END
}
exit(EXIT_FAILURE);
}
static void
alarmc(signo)
int signo;
{
/* Time out after some seconds
*/
panic("File locking timed out");
}
/* Local functions */
static char *
cwdname()
{
/* Read in the current directory; the name will be overwritten on
* subsequent calls.
*/
static char *ptr = NULL;
static size_t size = SIZE;
if (ptr == NULL)
ptr = (char *) malloc(size);
while (1) {
if (ptr == NULL)
panic("Out of memory");
if (getcwd(ptr, size - 1) != NULL)
return ptr;
if (errno != ERANGE)
perr("Cannot get directory");
free(ptr);
size += SIZE;
ptr = (char *) malloc(size);
}
}
static void
writefile(runtimer, queue)
time_t runtimer;
char queue;
{
/*
* This does most of the work if at or batch are invoked for
* writing a job.
*/
int i;
char *ap, *ppos, *mailname;
struct passwd *pass_entry;
struct stat statbuf;
int fdes, lockdes, fd2;
FILE *fp, *fpin;
struct sigaction act;
char **atenv;
int ch;
mode_t cmask;
struct flock lock;
/*
* Install the signal handler for SIGINT; terminate after removing the
* spool file if necessary
*/
act.sa_handler = sigc;
sigemptyset(&(act.sa_mask));
act.sa_flags = 0;
sigaction(SIGINT, &act, NULL);
strcpy(atfile, _PATH_ATJOBS);
ppos = atfile + strlen(_PATH_ATJOBS);
/*
* Loop over all possible file names for running something at this
* particular time, see if a file is there; the first empty slot at
* any particular time is used. Lock the file _PATH_LOCKFILE first
* to make sure we're alone when doing this.
*/
PRIV_START
if ((lockdes = open(_PATH_LOCKFILE, O_WRONLY | O_CREAT, 0600)) < 0)
perr2("Cannot open lockfile ", _PATH_LOCKFILE);
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
act.sa_handler = alarmc;
sigemptyset(&(act.sa_mask));
act.sa_flags = 0;
/*
* Set an alarm so a timeout occurs after ALARMC seconds, in case
* something is seriously broken.
*/
sigaction(SIGALRM, &act, NULL);
alarm(ALARMC);
fcntl(lockdes, F_SETLKW, &lock);
alarm(0);
for (i = 0; i < AT_MAXJOBS; i++) {
sprintf(ppos, "%c%8lx.%3x", queue,
(unsigned long) (runtimer / 60), i);
for (ap = ppos; *ap != '\0'; ap++)
if (*ap == ' ')
*ap = '0';
if (stat(atfile, &statbuf) != 0) {
if (errno == ENOENT)
break;
else
perr2("Cannot access ", _PATH_ATJOBS);
}
} /* for */
if (i >= AT_MAXJOBS)
panic("Too many jobs already");
/*
* Create the file. The x bit is only going to be set after it has
* been completely written out, to make sure it is not executed in
* the meantime. To make sure they do not get deleted, turn off
* their r bit. Yes, this is a kluge.
*/
cmask = umask(S_IRUSR | S_IWUSR | S_IXUSR);
if ((fdes = creat(atfile, O_WRONLY)) == -1)
perr("Cannot create atjob file");
if ((fd2 = dup(fdes)) < 0)
perr("Error in dup() of job file");
if (fchown(fd2, real_uid, -1) != 0)
perr("Cannot give away file");
PRIV_END
/*
* We no longer need suid root; now we just need to be able to
* write to the directory, if necessary.
*/
REDUCE_PRIV(0);
/*
* We've successfully created the file; let's set the flag so it
* gets removed in case of an interrupt or error.
*/
fcreated = 1;
/* Now we can release the lock, so other people can access it */
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
fcntl(lockdes, F_SETLKW, &lock);
close(lockdes);
if ((fp = fdopen(fdes, "w")) == NULL)
panic("Cannot reopen atjob file");
/*
* Get the userid to mail to, first by trying getlogin(), which
* reads /etc/utmp, then from LOGNAME, finally from getpwuid().
*/
mailname = getlogin();
if (mailname == NULL)
mailname = getenv("LOGNAME");
if ((mailname == NULL) || (mailname[0] == '\0')
|| (strlen(mailname) > 8)) {
pass_entry = getpwuid(getuid());
if (pass_entry != NULL)
mailname = pass_entry->pw_name;
}
if (atinput != (char *) NULL) {
fpin = freopen(atinput, "r", stdin);
if (fpin == NULL)
perr("Cannot open input file");
}
fprintf(fp, "#! /bin/sh\n# mail %8s %d\n", mailname, send_mail);
/* Write out the umask at the time of invocation */
fprintf(fp, "umask %lo\n", (unsigned long) cmask);
/*
* Write out the environment. Anything that may look like a special
* character to the shell is quoted, except for \n, which is done
* with a pair of "'s. Dont't export the no_export list (such as
* TERM or DISPLAY) because we don't want these.
*/
for (atenv = environ; *atenv != NULL; atenv++) {
int export = 1;
char *eqp;
eqp = strchr(*atenv, '=');
if (ap == NULL)
eqp = *atenv;
else {
int i;
for (i = 0;i < sizeof(no_export) /
sizeof(no_export[0]); i++) {
export = export
&& (strncmp(*atenv, no_export[i],
(size_t) (eqp - *atenv)) != 0);
}
eqp++;
}
if (export) {
fwrite(*atenv, sizeof(char), eqp - *atenv, fp);
for (ap = eqp; *ap != '\0'; ap++) {
if (*ap == '\n')
fprintf(fp, "\"\n\"");
else {
if (!isalnum(*ap))
fputc('\\', fp);
fputc(*ap, fp);
}
}
fputs("; export ", fp);
fwrite(*atenv, sizeof(char), eqp - *atenv - 1, fp);
fputc('\n', fp);
}
}
/*
* Cd to the directory at the time and write out all the commands
* the user supplies from stdin.
*/
fprintf(fp, "cd %s\n", cwdname());
while ((ch = getchar()) != EOF)
fputc(ch, fp);
fprintf(fp, "\n");
if (ferror(fp))
panic("Output error");
if (ferror(stdin))
panic("Input error");
fclose(fp);
/*
* Set the x bit so that we're ready to start executing
*/
if (fchmod(fd2, S_IRUSR | S_IWUSR | S_IXUSR) < 0)
perr("Cannot give away file");
close(fd2);
fprintf(stderr, "Job %s will be executed using /bin/sh\n", ppos);
}
static void
list_jobs()
{
/*
* List all a user's jobs in the queue, by looping through
* _PATH_ATJOBS, or everybody's if we are root
*/
struct passwd *pw;
DIR *spool;
struct dirent *dirent;
struct stat buf;
struct tm runtime;
unsigned long ctm;
char queue;
time_t runtimer;
char timestr[TIMESIZE];
int first = 1;
PRIV_START
if (chdir(_PATH_ATJOBS) != 0)
perr2("Cannot change to ", _PATH_ATJOBS);
if ((spool = opendir(".")) == NULL)
perr2("Cannot open ", _PATH_ATJOBS);
/* Loop over every file in the directory */
while ((dirent = readdir(spool)) != NULL) {
if (stat(dirent->d_name, &buf) != 0)
perr2("Cannot stat in ", _PATH_ATJOBS);
/*
* See it's a regular file and has its x bit turned on and
* is the user's
*/
if (!S_ISREG(buf.st_mode)
|| ((buf.st_uid != real_uid) && !(real_uid == 0))
|| !(S_IXUSR & buf.st_mode || atverify))
continue;
if (sscanf(dirent->d_name, "%c%8lx", &queue, &ctm) != 2)
continue;
if (atqueue && (queue != atqueue))
continue;
runtimer = 60 * (time_t) ctm;
runtime = *localtime(&runtimer);
strftime(timestr, TIMESIZE, "%X %x", &runtime);
if (first) {
printf("Date\t\t\tOwner\tQueue\tJob#\n");
first = 0;
}
pw = getpwuid(buf.st_uid);
printf("%s\t%s\t%c%s\t%s\n",
timestr,
pw ? pw->pw_name : "???",
queue,
(S_IXUSR & buf.st_mode) ? "" : "(done)",
dirent->d_name);
}
PRIV_END
}
static void
delete_jobs(argc, argv)
int argc;
char **argv;
{
/* Delete every argument (job - ID) given */
int i;
struct stat buf;
PRIV_START
if (chdir(_PATH_ATJOBS) != 0)
perr2("Cannot change to ", _PATH_ATJOBS);
for (i = optind; i < argc; i++) {
if (stat(argv[i], &buf) != 0)
perr(argv[i]);
if ((buf.st_uid != real_uid) && !(real_uid == 0)) {
fprintf(stderr, "%s: Not owner\n", argv[i]);
exit(EXIT_FAILURE);
}
if (unlink(argv[i]) != 0)
perr(argv[i]);
}
PRIV_END
} /* delete_jobs */
/* Global functions */
int
main(argc, argv)
int argc;
char **argv;
{
int c;
char queue = 'a';
char *pgm;
enum {
ATQ, ATRM, AT, BATCH
}; /* what program we want to run */
int program = AT; /* our default program */
char *options = "q:f:mv"; /* default options for at */
time_t timer;
RELINQUISH_PRIVS
/* Eat any leading paths */
if ((pgm = strrchr(argv[0], '/')) == NULL)
pgm = argv[0];
else
pgm++;
namep = pgm;
/* find out what this program is supposed to do */
if (strcmp(pgm, "atq") == 0) {
program = ATQ;
options = "q:v";
} else if (strcmp(pgm, "atrm") == 0) {
program = ATRM;
options = "";
} else if (strcmp(pgm, "batch") == 0) {
program = BATCH;
options = "f:mv";
}
/* process whatever options we can process */
opterr = 1;
while ((c = getopt(argc, argv, options)) != EOF)
switch (c) {
case 'v': /* verify time settings */
atverify = 1;
break;
case 'm': /* send mail when job is complete */
send_mail = 1;
break;
case 'f':
atinput = optarg;
break;
case 'q': /* specify queue */
if (strlen(optarg) > 1)
usage();
atqueue = queue = *optarg;
if ((!islower(queue)) || (queue > 'l'))
usage();
break;
default:
usage();
break;
}
/* end of options eating */
/* select our program */
switch (program) {
case ATQ:
REDUCE_PRIV(0);
list_jobs();
break;
case ATRM:
REDUCE_PRIV(0);
delete_jobs(argc, argv);
break;
case AT:
timer = parsetime(argc, argv);
if (atverify) {
struct tm *tm = localtime(&timer);
fprintf(stderr, "%s\n", asctime(tm));
}
writefile(timer, queue);
break;
case BATCH:
writefile(time(NULL), 'b');
break;
default:
panic("Internal error");
break;
}
exit(EXIT_SUCCESS);
}

34
usr.bin/at/at.h Normal file
View File

@ -0,0 +1,34 @@
/*
* at.h - header for at(1)
* Copyright (c) 1993 by Thomas Koenig
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: at.h,v 1.1 1993/12/05 11:36:45 cgd Exp $
*/
extern int fcreated;
extern char *namep;
extern char atfile[];
extern char atverify;
#define AT_MAXJOBS 255 /* max jobs outstanding per user */

91
usr.bin/at/panic.c Normal file
View File

@ -0,0 +1,91 @@
/*
* panic.c - terminate fast in case of error
* Copyright (c) 1993 by Thomas Koenig
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* System Headers */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* Local headers */
#include "panic.h"
#include "at.h"
/* File scope variables */
static char rcsid[] = "$Id: panic.c,v 1.1 1993/12/05 11:36:51 cgd Exp $";
/* External variables */
/* Global functions */
void
panic(a)
char *a;
{
/* Something fatal has happened, print error message and exit.
*/
fprintf(stderr, "%s: %s\n", namep, a);
if (fcreated)
unlink(atfile);
exit(EXIT_FAILURE);
}
void
perr(a)
char *a;
{
/* Some operating system error; print error message and exit.
*/
perror(a);
if (fcreated)
unlink(atfile);
exit(EXIT_FAILURE);
}
void
perr2(a, b)
char *a, *b;
{
fprintf(stderr, "%s", a);
perr(b);
}
void
usage(void)
{
/* Print usage and exit.
*/
fprintf(stderr, "Usage: at [-q x] [-f file] [-m] time\n"
" atq [-q x] [-v]\n"
" atrm [-q x] job ...\n"
" batch [-f file] [-m]\n");
exit(EXIT_FAILURE);
}

32
usr.bin/at/panic.h Normal file
View File

@ -0,0 +1,32 @@
/*
* panic.h - header for at(1)
* Copyright (c) 1993 Thomas Koenig
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: panic.h,v 1.1 1993/12/05 11:36:58 cgd Exp $
*/
void panic __P((char *a));
void perr __P((char *a));
void perr2 __P((char *a, char *b));
void usage __P((void));

591
usr.bin/at/parsetime.c Normal file
View File

@ -0,0 +1,591 @@
/*
* parsetime.c - parse time for at(1)
* Copyright (C) 1993 Thomas Koenig
*
* modifications for english-language times
* Copyright (C) 1993 David Parsons
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* at [NOW] PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS
* /NUMBER [DOT NUMBER] [AM|PM]\ /[MONTH NUMBER [NUMBER]] \
* |NOON | |[TOMORROW] |
* |MIDNIGHT | |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
* \TEATIME / \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
*/
/* System Headers */
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>
/* Local headers */
#include "at.h"
#include "panic.h"
/* Structures and unions */
enum { /* symbols */
MIDNIGHT, NOON, TEATIME,
PM, AM, TOMORROW, TODAY, NOW,
MINUTES, HOURS, DAYS, WEEKS,
NUMBER, PLUS, DOT, SLASH, ID, JUNK,
JAN, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC
};
/*
* parse translation table - table driven parsers can be your FRIEND!
*/
struct {
char *name; /* token name */
int value; /* token id */
} Specials[] = {
{ "midnight", MIDNIGHT }, /* 00:00:00 of today or tomorrow */
{ "noon", NOON }, /* 12:00:00 of today or tomorrow */
{ "teatime", TEATIME }, /* 16:00:00 of today or tomorrow */
{ "am", AM }, /* morning times for 0-12 clock */
{ "pm", PM }, /* evening times for 0-12 clock */
{ "tomorrow", TOMORROW }, /* execute 24 hours from time */
{ "today", TODAY }, /* execute today - don't advance time */
{ "now", NOW }, /* opt prefix for PLUS */
{ "minute", MINUTES }, /* minutes multiplier */
{ "min", MINUTES },
{ "m", MINUTES },
{ "minutes", MINUTES }, /* (pluralized) */
{ "hour", HOURS }, /* hours ... */
{ "hr", HOURS }, /* abbreviated */
{ "h", HOURS },
{ "hours", HOURS }, /* (pluralized) */
{ "day", DAYS }, /* days ... */
{ "d", DAYS },
{ "days", DAYS }, /* (pluralized) */
{ "week", WEEKS }, /* week ... */
{ "w", WEEKS },
{ "weeks", WEEKS }, /* (pluralized) */
{ "jan", JAN },
{ "feb", FEB },
{ "mar", MAR },
{ "apr", APR },
{ "may", MAY },
{ "jun", JUN },
{ "jul", JUL },
{ "aug", AUG },
{ "sep", SEP },
{ "oct", OCT },
{ "nov", NOV },
{ "dec", DEC }
} ;
/* File scope variables */
static char **scp; /* scanner - pointer at arglist */
static char scc; /* scanner - count of remaining arguments */
static char *sct; /* scanner - next char pointer in current argument */
static int need; /* scanner - need to advance to next argument */
static char *sc_token; /* scanner - token buffer */
static size_t sc_len; /* scanner - lenght of token buffer */
static int sc_tokid; /* scanner - token id */
static char rcsid[] = "$Id: parsetime.c,v 1.1 1993/12/05 11:37:05 cgd Exp $";
/* Local functions */
/*
* parse a token, checking if it's something special to us
*/
static int
parse_token(arg)
char *arg;
{
int i;
for (i=0; i<(sizeof Specials/sizeof Specials[0]); i++)
if (strcasecmp(Specials[i].name, arg) == 0) {
return sc_tokid = Specials[i].value;
}
/* not special - must be some random id */
return ID;
} /* parse_token */
/*
* init_scanner() sets up the scanner to eat arguments
*/
static void
init_scanner(argc, argv)
int argc;
char **argv;
{
scp = argv;
scc = argc;
need = 1;
sc_len = 1;
while (--argc > 0)
sc_len += strlen(*++argv);
sc_token = (char *) malloc(sc_len);
if (sc_token == NULL)
panic("Insufficient virtual memory");
} /* init_scanner */
/*
* token() fetches a token from the input stream
*/
static int
token()
{
int idx;
while (1) {
memset(sc_token, 0, sc_len);
sc_tokid = EOF;
idx = 0;
/*
* if we need to read another argument, walk along the argument list;
* when we fall off the arglist, we'll just return EOF forever
*/
if (need) {
if (scc < 1)
return sc_tokid;
sct = *scp;
scp++;
scc--;
need = 0;
}
/*
* eat whitespace now - if we walk off the end of the argument,
* we'll continue, which puts us up at the top of the while loop
* to fetch the next argument in
*/
while (isspace(*sct))
++sct;
if (!*sct) {
need = 1;
continue;
}
/*
* preserve the first character of the new token
*/
sc_token[0] = *sct++;
/*
* then see what it is
*/
if (isdigit(sc_token[0])) {
while (isdigit(*sct))
sc_token[++idx] = *sct++;
sc_token[++idx] = 0;
return sc_tokid = NUMBER;
} else if (isalpha(sc_token[0])) {
while (isalpha(*sct))
sc_token[++idx] = *sct++;
sc_token[++idx] = 0;
return parse_token(sc_token);
}
else if (sc_token[0] == ':' || sc_token[0] == '.')
return sc_tokid = DOT;
else if (sc_token[0] == '+')
return sc_tokid = PLUS;
else if (*sct == '/')
return sc_tokid = SLASH;
else
return sc_tokid = JUNK;
} /* while (1) */
} /* token */
/*
* plonk() gives an appropriate error message if a token is incorrect
*/
static void
plonk(tok)
int tok;
{
panic((tok == EOF) ? "incomplete time"
: "garbled time");
} /* plonk */
/*
* expect() gets a token and dies most horribly if it's not the token we want
*/
static void
expect(desired)
int desired;
{
if (token() != desired)
plonk(sc_tokid); /* and we die here... */
} /* expect */
/*
* dateadd() adds a number of minutes to a date. It is extraordinarily
* stupid regarding day-of-month overflow, and will most likely not
* work properly
*/
static void
dateadd(minutes, tm)
int minutes;
struct tm *tm;
{
/* increment days */
while (minutes > 24*60) {
minutes -= 24*60;
tm->tm_mday++;
}
/* increment hours */
while (minutes > 60) {
minutes -= 60;
tm->tm_hour++;
if (tm->tm_hour > 23) {
tm->tm_mday++;
tm->tm_hour = 0;
}
}
/* increment minutes */
tm->tm_min += minutes;
if (tm->tm_min > 59) {
tm->tm_hour++;
tm->tm_min -= 60;
if (tm->tm_hour > 23) {
tm->tm_mday++;
tm->tm_hour = 0;
}
}
} /* dateadd */
/*
* plus() parses a now + time
*
* at [NOW] PLUS NUMBER [MINUTES|HOURS|DAYS|WEEKS]
*
*/
static void
plus(tm)
struct tm *tm;
{
int delay;
expect(NUMBER);
delay = atoi(sc_token);
switch (token()) {
case WEEKS:
delay *= 7;
case DAYS:
delay *= 24;
case HOURS:
delay *= 60;
case MINUTES:
dateadd(delay, tm);
return;
}
plonk(sc_tokid);
} /* plus */
/*
* tod() computes the time of day
* [NUMBER [DOT NUMBER] [AM|PM]]
*/
static void
tod(tm)
struct tm *tm;
{
int hour, minute = 0;
int tlen;
hour = atoi(sc_token);
tlen = strlen(sc_token);
/*
* first pick out the time of day - if it's 4 digits, we assume
* a HHMM time, otherwise it's HH DOT MM time
*/
if (token() == DOT) {
expect(NUMBER);
minute = atoi(sc_token);
if (minute > 59)
panic("garbled time");
token();
} else if (tlen == 4) {
minute = hour%100;
if (minute > 59)
panic("garbeld time");
hour = hour/100;
}
/*
* check if an AM or PM specifier was given
*/
if (sc_tokid == AM || sc_tokid == PM) {
if (hour > 12)
panic("garbled time");
if (sc_tokid == PM)
hour += 12;
token();
} else if (hour > 23)
panic("garbled time");
/*
* if we specify an absolute time, we don't want to bump the day even
* if we've gone past that time - but if we're specifying a time plus
* a relative offset, it's okay to bump things
*/
if ((sc_tokid == EOF || sc_tokid == PLUS) && tm->tm_hour > hour)
tm->tm_mday++;
tm->tm_hour = hour;
tm->tm_min = minute;
if (tm->tm_hour == 24) {
tm->tm_hour = 0;
tm->tm_mday++;
}
} /* tod */
/*
* assign_date() assigns a date, wrapping to next year if needed
*/
static void
assign_date(tm, mday, mon, year)
struct tm *tm;
long mday, mon, year;
{
if (year > 99) {
if (year > 1899)
year -= 1900;
else
panic("garbled time");
}
if (year < 0 &&
(tm->tm_mon > mon ||(tm->tm_mon == mon && tm->tm_mday > mday)))
year = tm->tm_year + 1;
tm->tm_mday = mday;
tm->tm_mon = mon;
if (year >= 0)
tm->tm_year = year;
} /* assign_date */
/*
* month() picks apart a month specification
*
* /[<month> NUMBER [NUMBER]] \
* |[TOMORROW] |
* |NUMBER [SLASH NUMBER [SLASH NUMBER]]|
* \PLUS NUMBER MINUTES|HOURS|DAYS|WEEKS/
*/
static void
month(tm)
struct tm *tm;
{
long year= (-1);
long mday, mon;
int tlen;
switch (sc_tokid) {
case PLUS:
plus(tm);
break;
case TOMORROW:
/* do something tomorrow */
tm->tm_mday ++;
case TODAY: /* force ourselves to stay in today - no further processing */
token();
break;
case JAN: case FEB: case MAR: case APR: case MAY: case JUN:
case JUL: case AUG: case SEP: case OCT: case NOV: case DEC:
/*
* do month mday [year]
*/
mon = (sc_tokid-JAN);
expect(NUMBER);
mday = atol(sc_token)-1;
if (token() == NUMBER) {
year = atol(sc_token);
token();
}
assign_date(tm, mday, mon, year);
break;
case NUMBER:
/*
* get numeric MMDDYY, mm/dd/yy, or dd.mm.yy
*/
tlen = strlen(sc_token);
mon = atol(sc_token);
token();
if (sc_tokid == SLASH || sc_tokid == DOT) {
int sep;
sep = sc_tokid;
expect(NUMBER);
mday = atol(sc_token);
if (token() == sep) {
expect(NUMBER);
year = atol(sc_token);
token();
}
/*
* flip months and days for european timing
*/
if (sep == DOT) {
int x = mday;
mday = mon;
mon = x;
}
} else if (tlen == 6 || tlen == 8) {
if (tlen == 8) {
year = (mon % 10000) - 1900;
mon /= 10000;
} else {
year = mon % 100;
mon /= 100;
}
mday = mon % 100;
mon /= 100;
} else
panic("garbled time");
mon--;
if (mon < 0 || mon > 11 || mday < 1 || mday > 31)
panic("garbled time");
assign_date(tm, mday, mon, year);
break;
} /* case */
} /* month */
/* Global functions */
time_t
parsetime(argc, argv)
int argc;
char **argv;
{
/*
* Do the argument parsing, die if necessary, and return the time the job
* should be run.
*/
time_t nowtimer, runtimer;
struct tm nowtime, runtime;
int hr = 0;
/* this MUST be initialized to zero for midnight/noon/teatime */
nowtimer = time(NULL);
nowtime = *localtime(&nowtimer);
runtime = nowtime;
runtime.tm_sec = 0;
runtime.tm_isdst = 0;
if (argc <= optind)
usage();
init_scanner(argc-optind, argv+optind);
switch (token()) {
case NOW: /* now is optional prefix for PLUS tree */
expect(PLUS);
case PLUS:
plus(&runtime);
break;
case NUMBER:
tod(&runtime);
month(&runtime);
break;
/*
* evil coding for TEATIME|NOON|MIDNIGHT - we've initialised
* hr to zero up above, then fall into this case in such a
* way so we add +12 +4 hours to it for teatime, +12 hours
* to it for noon, and nothing at all for midnight, then
* set our runtime to that hour before leaping into the
* month scanner
*/
case TEATIME:
hr += 4;
case NOON:
hr += 12;
case MIDNIGHT:
if (runtime.tm_hour >= hr)
runtime.tm_mday++;
runtime.tm_hour = hr;
runtime.tm_min = 0;
token();
/* fall through to month setting */
default:
month(&runtime);
break;
} /* ugly case statement */
expect(EOF);
/*
* adjust for daylight savings time
*/
runtime.tm_isdst = -1;
runtimer = mktime(&runtime);
if (runtime.tm_isdst > 0) {
runtimer -= 3600;
runtimer = mktime(&runtime);
}
if (runtimer < 0)
panic("garbled time");
if (nowtimer > runtimer)
panic("Trying to travel back in time");
return runtimer;
} /* parsetime */

29
usr.bin/at/parsetime.h Normal file
View File

@ -0,0 +1,29 @@
/*
* at.h - header for at(1)
* Copyright (c) 1993 by Thomas Koenig
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: parsetime.h,v 1.1 1993/12/05 11:37:17 cgd Exp $
*/
time_t parsetime __P((int argc, char **argv));

42
usr.bin/at/pathnames.h Normal file
View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 1993 Christopher G. Demetriou
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Christopher G. Demetriou.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software withough specific prior written permission
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: pathnames.h,v 1.1 1993/12/05 11:37:23 cgd Exp $
*/
#ifndef _PATHNAMES_H_
#define _PATHNAMES_H_
#include <paths.h>
#define _PATH_ATJOBS "/var/at/jobs/"
#define _PATH_ATSPOOL "/var/at/spool/"
#define _PATH_LOCKFILE "/var/at/lockfile"
#endif /* !_PATHNAMES_H_ */

92
usr.bin/at/privs.h Normal file
View File

@ -0,0 +1,92 @@
/*
* privs.h - header for privileged operations
* Copyright (c) 1993 by Thomas Koenig
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author(s) may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: privs.h,v 1.1 1993/12/05 11:37:29 cgd Exp $
*/
#ifndef _PRIVS_H
#define _PRIVS_H
#include <unistd.h>
/* Relinquish privileges temporarily for a setuid program
* with the option of getting them back later. This is done by swapping
* the real and effective userid BSD style. Call RELINQUISH_PRIVS once
* at the beginning of the main program. This will cause all operatons
* to be executed with the real userid. When you need the privileges
* of the setuid invocation, call PRIV_START; when you no longer
* need it, call PRIV_END. Note that it is an error to call PRIV_START
* and not PRIV_END within the same function.
*
* Use RELINQUISH_PRIVS_ROOT(a) if your program started out running
* as root, and you want to drop back the effective userid to a
* and the effective group id to b, with the option to get them back
* later.
*
* If you no longer need root privileges, but those of some other
* userid/groupid, you can call REDUCE_PRIV(a) when your effective
* is the user's.
*
* Problems: Do not use return between PRIV_START and PRIV_END; this
* will cause the program to continue running in an unprivileged
* state.
*
* It is NOT safe to call exec(), system() or popen() with a user-
* supplied program (i.e. without carefully checking PATH and any
* library load paths) with relinquished privileges; the called program
* can aquire them just as easily. Set both effective and real userid
* to the real userid before calling any of them.
*/
#ifndef MAIN
extern
#endif
uid_t real_uid, effective_uid;
#define RELINQUISH_PRIVS { \
real_uid = getuid(); \
effective_uid = geteuid(); \
setreuid(effective_uid,real_uid); \
}
#define RELINQUISH_PRIVS_ROOT(a) { \
real_uid = (a); \
effective_uid = geteuid(); \
setreuid(effective_uid,real_uid); \
}
#define PRIV_START { \
setreuid(real_uid,effective_uid);
#define PRIV_END \
setreuid(effective_uid,real_uid); \
}
#define REDUCE_PRIV(a) { \
setreuid(real_uid,effective_uid); \
effective_uid = (a); \
setreuid(effective_uid,real_uid); \
}
#endif