|
|
1.1 ! root 1: /* ! 2: * daemon0.c -- dem_setup() and logerr() routines for spider and ! 3: * and dataphone dpd and fget daemons. ! 4: */ ! 5: ! 6: #include <stdio.h> ! 7: #include <sys/types.h> ! 8: #include <sys/stat.h> ! 9: #include <pwd.h> ! 10: #include <signal.h> ! 11: ! 12: #define WRMODE 2 ! 13: ! 14: #define FCLOSE(F) if(F != NULL){ fclose(F); F = NULL;} ! 15: #define DAEMUID 1 ! 16: #define DAEMNAM "daemon\0\0" ! 17: ! 18: struct stat statbuf; ! 19: struct passwd *getpwuid(); ! 20: ! 21: unlock() ! 22: { ! 23: signal(SIGTERM, SIG_IGN); ! 24: dem_dis(); ! 25: logerr("Daemon killed."); ! 26: unlink(lock); ! 27: exit(1); ! 28: } ! 29: ! 30: ! 31: dem_setup() ! 32: { ! 33: int i; ! 34: int of; ! 35: ! 36: setuid(DAEMUID); ! 37: signal(SIGHUP, SIG_IGN); ! 38: signal(SIGINT, SIG_IGN); ! 39: signal(SIGTERM, unlock); ! 40: #ifndef DEBUG ! 41: signal(SIGQUIT, SIG_IGN); ! 42: /* ! 43: * Close all files, open /dev/null as 0, 1, 2 ! 44: * to assure standard environment ! 45: */ ! 46: freopen("/dev/null", "r", stdin); ! 47: freopen("/dev/null", "w", stdout); ! 48: freopen("/dev/null", "w", stderr); ! 49: for (of=3; of<_NFILE; of++) ! 50: close(of); ! 51: #endif ! 52: if ((of=creat(lock, 0)) < 0) ! 53: exit(0); ! 54: if(fstat(of, &statbuf) < 0 || statbuf.st_mode != 0100000){ ! 55: logerr("Bad lock file %s.", lock); ! 56: exit(1); ! 57: } ! 58: close(of); ! 59: #ifndef DEBUG ! 60: if (i = fork()){ ! 61: if(i == -1){ ! 62: logerr("Unable to fork."); ! 63: unlink(lock); ! 64: exit(1); ! 65: } ! 66: exit(0); ! 67: } ! 68: #endif ! 69: chdir(dpd); ! 70: } ! 71: ! 72: ! 73: #if LPD == 0 ! 74: ! 75: int nlog = 0; ! 76: ! 77: /* VARARGS */ ! 78: logerr(s, a1, a2, a3, a4) ! 79: char *s; ! 80: int a1, a2, a3, a4; ! 81: { ! 82: long static tb; ! 83: register i; ! 84: FILE *f; ! 85: struct passwd *pwp; ! 86: ! 87: if(access(error, WRMODE) != 0) ! 88: return; ! 89: if((f = fopen(error, "a")) == NULL) ! 90: return; ! 91: time(&tb); ! 92: fprintf(f, "%.19s:%s:", ctime(&tb), dname); ! 93: fprintf(f, s, a1, a2, a3, a4); ! 94: #if FGET == 0 ! 95: i = snsum/1000; ! 96: if(i <= 0){ ! 97: i = snsum; ! 98: fprintf(f, " %3d bytes", i); ! 99: } ! 100: else ! 101: fprintf(f, " %3dk bytes", i); ! 102: #endif ! 103: if(!nlog++ && ((pwp = getpwuid(getuid())) != NULL)){ ! 104: putc(' ', f); putc(' ', f); ! 105: i = 0; ! 106: while(pwp->pw_name[i]) ! 107: putc(pwp->pw_name[i++], f); ! 108: } ! 109: putc('\n', f); ! 110: fclose(f); ! 111: } ! 112: ! 113: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.