Deleted Added
full compact
misc.c (22997) misc.c (29452)
1/* Copyright 1988,1990,1993,1994 by Paul Vixie
2 * All rights reserved
3 *
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this
9 * software; use at your own risk, responsibility for damages (if any) to
10 * anyone resulting from the use of this software rests entirely with the
11 * user.
12 *
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14 * I'll try to keep a version up to date. I can be reached as follows:
15 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
16 */
17
18#if !defined(lint) && !defined(LINT)
1/* Copyright 1988,1990,1993,1994 by Paul Vixie
2 * All rights reserved
3 *
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this
9 * software; use at your own risk, responsibility for damages (if any) to
10 * anyone resulting from the use of this software rests entirely with the
11 * user.
12 *
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14 * I'll try to keep a version up to date. I can be reached as follows:
15 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
16 */
17
18#if !defined(lint) && !defined(LINT)
19static char rcsid[] = "$Id$";
19static const char rcsid[] =
20 "$Id: misc.c,v 1.5 1997/02/22 16:05:08 peter Exp $";
20#endif
21
22/* vix 26jan87 [RCS has the rest of the log]
23 * vix 30dec86 [written]
24 */
25
26
27#include "cron.h"
28#if SYS_TIME_H
29# include <sys/time.h>
30#else
31# include <time.h>
32#endif
33#include <sys/file.h>
34#include <sys/stat.h>
21#endif
22
23/* vix 26jan87 [RCS has the rest of the log]
24 * vix 30dec86 [written]
25 */
26
27
28#include "cron.h"
29#if SYS_TIME_H
30# include <sys/time.h>
31#else
32# include <time.h>
33#endif
34#include <sys/file.h>
35#include <sys/stat.h>
36#include <err.h>
35#include <errno.h>
36#include <string.h>
37#include <fcntl.h>
38#if defined(SYSLOG)
39# include <syslog.h>
40#endif
41
42

--- 126 unchanged lines hidden (view full) ---

