home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Source Code 1992 March
/
Source_Code_CD-ROM_Walnut_Creek_March_1992.iso
/
unix_c
/
usenet
/
newsxd.tar
/
newsxd
/
util.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-08-31
|
11KB
|
350 lines
/*
* #include <legal/bs.h>
>
> Copyright (c) 1989 Washington University in Saint Louis, Missouri and
> Chris Myers. All rights reserved.
>
> Permission is hereby granted to copy, reproduce, redistribute or
> otherwise use this software as long as: (1) there is no monetary
> profit gained specifically from the use or reproduction of this
> software, (2) it is not sold, rented, traded, or otherwise marketed,
> (3) the above copyright notice and this paragraph is included
> prominently in any copy made, and (4) that the name of the University
> is not used to endorse or promote products derived from this software
> without the specific prior written permission of the University.
> THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
> IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
> WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
>
*/
#include "defs.h"
/*************************************************************************/
/* FUNCTION : daemon_start */
/* PURPOSE : To disassociate newsxd from the calling process so it can */
/* run as a daemon. */
/* ARGUMENTS : none */
/*************************************************************************/
void
daemon_start()
{
register int childpid, fd;
extern int errno;
/* Ignore the terminal stop signals */
(void) signal(SIGTTOU, SIG_IGN);
(void) signal(SIGTTIN, SIG_IGN);
(void) signal(SIGTSTP, SIG_IGN);
/* Fork and let the parent process exit */
if ((childpid = fork()) < 0) {
logerr("newsxd: can't fork to enter daemon mode\n");
(void) exit(1);
} else if (childpid > 0) exit(0);
/* Lose the process group */
if (setpgrp(0, getpid()) == -1) {
logerr("newsxd: can't change pgrp to enter daemon mode\n");
(void) exit(1);
}
/* Lose the controlling terminal */
if ((fd = open("/dev/tty", O_RDWR)) >= 0) {
(void) ioctl(fd, TIOCNOTTY, (char *) NULL);
(void) close(fd);
}
/* Close any open files */
for (fd = 0; fd < NOFILE; fd++) (void) close(fd);
errno = 0;
/* Set a proper default umask */
(void) umask(022);
}
/*************************************************************************/
/* FUNCTION : getla */
/* PURPOSE : Return the current system load */
/* ARGUMENTS : none */
/* NOTES : this code stolen from sendmail 5.61 which stole it from */
/* from something else... */
/*************************************************************************/
int
getla()
{
#if defined(sun) | defined(mips)
long avenrun[3];
#else
double avenrun[3];
#endif
extern off_t lseek();
static int kmem = -1;
static struct nlist Nl[] =
{
{ "_avenrun" },
#define X_AVENRUN 0
{ 0 },
};
if (kmem < 0) {
kmem = open("/dev/kmem", 0, 0);
if (kmem < 0) return (-1);
(void) ioctl(kmem, (int) FIOCLEX, (char *) 0);
(void) nlist("/vmunix", Nl);
if (Nl[0].n_type == 0) return (-1);
}
if (lseek(kmem, (off_t) Nl[X_AVENRUN].n_value, 0) == -1 ||
read(kmem, (char *) avenrun, sizeof(avenrun)) < sizeof(avenrun))
{
/* thank you Ian */
return (-1);
}
#ifdef sun
return ((int) (avenrun[0] + FSCALE/2) >> FSHIFT);
#else
#ifdef mips
return (FIX_TO_INT(avenrun[0]));
#else
return ((int) (avenrun[0] + 0.5));
#endif
#endif
}
/*************************************************************************/
/* FUNCTION : processarg */
/* PURPOSE : Do %value substitutions in xargv */
/* ARGUMENTS : which value of argv to modify, hostptr, classptr */
/*************************************************************************/
void
processarg(which, hostptr, classptr)
int which;
struct host *hostptr;
struct class *classptr;
{
char buf1[MAXPATHLEN],
buf2[MAXPATHLEN],
charbuf[2],
*newxargv,
*strptr;
if (classptr->xargv[which] == NULL) return;
charbuf[1] = '\0';
*buf1 = '\0';
for (strptr = classptr->xargv[which]; *strptr != '\0'; strptr++) {
if (*strptr != '%') {
charbuf[0] = *strptr;
(void) strcat(buf1, charbuf);
} else {
strptr++;
switch (*strptr) {
case 'h': (void) strcat(buf1, hostptr->hostname);
break;
case 'f': (void) strcat(buf1, "%f");
/* We shouldn't have seen a %f at this point! */
break;
case 'd': (void) strcat(buf1, "%d");
/* We shouldn't have seen a %d at this point! */
break;
case 'b': (void) sprintf(buf2, batchfile, hostptr->hostname);
(void) strcat(buf1, buf2);
break;
case 'w': if (classptr->flags[C_NOWORK])
(void) sprintf(buf2, batchfile, hostptr->hostname);
else
(void) sprintf(buf2, workfile, hostptr->hostname);
(void) strcat(buf1, buf2);
break;
case 's': (void) sprintf(buf2, "%d", hostptr->classslot);
(void) strcat(buf1, buf2);
break;
case '%': (void) strcat(buf1, "%"); /* %% changes to % */
break;
default : strptr--;
}
}
}
newxargv = (char *) malloc(strlen(buf1) + 1);
(void) strcpy(newxargv, buf1);
classptr->xargv[which] = newxargv;
}
/*************************************************************************/
/* FUNCTION : parse_time */
/* PURPOSE : Check a single valid-time-string against the current time */
/* and return whether or not a match occurs. */
/* ARGUMENTS : a pointer to the time-string */
/*************************************************************************/
int
parsetime(whattime)
char *whattime;
{
static char *days[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", "Wk" };
long clock;
struct tm *curtime;
int wday, start, stop, ltime, validday, loop, match;
(void) time(&clock);
curtime = localtime(&clock);
wday = curtime->tm_wday;
validday = 0;
match = 1;
while (match && isalpha(*whattime) && isupper(*whattime)) {
match = 0;
for (loop = 0; loop < 8; loop++) {
if (strncmp(days[loop], whattime, 2) == NULL) {
whattime += 2;
match = 1;
if ((wday == loop) | ((loop == 7) && wday && (wday < 6))) {
validday = 1;
}
}
}
}
if (strncmp(whattime, "Any", 3) == NULL) {
validday = 1;
whattime += 3;
}
if (!validday) return 0;
if (sscanf(whattime, "%d-%d", &start, &stop) == 2) {
ltime = curtime->tm_min + 100 * curtime->tm_hour;
if ((start < stop) && ((ltime > start) & ltime < stop)) return 1;
if ((start > stop) && ((ltime > start) | ltime < stop)) return 1;
} else return 1;
return 0;
}
/*************************************************************************/
/* FUNCTION : validtime */
/* PURPOSE : Break apart a set of valid time-strings and pass them to */
/* parse_time, returning whether or not ANY matches occurred */
/* ARGUMENTS : a pointer to the time-string */
/*************************************************************************/
int
validtime(ptr)
char *ptr;
{
char *nextptr;
int good;
while (1) {
nextptr = STRCHR(ptr, '|');
if (STRCHR(ptr, '|') == NULL) return(parsetime(ptr));
*nextptr = '\0';
good = parsetime(ptr);
*nextptr++ = '|'; /* gotta restore the | or things get skipped! */
if (good) return(1);
ptr = nextptr;
}
}
/*************************************************************************/
/* FUNCTION : getclassslot */
/* PURPOSE : */
/* ARGUMENTS : a pointer to the class structure */
/* RETURNS : the slot number */
/*************************************************************************/
int getclassslot(classptr)
struct class *classptr;
{
int loop;
for (loop = 0; loop < MAXCLASSXMITTERS; loop++)
if (classptr->slots[loop] != 'X') {
classptr->slots[loop] = 'X';
return loop;
}
logerr("(getclassslot) Too many xmitters for class %s", classptr->classname);
return -1;
}
/*************************************************************************/
/* FUNCTION : freeclassslot */
/* PURPOSE : */
/* ARGUMENTS : slot number to free and a pointer to the class structure */
/*************************************************************************/
void freeclassslot(classptr, slot)
struct class *classptr;
int slot;
{
classptr->slots[slot] = '.';
}
/*************************************************************************/
/* FUNCTION : idle */
/* PURPOSE : Set newsxd to idle mode. Don't process xmitter queue... */
/* ARGUMENTS : none */
/*************************************************************************/
void
idle()
{
daemon_idle = 1;
}
/*************************************************************************/
/* FUNCTION : reset */
/* PURPOSE : Kill all outstanding transmitters and idle newsxd. */
/* ARGUMENTS : none */
/*************************************************************************/
void
reset()
{
kill_children(0);
daemon_idle = 1;
}
#ifdef CNEWSLOCKING
/*************************************************************************/
/* FUNCTION : unprivileged */
/* PURPOSE : keep the cnews libraries happy. */
/* ARGUMENTS : none used. */
/*************************************************************************/
int
unprivileged(reason)
char *reason;
{
}
#endif CNEWSLOCKING