]>
git.saurik.com Git - apple/system_cmds.git/blob - atrun.tproj/atrun.c
2 * atrun.c - run jobs queued by at; run with root privileges.
3 * Copyright (C) 1993, 1994 Thomas Koenig
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. The name of the author(s) may not be used to endorse or promote
11 * products derived from this software without specific prior written
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 static const char rcsid
[] =
28 "$FreeBSD: src/libexec/atrun/atrun.c,v 1.18 2004/07/11 17:37:32 stefanf Exp $";
33 #include <sys/fcntl.h>
34 #include <sys/types.h>
37 #include <sys/param.h>
58 #if (MAXLOGNAME-1) > UT_NAMESIZE
59 #define LOGNAMESIZE UT_NAMESIZE
61 #define LOGNAMESIZE (MAXLOGNAME-1)
68 #include "pathnames.h"
72 #define ATJOB_DIR _PATH_ATJOBS
76 #define ATSPOOL_DIR _PATH_ATSPOOL
80 #define LOADAVG_MX 1.5
83 /* File scope variables */
87 void perr(const char *a
);
88 static void usage(void);
92 write_string(int fd
, const char* a
)
94 return write(fd
, a
, strlen(a
));
105 kill(getpid(),SIGSTOP
);
113 run_file(const char *filename
, uid_t uid
, gid_t gid
)
115 /* Run a file by spawning off a process which redirects I/O,
116 * spawns a subshell, then waits for it to complete and sends
122 char mailbuf
[LOGNAMESIZE
+ 1], fmt
[49];
123 char *mailname
= NULL
;
126 struct stat buf
, lbuf
;
128 struct passwd
*pentry
;
136 if (chmod(filename
, S_IRUSR
) != 0)
138 perr("cannot change file permissions");
150 /* Let's see who we mail to. Hopefully, we can read it from
151 * the command file; if not, send it to the owner, or, failing that,
155 pentry
= getpwuid(uid
);
158 syslog(LOG_ERR
,"Userid %lu not found - aborting job %s",
159 (unsigned long) uid
, filename
);
164 stream
=fopen(filename
, "r");
169 if (pentry
->pw_expire
&& time(NULL
) >= pentry
->pw_expire
)
171 syslog(LOG_ERR
, "Userid %lu is expired - aborting job %s",
172 (unsigned long) uid
, filename
);
178 perr("cannot open input file");
180 if ((fd_in
= dup(fileno(stream
))) <0)
181 perr("error duplicating input file descriptor");
183 if (fstat(fd_in
, &buf
) == -1)
184 perr("error in fstat of input file descriptor");
186 if (lstat(filename
, &lbuf
) == -1)
187 perr("error in fstat of input file");
189 if (S_ISLNK(lbuf
.st_mode
)) {
190 syslog(LOG_ERR
,"Symbolic link encountered in job %s - aborting",
194 if ((lbuf
.st_dev
!= buf
.st_dev
) || (lbuf
.st_ino
!= buf
.st_ino
) ||
195 (lbuf
.st_uid
!= buf
.st_uid
) || (lbuf
.st_gid
!= buf
.st_gid
) ||
196 (lbuf
.st_size
!=buf
.st_size
)) {
197 syslog(LOG_ERR
,"Somebody changed files from under us for job %s - "
198 "aborting",filename
);
201 if (buf
.st_nlink
> 1) {
202 syslog(LOG_ERR
,"Someboy is trying to run a linked script for job %s",
206 if ((fflags
= fcntl(fd_in
, F_GETFD
)) <0)
207 perr("error in fcntl");
209 fcntl(fd_in
, F_SETFD
, fflags
& ~FD_CLOEXEC
);
211 snprintf(fmt
, sizeof(fmt
),
212 "#!/bin/sh\n# atrun uid=%%ld gid=%%ld\n# mail %%%ds %%d",
214 if (fscanf(stream
, fmt
, &nuid
, &ngid
, mailbuf
, &send_mail
) != 4) {
215 syslog(LOG_ERR
,"File %s is in wrong format - aborting", filename
);
218 if (mailbuf
[0] == '-') {
219 syslog(LOG_ERR
,"illegal mail name %s in %s",mailbuf
,filename
);
224 syslog(LOG_ERR
,"Job %s - userid %ld does not match file uid %lu",
225 filename
, nuid
, (unsigned long)uid
);
229 syslog(LOG_ERR
,"Job %s - groupid %ld does not match file gid %lu",
230 filename
, ngid
, (unsigned long)gid
);
234 if (chdir(ATSPOOL_DIR
) < 0)
235 perr("cannot chdir to " ATSPOOL_DIR
);
237 /* Create a file to hold the output of the job we are about to run.
238 * Write the mail header.
240 if((fd_out
=open(filename
,
241 O_WRONLY
| O_CREAT
| O_EXCL
, S_IWUSR
| S_IRUSR
)) < 0)
242 perr("cannot create output file");
244 write_string(fd_out
, "Subject: Output from your job ");
245 write_string(fd_out
, filename
);
246 write_string(fd_out
, "\n\n");
251 close(STDOUT_FILENO
);
252 close(STDERR_FILENO
);
256 perr("error in fork");
263 /* Set up things for the child; we want standard input from the input file,
264 * and standard output and error sent to our output file.
267 if (lseek(fd_in
, (off_t
) 0, SEEK_SET
) < 0)
268 perr("error in lseek");
270 if (dup(fd_in
) != STDIN_FILENO
)
271 perr("error in I/O redirection");
273 if (dup(fd_out
) != STDOUT_FILENO
)
274 perr("error in I/O redirection");
276 if (dup(fd_out
) != STDERR_FILENO
)
277 perr("error in I/O redirection");
281 if (chdir(ATJOB_DIR
) < 0)
282 perr("cannot chdir to " ATJOB_DIR
);
288 nice(tolower(queue
) - 'a');
290 if (initgroups(pentry
->pw_name
,pentry
->pw_gid
))
291 perr("cannot delete saved userids");
293 if (setgid(gid
) < 0 || setegid(pentry
->pw_gid
) < 0)
294 perr("cannot change group");
296 if (setlogin(pentry
->pw_name
))
297 perr("cannot set login name");
299 if (setuid(uid
) < 0 || seteuid(uid
) < 0)
300 perr("cannot set user id");
302 if (chdir(pentry
->pw_dir
))
305 if(execle("/bin/sh","sh",(char *) NULL
, nenvp
) != 0)
306 perr("exec failed for /bin/sh");
310 /* We're the parent. Let's wait.
314 waitpid(pid
, (int *) NULL
, 0);
316 /* Send mail. Unlink the output file first, so it is deleted after
319 stat(filename
, &buf
);
320 if (open(filename
, O_RDONLY
) != STDIN_FILENO
)
321 perr("open of jobfile failed");
324 if ((buf
.st_size
!= size
) || send_mail
)
328 if (initgroups(pentry
->pw_name
,pentry
->pw_gid
))
329 perr("cannot delete saved userids");
331 if (setgid(gid
) < 0 || setegid(pentry
->pw_gid
) < 0)
332 perr("cannot change group");
334 if (setlogin(pentry
->pw_name
))
335 perr("cannot set login name");
337 if (setuid(uid
) < 0 || seteuid(uid
) < 0)
338 perr("cannot set user id");
340 if (chdir(pentry
->pw_dir
))
344 execl(_PATH_SENDMAIL
, "sendmail", "-F", "Atrun Service",
346 mailname
, (char *) NULL
);
348 execl(MAIL_CMD
, MAIL_CMD
, mailname
, (char *) NULL
);
350 perr("exec failed for mail command");
357 /* Global functions */
359 /* Needed in gloadavg.c */
368 syslog(LOG_ERR
, "%s: %m", a
);
374 main(int argc
, char *argv
[])
376 /* Browse through ATJOB_DIR, checking all the jobfiles wether they should
377 * be executed and or deleted. The queue is coded into the first byte of
378 * the job filename, the date (in minutes since Eon) as a hex number in the
379 * following eight bytes, followed by a dot and a serial number. A file
380 * which has not been executed yet is denoted by its execute - bit set.
381 * For those files which are to be executed, run_file() is called, which forks
382 * off a child which takes care of I/O redirection, forks off another child
383 * for execution and yet another one, optionally, for sending mail.
384 * Files which already have run are removed during the next invocation.
387 struct dirent
*dirent
;
392 time_t now
, run_time
;
393 char batch_name
[] = "Z2345678901234";
398 double load_avg
= LOADAVG_MX
, la
;
400 /* We don't need root privileges all the time; running under uid and gid daemon
404 RELINQUISH_PRIVS_ROOT(DAEMON_UID
, DAEMON_GID
)
406 openlog("atrun", LOG_PID
, LOG_CRON
);
409 while((c
=getopt(argc
, argv
, "dl:"))!= -1)
414 if (sscanf(optarg
, "%lf", &load_avg
) != 1)
415 perr("garbled option -l");
417 load_avg
= LOADAVG_MX
;
430 if (chdir(ATJOB_DIR
) != 0)
431 perr("cannot change to " ATJOB_DIR
);
433 /* Main loop. Open spool directory for reading and look over all the
434 * files in there. If the filename indicates that the job should be run
435 * and the x bit is set, fork off a child which sets its user and group
436 * id to that of the files and exec a /bin/sh which executes the shell
437 * script. Unlink older files if they should no longer be run. For
438 * deletion, their r bit has to be turned on.
440 * Also, pick the oldest batch job to run, at most one per invocation of
443 if ((spool
= opendir(".")) == NULL
)
444 perr("cannot read " ATJOB_DIR
);
448 batch_uid
= (uid_t
) -1;
449 batch_gid
= (gid_t
) -1;
451 while ((dirent
= readdir(spool
)) != NULL
) {
452 if (stat(dirent
->d_name
,&buf
) != 0)
453 perr("cannot stat in " ATJOB_DIR
);
455 /* We don't want directories
457 if (!S_ISREG(buf
.st_mode
))
460 if (sscanf(dirent
->d_name
,"%c%5lx%8lx",&queue
,&jobno
,&ctm
) != 3)
463 run_time
= (time_t) ctm
*60;
465 if ((S_IXUSR
& buf
.st_mode
) && (run_time
<=now
)) {
466 if (isupper(queue
) && (strcmp(batch_name
,dirent
->d_name
) > 0)) {
468 strncpy(batch_name
, dirent
->d_name
, sizeof(batch_name
));
469 batch_uid
= buf
.st_uid
;
470 batch_gid
= buf
.st_gid
;
473 /* The file is executable and old enough
476 run_file(dirent
->d_name
, buf
.st_uid
, buf
.st_gid
);
478 /* Delete older files
480 if ((run_time
< now
) && !(S_IXUSR
& buf
.st_mode
) && (S_IRUSR
& buf
.st_mode
))
481 unlink(dirent
->d_name
);
483 /* run the single batch file, if any
485 if (run_batch
&& (getloadavg(&la
, 1) == 1) && la
< load_avg
)
486 run_file(batch_name
, batch_uid
, batch_gid
);
496 fprintf(stderr
, "usage: atrun [-l load_avg] [-d]\n");
498 syslog(LOG_ERR
, "usage: atrun [-l load_avg] [-d]");