169#endif /* DEBUGGING */
170}
171
172
173void
174set_cron_uid()
175{
176#if defined(BSD) || defined(POSIX)
37#include <errno.h>
38#include <string.h>
39#include <fcntl.h>
40#if defined(SYSLOG)
41# include <syslog.h>
42#endif
43
44

--- 126 unchanged lines hidden (view full) ---

171#endif /* DEBUGGING */
172}
173
174
175void
176set_cron_uid()
177{
178#if defined(BSD) || defined(POSIX)
177 if (seteuid(ROOT_UID) < OK) {
178 perror("seteuid");
179 exit(ERROR_EXIT);
180 }
179 if (seteuid(ROOT_UID) < OK)
180 err(ERROR_EXIT, "seteuid");
181#else
181#else
182 if (setuid(ROOT_UID) < OK) {
183 perror("setuid");
184 exit(ERROR_EXIT);
185 }
182 if (setuid(ROOT_UID) < OK)
183 err(ERROR_EXIT, "setuid");
186#endif
187}
188
189
190void
191set_cron_cwd()
192{
193 struct stat sb;
194
195 /* first check for CRONDIR ("/var/cron" or some such)
196 */
197 if (stat(CRONDIR, &sb) < OK && errno == ENOENT) {
184#endif
185}
186
187
188void
189set_cron_cwd()
190{
191 struct stat sb;
192
193 /* first check for CRONDIR ("/var/cron" or some such)
194 */
195 if (stat(CRONDIR, &sb) < OK && errno == ENOENT) {
198 perror(CRONDIR);
196 warn("%s", CRONDIR);
199 if (OK == mkdir(CRONDIR, 0700)) {
197 if (OK == mkdir(CRONDIR, 0700)) {
200 fprintf(stderr, "%s: created\n", CRONDIR);
198 warnx("%s: created", CRONDIR);
201 stat(CRONDIR, &sb);
202 } else {
199 stat(CRONDIR, &sb);
200 } else {
203 fprintf(stderr, "%s: ", CRONDIR);
204 perror("mkdir");
205 exit(ERROR_EXIT);
201 err(ERROR_EXIT, "%s: mkdir", CRONDIR);
206 }
207 }
202 }
203 }
208 if (!(sb.st_mode & S_IFDIR)) {
209 fprintf(stderr, "'%s' is not a directory, bailing out.\n",
210 CRONDIR);
211 exit(ERROR_EXIT);
212 }
213 if (chdir(CRONDIR) < OK) {
214 fprintf(stderr, "cannot chdir(%s), bailing out.\n", CRONDIR);
215 perror(CRONDIR);
216 exit(ERROR_EXIT);
217 }
204 if (!(sb.st_mode & S_IFDIR))
205 err(ERROR_EXIT, "'%s' is not a directory, bailing out", CRONDIR);
206 if (chdir(CRONDIR) < OK)
207 err(ERROR_EXIT, "cannot chdir(%s), bailing out", CRONDIR);
218
219 /* CRONDIR okay (now==CWD), now look at SPOOL_DIR ("tabs" or some such)
220 */
221 if (stat(SPOOL_DIR, &sb) < OK && errno == ENOENT) {
208
209 /* CRONDIR okay (now==CWD), now look at SPOOL_DIR ("tabs" or some such)
210 */
211 if (stat(SPOOL_DIR, &sb) < OK && errno == ENOENT) {
222 perror(SPOOL_DIR);
212 warn("%s", SPOOL_DIR);
223 if (OK == mkdir(SPOOL_DIR, 0700)) {
213 if (OK == mkdir(SPOOL_DIR, 0700)) {
224 fprintf(stderr, "%s: created\n", SPOOL_DIR);
214 warnx("%s: created", SPOOL_DIR);
225 stat(SPOOL_DIR, &sb);
226 } else {
215 stat(SPOOL_DIR, &sb);
216 } else {
227 fprintf(stderr, "%s: ", SPOOL_DIR);
228 perror("mkdir");
229 exit(ERROR_EXIT);
217 err(ERROR_EXIT, "%s: mkdir", SPOOL_DIR);
230 }
231 }
218 }
219 }
232 if (!(sb.st_mode & S_IFDIR)) {
233 fprintf(stderr, "'%s' is not a directory, bailing out.\n",
234 SPOOL_DIR);
235 exit(ERROR_EXIT);
236 }
220 if (!(sb.st_mode & S_IFDIR))
221 err(ERROR_EXIT, "'%s' is not a directory, bailing out", SPOOL_DIR);
237}
238
239
240/* acquire_daemonlock() - write our PID into /etc/cron.pid, unless
241 * another daemon is already running, which we detect here.
242 *
243 * note: main() calls us twice; once before forking, once after.
244 * we maintain static storage of the file pointer so that we

--- 19 unchanged lines hidden (view full) ---

264 int fd, otherpid;
265
266 (void) sprintf(pidfile, PIDFILE, PIDDIR);
267 if ((-1 == (fd = open(pidfile, O_RDWR|O_CREAT, 0644)))
268 || (NULL == (fp = fdopen(fd, "r+")))
269 ) {
270 sprintf(buf, "can't open or create %s: %s",
271 pidfile, strerror(errno));
222}
223
224
225/* acquire_daemonlock() - write our PID into /etc/cron.pid, unless
226 * another daemon is already running, which we detect here.
227 *
228 * note: main() calls us twice; once before forking, once after.
229 * we maintain static storage of the file pointer so that we

--- 19 unchanged lines hidden (view full) ---

249 int fd, otherpid;
250
251 (void) sprintf(pidfile, PIDFILE, PIDDIR);
252 if ((-1 == (fd = open(pidfile, O_RDWR|O_CREAT, 0644)))
253 || (NULL == (fp = fdopen(fd, "r+")))
254 ) {
255 sprintf(buf, "can't open or create %s: %s",
256 pidfile, strerror(errno));
272 fprintf(stderr, "%s: %s\n", ProgramName, buf);
273 log_it("CRON", getpid(), "DEATH", buf);
257 log_it("CRON", getpid(), "DEATH", buf);
274 exit(ERROR_EXIT);
258 errx(ERROR_EXIT, "%s", buf);
275 }
276
277 if (flock(fd, LOCK_EX|LOCK_NB) < OK) {
278 int save_errno = errno;
279
280 fscanf(fp, "%d", &otherpid);
281 sprintf(buf, "can't lock %s, otherpid may be %d: %s",
282 pidfile, otherpid, strerror(save_errno));
259 }
260
261 if (flock(fd, LOCK_EX|LOCK_NB) < OK) {
262 int save_errno = errno;
263
264 fscanf(fp, "%d", &otherpid);
265 sprintf(buf, "can't lock %s, otherpid may be %d: %s",
266 pidfile, otherpid, strerror(save_errno));
283 fprintf(stderr, "%s: %s\n", ProgramName, buf);
284 log_it("CRON", getpid(), "DEATH", buf);
267 log_it("CRON", getpid(), "DEATH", buf);
285 exit(ERROR_EXIT);
268 errx(ERROR_EXIT, "%s", buf);
286 }
287
288 (void) fcntl(fd, F_SETFD, 1);
289 }
290
291 rewind(fp);
292 fprintf(fp, "%d\n", getpid());
293 fflush(fp);

--- 184 unchanged lines hidden (view full) ---

478 msg = malloc(strlen(username)
479 + strlen(event)
480 + strlen(detail)
481 + MAX_TEMPSTR);
482
483 if (LogFD < OK) {
484 LogFD = open(LOG_FILE, O_WRONLY|O_APPEND|O_CREAT, 0600);
485 if (LogFD < OK) {
269 }
270
271 (void) fcntl(fd, F_SETFD, 1);
272 }
273
274 rewind(fp);
275 fprintf(fp, "%d\n", getpid());
276 fflush(fp);

--- 184 unchanged lines hidden (view full) ---

461 msg = malloc(strlen(username)
462 + strlen(event)
463 + strlen(detail)
464 + MAX_TEMPSTR);
465
466 if (LogFD < OK) {
467 LogFD = open(LOG_FILE, O_WRONLY|O_APPEND|O_CREAT, 0600);
468 if (LogFD < OK) {
486 fprintf(stderr, "%s: can't open log file\n",
487 ProgramName);
488 perror(LOG_FILE);
469 warn("can't open log file %s", LOG_FILE);
489 } else {
490 (void) fcntl(LogFD, F_SETFD, 1);
491 }
492 }
493
494 /* we have to sprintf() it because fprintf() doesn't always write
495 * everything out in one chunk and this has to be atomically appended
496 * to the log file.
497 */
498 sprintf(msg, "%s (%02d/%02d-%02d:%02d:%02d-%d) %s (%s)\n",
499 username,
500 t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, pid,
501 event, detail);
502
503 /* we have to run strlen() because sprintf() returns (char*) on old BSD
504 */
505 if (LogFD < OK || write(LogFD, msg, strlen(msg)) < OK) {
506 if (LogFD >= OK)
470 } else {
471 (void) fcntl(LogFD, F_SETFD, 1);
472 }
473 }
474
475 /* we have to sprintf() it because fprintf() doesn't always write
476 * everything out in one chunk and this has to be atomically appended
477 * to the log file.
478 */
479 sprintf(msg, "%s (%02d/%02d-%02d:%02d:%02d-%d) %s (%s)\n",
480 username,
481 t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, pid,
482 event, detail);
483
484 /* we have to run strlen() because sprintf() returns (char*) on old BSD
485 */
486 if (LogFD < OK || write(LogFD, msg, strlen(msg)) < OK) {
487 if (LogFD >= OK)
507 perror(LOG_FILE);
508 fprintf(stderr, "%s: can't write to log file\n", ProgramName);
488 warn("%s", LOG_FILE);
489 warnx("can't write to log file");
509 write(STDERR, msg, strlen(msg));
510 }
511
512 free(msg);
513#endif /*LOG_FILE*/
514
515#if defined(SYSLOG)
516 if (!syslog_open) {

--- 148 unchanged lines hidden ---
490 write(STDERR, msg, strlen(msg));
491 }
492
493 free(msg);
494#endif /*LOG_FILE*/
495
496#if defined(SYSLOG)
497 if (!syslog_open) {

--- 148 unchanged lines hidden ---