--- Makefile.in +++ Makefile.in 2006-02-07 16:29:20.000000000 +0100 @@ -28,7 +28,7 @@ distclean: clean detectups: detectups.c - $(CC) $(LFLAGS) $(LIBS) detectups.c -o detectups + $(CC) $(LFLAGS) $(CFLAGS) $(LIBS) detectups.c -o detectups powerd: powerd.h config.h $(OBJS) $(CC) $(LFLAGS) $(LIBS) $(OBJS) -o $@ --- config.h +++ config.h 2006-02-07 16:31:03.000000000 +0100 @@ -8,7 +8,12 @@ /* You shouldnt (and it wont work unless you have modified init) change * this file - this is what init uses to know the power status. */ -#define PWRSTAT "/etc/powerstatus" +#define PWRSTAT "/var/run/powerstatus" +#if defined(linux) +# include +# define NEWINIT +# define INIT_REQ_SLEEP 5 +#endif /* Define this if you want to develop on powerd. This will cause powerd to --- powerd.c +++ powerd.c 2008-01-10 10:48:25.967567115 +0100 @@ -27,7 +27,6 @@ #include "powerd.h" - static Client *clients; static Listen *listens; @@ -73,7 +72,10 @@ int main(int argc, char **argv) int i; #endif char *me=argv[0]; + char *base = basename(me); + char *pidf = NULL; int delay = 16; + int n; if (argc<1 || (argc >1 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h")))) { printf("powerd %s by: James Brents (DaSyonic)\n\n", ver); @@ -88,14 +90,42 @@ int main(int argc, char **argv) #ifndef DEBUG switch(i=fork()) { case 0: /* Child */ - chdir("/"); + if (chdir("/") < 0) + fprintf(stderr, "%s: %s\n", me, strerror(errno)); setsid(); if ((root = getuid())) { - fprintf(stderr, "%s: need to have root privlidges.", me); + fprintf(stderr, "%s: need to have root privileges\n", me); } else if (touser != NULL) { if (seteuid(user = uname2id(touser)) == -1) - fprintf(stderr, "%s is not a valid username.", touser); + fprintf(stderr, "%s is not a valid username: %s\n", touser, strerror(errno)); + } + + if ((pidf = (char*)malloc(strlen(_PATH_VARRUN)+strlen(base)+1+3+1)) == NULL) { + fprintf(stderr, "%s: can create pid file: %s\n", me, strerror(errno)); + } else { + pidf = strcpy(pidf, _PATH_VARRUN); + pidf = strcat(pidf, base); + pidf = strcat(pidf, ".pid"); + + if ((n = open(pidf, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) { + fprintf(stderr, "%s: can create pid file: %s\n", me, strerror(errno)); + } else { + snprintf(logbuffer, sizeof(logbuffer)-1, "%d", (int)getpid()); + if (write(n, logbuffer, strlen(logbuffer)) < 0) + fprintf(stderr, "%s: %s\n", me, strerror(errno)); + close(n); + } + } + + if ((n = open("/dev/null", O_RDWR)) == -1) { + fprintf(stderr, "%s: can not open /dev/null.", me); + } else { + (void)dup2(n, 0); + (void)dup2(n, 1); + (void)dup2(n, 2); + (void)close(n); + errno=0; } break; @@ -116,6 +146,8 @@ int main(int argc, char **argv) monitorups(delay); else if (mode == 1) bindport(); + if (pidf) + (void)unlink(pidf); exit(0); } @@ -194,9 +226,12 @@ void monitorups(int delay) { /* log to the syslog */ void log(int type, int facility, const char *entry, ...) { + va_list ap; + va_start(ap, entry); openlog("powerd", LOG_CONS | LOG_PID, facility); - syslog(type, entry); + vsyslog(type, entry, ap); closelog(); + va_end(ap); } /* Borrowed from Apache @@ -220,12 +255,39 @@ uid_t uname2id(char *name) { */ void notifyinit(int status) { unsigned int file; +#ifdef NEWINIT + struct init_request req; + int success = 0; + void (*save_sigalrm)(); +#endif errno = 0; if (touser != NULL) seteuid(root); +#ifdef NEWINIT + /* Fill out the request struct. */ + memset(&req, 0, sizeof(req)); + req.magic = INIT_MAGIC; + req.sleeptime = INIT_REQ_SLEEP; + req.cmd = (status == FAIL) ? INIT_CMD_POWERFAIL : INIT_CMD_POWEROK; + + /* Open the fifo (with timeout) */ + save_sigalrm = signal(SIGALRM, alarm_handler); + alarm(3); + if ((file = open(INIT_FIFO, O_WRONLY, 0644)) >= 0) { + if (write(file, &req, sizeof(req)) == sizeof(req)) + success = 1; + close(file); + } + alarm(0); + (void)signal(SIGALRM, save_sigalrm); + + if (!success) { + /* The old method ... */ +#endif + if ((file = open(PWRSTAT, O_CREAT | O_WRONLY, 0644)) == -1) { fprintf(stderr, "Cant open %s: %s\n", PWRSTAT, strerror(errno)); exit(-1); @@ -237,6 +299,10 @@ void notifyinit(int status) { #else bsdshutdown(status); #endif + +#ifdef NEWINIT + } +#endif if (touser != NULL) seteuid(user); @@ -259,7 +325,8 @@ void notifyinit(int status) { * connections */ int bindport() { - int len, addr_len, newfd, sin_size, numbytes; + socklen_t len, sin_size; + int newfd, numbytes; struct sockaddr_in serveraddr, remoteaddr; struct timeval mytv; FileDescriptor *list; @@ -289,7 +356,7 @@ int bindport() { exit(-1); } - len = sizeof(serveraddr); + len = (socklen_t)sizeof(serveraddr); if (getsockname(serverfd, (struct sockaddr *)&serveraddr, &len)) { fprintf(stderr, "getsockname()\n"); @@ -301,8 +368,6 @@ int bindport() { if ((touser != NULL) && (listenport < 1024)) seteuid(user); - addr_len = sizeof(struct sockaddr); - // printf("Im here\n"); while (1) { errno = 0; @@ -321,7 +386,7 @@ int bindport() { if (FD_ISSET(serverfd, &readfds)) { FileDescriptor *tmp; - sin_size = sizeof(struct sockaddr_in); + sin_size = (socklen_t)sizeof(struct sockaddr_in); if ((newfd=accept(serverfd, (struct sockaddr *)&remoteaddr, &sin_size)) == -1) { perror("accept"); @@ -540,6 +605,15 @@ void debughelp(int sig) mypower = (mypower == 1) ? 0 : 1; } +#ifdef NEWINIT +/* alarm handler for communication with init */ +/* static */ void alarm_handler (int sig) +{ + printf("Writing to init FIFO %s timed out\n", INIT_FIFO); + sleep(1); +} +#endif + /* Open and parse the configuration file */ void openconfig(int *delay) { --- powerd.h.in +++ powerd.h.in 2008-01-10 10:46:28.348626572 +0100 @@ -60,6 +60,7 @@ /*********** If you do, Please send patches. **********/ #include #include +#include #include #include #include @@ -67,6 +68,7 @@ #include #include #include +#include #include #include #include @@ -77,6 +79,11 @@ #include #include #include +#include +#include +#ifndef _PATH_VARRUN +# define _PATH_VARRUN "/var/run/" +#endif #define CONNECTED 0x01 #define SENTNOTICE 0x02 @@ -122,7 +129,8 @@ struct filedescriptor_ { Listen *listen; }; -void log(int type, int facility, const char *entry, ...); +void mylog(int type, int facility, const char *entry, ...); +#define log(args...) mylog(args) uid_t uname2id(char *name); void notifyinit(int status); void monitorups(int delay); @@ -139,6 +147,7 @@ int vhost(FileDescriptor *fd, char *para void auth(FileDescriptor *fd, char *password); void quit_sig(int sig); void debughelp(int sig); +/* static */ void alarm_handler(int sig); int notifyclients(int status); void getrid(FileDescriptor *fd, int i); void bsdshutdown(int status);