2006-12-14 18:06:00 +01:00
|
|
|
--- Makefile.in
|
|
|
|
+++ Makefile.in 2006-02-07 16:29:20.000000000 +0100
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -28,7 +28,7 @@ distclean: clean
|
2006-12-14 18:06:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
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 <initreq.h>
|
|
|
|
+# define NEWINIT
|
|
|
|
+# define INIT_REQ_SLEEP 5
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* Define this if you want to develop on powerd. This will cause powerd to
|
|
|
|
--- powerd.c
|
2008-01-10 15:29:00 +01:00
|
|
|
+++ powerd.c 2008-01-10 10:48:25.967567115 +0100
|
2006-12-14 18:06:00 +01:00
|
|
|
@@ -27,7 +27,6 @@
|
|
|
|
|
|
|
|
#include "powerd.h"
|
|
|
|
|
|
|
|
-
|
|
|
|
static Client *clients;
|
|
|
|
static Listen *listens;
|
|
|
|
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -73,7 +72,10 @@ int main(int argc, char **argv)
|
2006-12-14 18:06:00 +01:00
|
|
|
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 <james@nistix.com> (DaSyonic)\n\n", ver);
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -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));
|
2006-12-14 18:06:00 +01:00
|
|
|
setsid();
|
|
|
|
|
|
|
|
if ((root = getuid())) {
|
|
|
|
- fprintf(stderr, "%s: need to have root privlidges.", me);
|
2008-01-10 15:29:00 +01:00
|
|
|
+ fprintf(stderr, "%s: need to have root privileges\n", me);
|
2006-12-14 18:06:00 +01:00
|
|
|
} 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");
|
|
|
|
+
|
2008-01-10 15:29:00 +01:00
|
|
|
+ if ((n = open(pidf, O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) {
|
2006-12-14 18:06:00 +01:00
|
|
|
+ fprintf(stderr, "%s: can create pid file: %s\n", me, strerror(errno));
|
|
|
|
+ } else {
|
|
|
|
+ snprintf(logbuffer, sizeof(logbuffer)-1, "%d", (int)getpid());
|
2008-01-10 15:29:00 +01:00
|
|
|
+ if (write(n, logbuffer, strlen(logbuffer)) < 0)
|
|
|
|
+ fprintf(stderr, "%s: %s\n", me, strerror(errno));
|
2006-12-14 18:06:00 +01:00
|
|
|
+ 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;
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -116,6 +146,8 @@ int main(int argc, char **argv)
|
2006-12-14 18:06:00 +01:00
|
|
|
monitorups(delay);
|
|
|
|
else if (mode == 1)
|
|
|
|
bindport();
|
|
|
|
+ if (pidf)
|
|
|
|
+ (void)unlink(pidf);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -194,9 +226,12 @@ void monitorups(int delay) {
|
2006-12-14 18:06:00 +01:00
|
|
|
/* 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
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -220,12 +255,39 @@ uid_t uname2id(char *name) {
|
2006-12-14 18:06:00 +01:00
|
|
|
*/
|
|
|
|
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);
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -237,6 +299,10 @@ void notifyinit(int status) {
|
2006-12-14 18:06:00 +01:00
|
|
|
#else
|
|
|
|
bsdshutdown(status);
|
|
|
|
#endif
|
|
|
|
+
|
|
|
|
+#ifdef NEWINIT
|
|
|
|
+ }
|
|
|
|
+#endif
|
|
|
|
if (touser != NULL)
|
|
|
|
seteuid(user);
|
|
|
|
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -259,7 +325,8 @@ void notifyinit(int status) {
|
2006-12-14 18:06:00 +01:00
|
|
|
* 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;
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -289,7 +356,7 @@ int bindport() {
|
2006-12-14 18:06:00 +01:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
- len = sizeof(serveraddr);
|
|
|
|
+ len = (socklen_t)sizeof(serveraddr);
|
|
|
|
|
|
|
|
if (getsockname(serverfd, (struct sockaddr *)&serveraddr, &len)) {
|
|
|
|
fprintf(stderr, "getsockname()\n");
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -301,8 +368,6 @@ int bindport() {
|
2006-12-14 18:06:00 +01:00
|
|
|
if ((touser != NULL) && (listenport < 1024))
|
|
|
|
seteuid(user);
|
|
|
|
|
|
|
|
- addr_len = sizeof(struct sockaddr);
|
|
|
|
-
|
|
|
|
// printf("Im here\n");
|
|
|
|
while (1) {
|
|
|
|
errno = 0;
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -321,7 +386,7 @@ int bindport() {
|
2006-12-14 18:06:00 +01:00
|
|
|
|
|
|
|
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");
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -540,6 +605,15 @@ void debughelp(int sig)
|
2006-12-14 18:06:00 +01:00
|
|
|
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
|
2008-01-10 15:29:00 +01:00
|
|
|
+++ powerd.h.in 2008-01-10 10:46:28.348626572 +0100
|
2006-12-14 18:06:00 +01:00
|
|
|
@@ -60,6 +60,7 @@
|
|
|
|
/*********** If you do, Please send patches. **********/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
+#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -67,6 +68,7 @@
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
+#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
@@ -77,6 +79,11 @@
|
2006-12-14 18:06:00 +01:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
+#include <libgen.h>
|
|
|
|
+#include <paths.h>
|
|
|
|
+#ifndef _PATH_VARRUN
|
|
|
|
+# define _PATH_VARRUN "/var/run/"
|
|
|
|
+#endif
|
|
|
|
|
|
|
|
#define CONNECTED 0x01
|
|
|
|
#define SENTNOTICE 0x02
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -122,7 +129,8 @@ struct filedescriptor_ {
|
2006-12-14 18:06:00 +01:00
|
|
|
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);
|
2008-01-10 15:29:00 +01:00
|
|
|
@@ -139,6 +147,7 @@ int vhost(FileDescriptor *fd, char *para
|
2006-12-14 18:06:00 +01:00
|
|
|
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);
|