Fixup braces (patch from Thomas Geulig), run through indent and manually

adjust.
This commit is contained in:
Glenn L McGrath 2004-02-22 04:44:21 +00:00
parent e7f2b2bf31
commit 9079ad0cab

View File

@ -99,6 +99,7 @@ static void RunJobs(void);
static int CheckJobs(void);
static void RunJob(const char *user, CronLine * line);
#ifdef CONFIG_FEATURE_CROND_CALL_SENDMAIL
static void EndJob(const char *user, CronLine * line);
#else
@ -110,8 +111,7 @@ static void DeleteFile(const char *userName);
static CronFile *FileBase;
static void
crondlog(const char *ctl, ...)
static void crondlog(const char *ctl, ...)
{
va_list va;
const char *fmt;
@ -125,14 +125,15 @@ crondlog(const char *ctl, ...)
if (level >= LogLevel) {
#ifdef FEATURE_DEBUG_OPT
if (DebugOpt) vfprintf(stderr, fmt, va);
else
if (DebugOpt) {
vfprintf(stderr, fmt, va);
} else
#endif
if (LogFile == 0) vsyslog(type, fmt, va);
else {
int logfd;
if ((logfd = open(LogFile, O_WRONLY|O_CREAT|O_APPEND, 600)) >= 0) {
if (LogFile == 0) {
vsyslog(type, fmt, va);
} else {
int logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 600);
if (logfd >= 0) {
vdprintf(logfd, fmt, va);
close(logfd);
#ifdef FEATURE_DEBUG_OPT
@ -143,17 +144,19 @@ crondlog(const char *ctl, ...)
}
}
va_end(va);
if(ctl[0] & 0200)
if (ctl[0] & 0200) {
exit(20);
}
}
int
crond_main(int ac, char **av)
int crond_main(int ac, char **av)
{
unsigned long opt;
char *lopt, *Lopt, *copt;
#ifdef FEATURE_DEBUG_OPT
char *dopt;
bb_opt_complementaly = "f-b:b-f:S-L:L-S:d-l";
#else
bb_opt_complementaly = "f-b:b-f:S-L:L-S";
@ -169,12 +172,18 @@ crond_main(int ac, char **av)
, &dopt
#endif
);
if(opt & 1)
if (opt & 1) {
LogLevel = atoi(lopt);
if(opt & 2)
if (*Lopt != 0) LogFile = Lopt;
}
if (opt & 2) {
if (*Lopt != 0) {
LogFile = Lopt;
}
}
if (opt & 32) {
if (*copt != 0) CDir = copt;
if (*copt != 0) {
CDir = copt;
}
}
#ifdef FEATURE_DEBUG_OPT
if (opt & 64) {
@ -187,9 +196,9 @@ crond_main(int ac, char **av)
* change directory
*/
if (chdir(CDir) != 0)
if (chdir(CDir) != 0) {
bb_perror_msg_and_die("%s", CDir);
}
signal(SIGHUP, SIG_IGN); /* hmm.. but, if kill -HUP original
* version - his died. ;(
*/
@ -206,6 +215,7 @@ crond_main(int ac, char **av)
#else /* uClinux */
if (daemon(1, 0) < 0) {
bb_perror_msg_and_die("daemon");
}
#endif /* uClinux */
}
@ -216,8 +226,8 @@ crond_main(int ac, char **av)
* of 1 second.
*/
crondlog("\011%s " VERSION " dillon, started, log level %d\n", bb_applet_name,
LogLevel);
crondlog("\011%s " VERSION " dillon, started, log level %d\n",
bb_applet_name, LogLevel);
SynchronizeDir();
@ -267,24 +277,23 @@ crond_main(int ac, char **av)
TestJobs(t1, t2);
RunJobs();
sleep(5);
if (CheckJobs() > 0)
if (CheckJobs() > 0) {
sleep_time = 10;
else
} else {
sleep_time = 60;
}
t1 = t2;
}
}
}
/* not reached */
}
}
#if defined(FEATURE_DEBUG_OPT) || defined(CONFIG_FEATURE_CROND_CALL_SENDMAIL)
/*
write to temp file..
*/
static void
fdprintf(int fd, const char *ctl, ...)
static void fdprintf(int fd, const char *ctl, ...)
{
va_list va;
@ -295,8 +304,7 @@ fdprintf(int fd, const char *ctl, ...)
#endif
static int
ChangeUser(const char *user)
static int ChangeUser(const char *user)
{
struct passwd *pas;
const char *err_msg;
@ -304,8 +312,8 @@ ChangeUser(const char *user)
/*
* Obtain password entry and change privilages
*/
if ((pas = getpwnam(user)) == 0) {
pas = getpwnam(user);
if (pas == 0) {
crondlog("\011failed to get uid for %s", user);
return (-1);
}
@ -331,20 +339,21 @@ ChangeUser(const char *user)
return (pas->pw_uid);
}
static void
startlogger(void)
static void startlogger(void)
{
if (LogFile == 0)
if (LogFile == 0) {
openlog(bb_applet_name, LOG_CONS | LOG_PID, LOG_CRON);
}
#ifdef FEATURE_DEBUG_OPT
else { /* test logfile */
int logfd;
if ((logfd = open(LogFile, O_WRONLY|O_CREAT|O_APPEND, 600)) >= 0)
if ((logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 600)) >= 0) {
close(logfd);
else
} else {
bb_perror_msg("Failed to open log file '%s' reason", LogFile);
}
}
#endif
}
@ -397,23 +406,21 @@ static const char * const MonAry[] = {
NULL
};
static char *
ParseField(char *user, char *ary, int modvalue, int off,
static char *ParseField(char *user, char *ary, int modvalue, int off,
const char *const *names, char *ptr)
{
char *base = ptr;
int n1 = -1;
int n2 = -1;
if (base == NULL)
if (base == NULL) {
return (NULL);
}
while (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
int skip = 0;
/*
* Handle numeric digit or symbol or '*'
*/
/* Handle numeric digit or symbol or '*' */
if (*ptr == '*') {
n1 = 0; /* everything will be filled */
@ -421,10 +428,11 @@ ParseField(char *user, char *ary, int modvalue, int off,
skip = 1;
++ptr;
} else if (*ptr >= '0' && *ptr <= '9') {
if (n1 < 0)
if (n1 < 0) {
n1 = strtol(ptr, &ptr, 10) + off;
else
} else {
n2 = strtol(ptr, &ptr, 10) + off;
}
skip = 1;
} else if (names) {
int i;
@ -436,17 +444,16 @@ ParseField(char *user, char *ary, int modvalue, int off,
}
if (names[i]) {
ptr += strlen(names[i]);
if (n1 < 0)
if (n1 < 0) {
n1 = i;
else
} else {
n2 = i;
}
skip = 1;
}
}
/*
* handle optional range '-'
*/
/* handle optional range '-' */
if (skip == 0) {
crondlog("\111failed user %s parsing %s\n", user, base);
@ -462,12 +469,12 @@ ParseField(char *user, char *ary, int modvalue, int off,
* in the character array appropriately.
*/
if (n2 < 0)
if (n2 < 0) {
n2 = n1;
if (*ptr == '/')
}
if (*ptr == '/') {
skip = strtol(ptr + 1, &ptr, 10);
}
/*
* fill array, using a failsafe is the easiest way to prevent
* an endless loop
@ -485,15 +492,17 @@ ParseField(char *user, char *ary, int modvalue, int off,
ary[n1 % modvalue] = 1;
s0 = skip;
}
} while (n1 != n2 && --failsafe);
}
while (n1 != n2 && --failsafe);
if (failsafe == 0) {
crondlog("\111failed user %s parsing %s\n", user, base);
return (NULL);
}
}
if (*ptr != ',')
if (*ptr != ',') {
break;
}
++ptr;
n1 = -1;
n2 = -1;
@ -504,15 +513,16 @@ ParseField(char *user, char *ary, int modvalue, int off,
return (NULL);
}
while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n')
while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n') {
++ptr;
}
#ifdef FEATURE_DEBUG_OPT
if (DebugOpt) {
int i;
for (i = 0; i < modvalue; ++i)
for (i = 0; i < modvalue; ++i) {
crondlog("\005%d", ary[i]);
}
crondlog("\005\n");
}
#endif
@ -520,8 +530,7 @@ ParseField(char *user, char *ary, int modvalue, int off,
return (ptr);
}
static void
FixDayDow(CronLine *line)
static void FixDayDow(CronLine * line)
{
short i;
short weekUsed = 0;
@ -549,15 +558,15 @@ FixDayDow(CronLine *line)
static void
SynchronizeFile(const char *fileName)
static void SynchronizeFile(const char *fileName)
{
int maxEntries = MAXLINES;
int maxLines;
char buf[1024];
if (strcmp(fileName, "root") == 0)
if (strcmp(fileName, "root") == 0) {
maxEntries = 65535;
}
maxLines = maxEntries * 10;
if (fileName) {
@ -565,7 +574,8 @@ SynchronizeFile(const char *fileName)
DeleteFile(fileName);
if ((fi = fopen(fileName, "r")) != NULL) {
fi = fopen(fileName, "r");
if (fi != NULL) {
struct stat sbuf;
if (fstat(fileno(fi), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
@ -579,38 +589,34 @@ SynchronizeFile(const char *fileName)
CronLine line;
char *ptr;
if (buf[0])
if (buf[0]) {
buf[strlen(buf) - 1] = 0;
if (buf[0] == 0 || buf[0] == '#' || buf[0] == ' ' || buf[0] == '\t')
}
if (buf[0] == 0 || buf[0] == '#' || buf[0] == ' ' || buf[0] == '\t') {
continue;
if (--maxEntries == 0)
}
if (--maxEntries == 0) {
break;
}
memset(&line, 0, sizeof(line));
#ifdef FEATURE_DEBUG_OPT
if (DebugOpt)
if (DebugOpt) {
crondlog("\111User %s Entry %s\n", fileName, buf);
}
#endif
/*
* parse date ranges
*/
/* parse date ranges */
ptr = ParseField(file->cf_User, line.cl_Mins, 60, 0, NULL, buf);
ptr = ParseField(file->cf_User, line.cl_Hrs, 24, 0, NULL, ptr);
ptr = ParseField(file->cf_User, line.cl_Days, 32, 0, NULL, ptr);
ptr = ParseField(file->cf_User, line.cl_Mons, 12, -1, MonAry, ptr);
ptr = ParseField(file->cf_User, line.cl_Dow, 7, 0, DowAry, ptr);
/*
* check failure
*/
if (ptr == NULL)
/* check failure */
if (ptr == NULL) {
continue;
}
/*
* fix days and dow - if one is not * and the other
@ -622,10 +628,7 @@ SynchronizeFile(const char *fileName)
*pline = calloc(1, sizeof(CronLine));
**pline = line;
/*
* copy command
*/
/* copy command */
(*pline)->cl_Shell = strdup(ptr);
#ifdef FEATURE_DEBUG_OPT
@ -641,21 +644,22 @@ SynchronizeFile(const char *fileName)
file->cf_Next = FileBase;
FileBase = file;
if (maxLines == 0 || maxEntries == 0)
if (maxLines == 0 || maxEntries == 0) {
crondlog("\111Maximum number of lines reached for user %s\n", fileName);
}
}
fclose(fi);
}
}
}
static void
CheckUpdates(void)
static void CheckUpdates(void)
{
FILE *fi;
char buf[256];
if ((fi = fopen(CRONUPDATE, "r")) != NULL) {
fi = fopen(CRONUPDATE, "r");
if (fi != NULL) {
remove(CRONUPDATE);
while (fgets(buf, sizeof(buf), fi) != NULL) {
SynchronizeFile(strtok(buf, " \t\r\n"));
@ -664,20 +668,17 @@ CheckUpdates(void)
}
}
static void
SynchronizeDir(void)
static void SynchronizeDir(void)
{
/*
* Attempt to delete the database.
*/
/* Attempt to delete the database. */
for (;;) {
CronFile *file;
for (file = FileBase; file && file->cf_Deleted; file = file->cf_Next)
;
if (file == NULL)
for (file = FileBase; file && file->cf_Deleted; file = file->cf_Next);
if (file == NULL) {
break;
}
DeleteFile(file->cf_User);
}
@ -695,18 +696,20 @@ SynchronizeDir(void)
crondlog("\311unable to find %s\n", CDir);
}
{
DIR *dir;
DIR *dir = opendir(".");
struct dirent *den;
if ((dir = opendir("."))) {
if (dir) {
while ((den = readdir(dir))) {
if (strchr(den->d_name, '.') != NULL)
if (strchr(den->d_name, '.') != NULL) {
continue;
if (getpwnam(den->d_name))
}
if (getpwnam(den->d_name)) {
SynchronizeFile(den->d_name);
else
} else {
crondlog("\007ignoring %s\n", den->d_name);
}
}
closedir(dir);
} else {
crondlog("\311Unable to open current dir!\n");
@ -722,8 +725,7 @@ SynchronizeDir(void)
* completely delete a database due to running processes.
*/
static void
DeleteFile(const char *userName)
static void DeleteFile(const char *userName)
{
CronFile **pfile = &FileBase;
CronFile *file;
@ -767,15 +769,12 @@ DeleteFile(const char *userName)
* hour (60 scans).
*/
static int
TestJobs(time_t t1, time_t t2)
static int TestJobs(time_t t1, time_t t2)
{
short nJobs = 0;
time_t t;
/*
* Find jobs > t1 and <= t2
*/
/* Find jobs > t1 and <= t2 */
for (t = t1 - t1 % 60; t <= t2; t += 60) {
if (t > t1) {
@ -795,20 +794,18 @@ TestJobs(time_t t1, time_t t2)
if (DebugOpt)
crondlog("\005 LINE %s\n", line->cl_Shell);
#endif
if (line->cl_Mins[tp->tm_min] &&
line->cl_Hrs[tp->tm_hour] &&
(line->cl_Days[tp->tm_mday] || line->cl_Dow[tp->tm_wday]) &&
line->cl_Mons[tp->tm_mon]
) {
if (line->cl_Mins[tp->tm_min] && line->cl_Hrs[tp->tm_hour] &&
(line->cl_Days[tp->tm_mday] || line->cl_Dow[tp->tm_wday])
&& line->cl_Mons[tp->tm_mon]) {
#ifdef FEATURE_DEBUG_OPT
if (DebugOpt)
crondlog("\005 JobToDo: %d %s\n", line->cl_Pid, line->cl_Shell);
if (DebugOpt) {
crondlog("\005 JobToDo: %d %s\n",
line->cl_Pid, line->cl_Shell);
}
#endif
if (line->cl_Pid > 0) {
crondlog("\010 process already running: %s %s\n",
file->cf_User,
line->cl_Shell
);
file->cf_User, line->cl_Shell);
} else if (line->cl_Pid == 0) {
line->cl_Pid = -1;
file->cf_Ready = 1;
@ -822,8 +819,7 @@ TestJobs(time_t t1, time_t t2)
return (nJobs);
}
static void
RunJobs(void)
static void RunJobs(void)
{
CronFile *file;
CronLine *line;
@ -838,19 +834,18 @@ RunJobs(void)
RunJob(file->cf_User, line);
crondlog("\010USER %s pid %3d cmd %s\n",
file->cf_User,
line->cl_Pid,
line->cl_Shell
);
if (line->cl_Pid < 0)
file->cf_User, line->cl_Pid, line->cl_Shell);
if (line->cl_Pid < 0) {
file->cf_Ready = 1;
else if (line->cl_Pid > 0)
}
else if (line->cl_Pid > 0) {
file->cf_Running = 1;
}
}
}
}
}
}
/*
* CheckJobs() - check for job completion
@ -859,8 +854,7 @@ RunJobs(void)
* all done.
*/
static int
CheckJobs(void)
static int CheckJobs(void)
{
CronFile *file;
CronLine *line;
@ -877,8 +871,9 @@ CheckJobs(void)
if (r < 0 || r == line->cl_Pid) {
EndJob(file->cf_User, line);
if (line->cl_Pid)
if (line->cl_Pid) {
file->cf_Running = 1;
}
} else if (r == 0) {
file->cf_Running = 1;
}
@ -896,27 +891,22 @@ static void
ForkJob(const char *user, CronLine * line, int mailFd,
const char *prog, const char *cmd, const char *arg, const char *mailf)
{
/*
* Fork as the user in question and run program
*/
/* Fork as the user in question and run program */
pid_t pid = fork();
line->cl_Pid = pid;
if (pid == 0) {
/*
* CHILD
*/
/* CHILD */
/*
* Change running state to the user in question
*/
/* Change running state to the user in question */
if (ChangeUser(user) < 0)
if (ChangeUser(user) < 0) {
exit(0);
}
#ifdef FEATURE_DEBUG_OPT
if (DebugOpt)
if (DebugOpt) {
crondlog("\005Child Running %s\n", prog);
}
#endif
if (mailFd >= 0) {
@ -925,29 +915,25 @@ ForkJob(const char *user, CronLine *line, int mailFd,
close(mailFd);
}
execl(prog, prog, cmd, arg, NULL);
crondlog("\024unable to exec, user %s cmd %s %s %s\n", user,
prog, cmd, arg);
if(mailf)
crondlog("\024unable to exec, user %s cmd %s %s %s\n", user, prog, cmd, arg);
if (mailf) {
fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
}
exit(0);
} else if (pid < 0) {
/*
* FORK FAILED
*/
/* FORK FAILED */
crondlog("\024couldn't fork, user %s\n", user);
line->cl_Pid = 0;
if(mailf)
if (mailf) {
remove(mailf);
}
} else if (mailf) {
/*
* PARENT, FORK SUCCESS
*
/* PARENT, FORK SUCCESS
* rename mail-file based on pid of process
*/
char mailFile2[128];
snprintf(mailFile2, sizeof(mailFile2), TMPDIR "/cron.%s.%d",
user, pid);
snprintf(mailFile2, sizeof(mailFile2), TMPDIR "/cron.%s.%d", user, pid);
rename(mailf, mailFile2);
}
/*
@ -955,12 +941,12 @@ ForkJob(const char *user, CronLine *line, int mailFd,
* a structure, closing it later, because we might run out of descriptors
*/
if (mailFd >= 0)
if (mailFd >= 0) {
close(mailFd);
}
}
static void
RunJob(const char *user, CronLine *line)
static void RunJob(const char *user, CronLine * line)
{
char mailFile[128];
int mailFd;
@ -968,12 +954,9 @@ RunJob(const char *user, CronLine *line)
line->cl_Pid = 0;
line->cl_MailFlag = 0;
/*
* open mail file - owner root so nobody can screw with it.
*/
/* open mail file - owner root so nobody can screw with it. */
snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d",
user, getpid());
snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", user, getpid());
mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
if (mailFd >= 0) {
@ -982,8 +965,7 @@ RunJob(const char *user, CronLine *line)
line->cl_Shell);
line->cl_MailPos = lseek(mailFd, 0, 1);
} else {
crondlog("\024unable to create mail file user %s file %s, output to /dev/null\n",
user, mailFile);
crondlog("\024unable to create mail file user %s file %s, output to /dev/null\n", user, mailFile);
}
ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile);
@ -993,16 +975,13 @@ RunJob(const char *user, CronLine *line)
* EndJob - called when job terminates and when mail terminates
*/
static void
EndJob(const char *user, CronLine *line)
static void EndJob(const char *user, CronLine * line)
{
int mailFd;
char mailFile[128];
struct stat sbuf;
/*
* No job
*/
/* No job */
if (line->cl_Pid <= 0) {
line->cl_Pid = 0;
@ -1014,13 +993,12 @@ EndJob(const char *user, CronLine *line)
* End of sendmail job
*/
snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d",
user, line->cl_Pid);
snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", user, line->cl_Pid);
line->cl_Pid = 0;
if (line->cl_MailFlag != 1)
if (line->cl_MailFlag != 1) {
return;
}
line->cl_MailFlag = 0;
/*
@ -1034,12 +1012,8 @@ EndJob(const char *user, CronLine *line)
return;
}
if (fstat(mailFd, &sbuf) < 0 ||
sbuf.st_uid != DaemonUid ||
sbuf.st_nlink != 0 ||
sbuf.st_size == line->cl_MailPos ||
!S_ISREG(sbuf.st_mode)
) {
if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid || sbuf.st_nlink != 0 ||
sbuf.st_size == line->cl_MailPos || !S_ISREG(sbuf.st_mode)) {
close(mailFd);
return;
}
@ -1048,29 +1022,23 @@ EndJob(const char *user, CronLine *line)
#else
/* crond whithout sendmail */
static void
RunJob(const char *user, CronLine *line)
static void RunJob(const char *user, CronLine * line)
{
/*
* Fork as the user in question and run program
*/
/* Fork as the user in question and run program */
pid_t pid = fork();
if (pid == 0) {
/*
* CHILD
*/
/* CHILD */
/*
* Change running state to the user in question
*/
/* Change running state to the user in question */
if (ChangeUser(user) < 0)
if (ChangeUser(user) < 0) {
exit(0);
}
#ifdef FEATURE_DEBUG_OPT
if (DebugOpt)
if (DebugOpt) {
crondlog("\005Child Running %s\n", DEFAULT_SHELL);
}
#endif
execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, NULL);
@ -1078,9 +1046,7 @@ RunJob(const char *user, CronLine *line)
DEFAULT_SHELL, line->cl_Shell);
exit(0);
} else if (pid < 0) {
/*
* FORK FAILED
*/
/* FORK FAILED */
crondlog("\024couldn't fork, user %s\n", user);
pid = 0;
}