1994-10-22 09:53:33 +00:00
|
|
|
/*-
|
1995-04-18 01:54:25 +00:00
|
|
|
* Copyright (c) 1992, 1993, 1995 Eugene W. Stark
|
1994-10-22 09:53:33 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Eugene W. Stark.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY EUGENE W. STARK (THE AUTHOR) ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-12-04 07:25:19 +00:00
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
1999-08-28 00:22:10 +00:00
|
|
|
"$FreeBSD$";
|
1997-12-04 07:25:19 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1994-10-22 09:53:33 +00:00
|
|
|
/*
|
|
|
|
* xtend - X-10 daemon
|
|
|
|
* Eugene W. Stark (stark@cs.sunysb.edu)
|
|
|
|
* January 14, 1993
|
|
|
|
*/
|
|
|
|
|
1997-12-04 07:25:19 +00:00
|
|
|
#include <err.h>
|
|
|
|
#include <pwd.h>
|
1994-10-22 09:53:33 +00:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <signal.h>
|
1997-12-04 07:25:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
1994-10-22 09:53:33 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
1995-04-18 01:54:25 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/errno.h>
|
1994-10-22 09:53:33 +00:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
1997-12-04 07:25:19 +00:00
|
|
|
#include <grp.h>
|
1994-10-22 09:53:33 +00:00
|
|
|
|
|
|
|
#include "xtend.h"
|
|
|
|
#include "xten.h"
|
|
|
|
#include "paths.h"
|
|
|
|
|
|
|
|
FILE *Log; /* Log file */
|
|
|
|
FILE *User; /* User connection */
|
|
|
|
STATUS Status[16][16]; /* Device status table */
|
|
|
|
int status; /* Status file descriptor */
|
|
|
|
int tw523; /* tw523 controller */
|
|
|
|
int sock; /* socket for user */
|
|
|
|
jmp_buf mainloop; /* longjmp point after SIGHUP */
|
|
|
|
void onhup(); /* SIGHUP handler */
|
|
|
|
void onterm(); /* SIGTERM handler */
|
|
|
|
void onpipe(); /* SIGPIPE handler */
|
|
|
|
|
1997-12-04 07:25:19 +00:00
|
|
|
void checkpoint_status __P((void));
|
|
|
|
void initstatus __P((void));
|
|
|
|
void logpacket __P((unsigned char *));
|
|
|
|
void processpacket __P((unsigned char *));
|
|
|
|
int user_command __P((void));
|
|
|
|
|
|
|
|
int
|
1994-10-22 09:53:33 +00:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
|
|
|
char *argv[];
|
|
|
|
{
|
|
|
|
char *twpath = TWPATH;
|
|
|
|
char *sockpath = SOCKPATH;
|
|
|
|
char logpath[MAXPATHLEN+1];
|
|
|
|
char statpath[MAXPATHLEN+1];
|
|
|
|
struct sockaddr_un sa;
|
|
|
|
struct timeval tv;
|
1995-04-18 01:54:25 +00:00
|
|
|
struct passwd *pw;
|
|
|
|
struct group *gr;
|
|
|
|
struct stat sb;
|
1994-10-22 09:53:33 +00:00
|
|
|
int user;
|
|
|
|
FILE *pidf;
|
|
|
|
|
|
|
|
/*
|
1995-04-18 01:54:25 +00:00
|
|
|
* Make sure we start out running as root
|
|
|
|
*/
|
1997-12-04 07:25:19 +00:00
|
|
|
if(geteuid() != 0)
|
|
|
|
errx(1, "you must be root");
|
1995-04-18 01:54:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out what UID/GID we are to run as
|
|
|
|
*/
|
1997-12-04 07:25:19 +00:00
|
|
|
if((pw = getpwnam(XTENUNAME)) == NULL)
|
|
|
|
errx(1, "no such user '%s'", XTENUNAME);
|
|
|
|
if((gr = getgrnam(XTENGNAME)) == NULL)
|
|
|
|
errx(1, "no such group '%s'", XTENGNAME);
|
1995-04-18 01:54:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the log file before doing anything else
|
1994-10-22 09:53:33 +00:00
|
|
|
*/
|
|
|
|
strcpy(logpath, X10DIR);
|
1995-04-18 01:54:25 +00:00
|
|
|
if(stat(logpath, &sb) == -1 && errno == ENOENT) {
|
|
|
|
if(mkdir(logpath, 0755) != -1) {
|
|
|
|
chown(logpath, pw->pw_uid, gr->gr_gid);
|
|
|
|
} else {
|
1997-12-04 07:25:19 +00:00
|
|
|
errx(1, "can't create directory '%s'", logpath);
|
1995-04-18 01:54:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
strcat(logpath, "/");
|
1994-10-22 09:53:33 +00:00
|
|
|
strcat(logpath, X10LOGNAME);
|
1997-12-04 07:25:19 +00:00
|
|
|
if((Log = fopen(logpath, "a")) == NULL)
|
|
|
|
errx(1, "can't open log file '%s'", logpath);
|
1995-04-18 01:54:25 +00:00
|
|
|
chown(logpath, pw->pw_uid, gr->gr_gid);
|
1994-10-22 09:53:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Become a daemon
|
|
|
|
*/
|
|
|
|
if(daemon(0, 0) == -1) {
|
1995-04-18 01:54:25 +00:00
|
|
|
fprintf(Log, "%s: Unable to become a daemon\n", thedate());
|
|
|
|
fclose(Log);
|
1994-10-22 09:53:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
1995-04-18 01:54:25 +00:00
|
|
|
fprintf(Log, "%s: %s [%d] started\n", thedate(), argv[0], getpid());
|
1994-10-22 09:53:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get ahold of the TW523 device
|
|
|
|
*/
|
|
|
|
if((tw523 = open(twpath, O_RDWR)) < 0) {
|
|
|
|
fprintf(Log, "%s: Can't open %s\n", thedate(), twpath);
|
1995-04-18 01:54:25 +00:00
|
|
|
fclose(Log);
|
1994-10-22 09:53:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fprintf(Log, "%s: %s successfully opened\n", thedate(), twpath);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put our pid in a file so we can be signalled by shell scripts
|
|
|
|
*/
|
|
|
|
if((pidf = fopen(PIDPATH, "w")) == NULL) {
|
|
|
|
fprintf(Log, "%s: Error writing pid file: %s\n", thedate(), PIDPATH);
|
1995-04-18 01:54:25 +00:00
|
|
|
fclose(Log);
|
1994-10-22 09:53:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fprintf(pidf, "%d\n", getpid());
|
|
|
|
fclose(pidf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up socket to accept user commands
|
|
|
|
*/
|
|
|
|
if((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
|
|
|
|
fprintf(Log, "%s: Can't create socket\n", thedate());
|
1995-04-18 01:54:25 +00:00
|
|
|
fclose(Log);
|
1994-10-22 09:53:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
strcpy(sa.sun_path, sockpath);
|
|
|
|
sa.sun_family = AF_UNIX;
|
|
|
|
unlink(sockpath);
|
|
|
|
if(bind(sock, (struct sockaddr *)(&sa), strlen(sa.sun_path) + 2) < 0) {
|
|
|
|
fprintf(Log, "%s: Can't bind socket to %s\n", thedate(), sockpath);
|
1995-04-18 01:54:25 +00:00
|
|
|
fclose(Log);
|
1994-10-22 09:53:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(listen(sock, 5) < 0) {
|
|
|
|
fprintf(Log, "%s: Can't listen on socket\n", thedate());
|
1995-04-18 01:54:25 +00:00
|
|
|
fclose(Log);
|
1994-10-22 09:53:33 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1995-04-18 01:54:25 +00:00
|
|
|
/*
|
|
|
|
* Set proper ownership and permissions on the socket
|
|
|
|
*/
|
|
|
|
if(chown(sockpath, pw->pw_uid, gr->gr_gid) == -1 ||
|
|
|
|
chmod(sockpath, 0660) == -1) {
|
|
|
|
fprintf(Log, "%s: Can't set owner/permissions on socket\n", thedate());
|
|
|
|
fclose(Log);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give up root privileges
|
|
|
|
*/
|
|
|
|
setgid(pw->pw_gid);
|
|
|
|
setuid(pw->pw_uid);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the status table
|
|
|
|
*/
|
|
|
|
strcpy(statpath, X10DIR);
|
|
|
|
strcat(statpath, "/");
|
|
|
|
strcat(statpath, X10STATNAME);
|
|
|
|
if((status = open(statpath, O_RDWR)) < 0) {
|
|
|
|
if((status = open(statpath, O_RDWR | O_CREAT, 0666)) < 0) {
|
|
|
|
fprintf(Log, "%s: Can't open %s\n", thedate(), statpath);
|
|
|
|
fclose(Log);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if(write(status, Status, 16 * 16 * sizeof(STATUS))
|
|
|
|
!= 16 * 16 * sizeof(STATUS)) {
|
|
|
|
fprintf(Log, "%s: Error initializing status file\n", thedate());
|
|
|
|
fclose(Log);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
initstatus();
|
|
|
|
|
1994-10-22 09:53:33 +00:00
|
|
|
/*
|
|
|
|
* Return here on SIGHUP after closing and reopening log file.
|
|
|
|
* Also on SIGPIPE after closing user connection.
|
|
|
|
*/
|
1995-04-18 01:54:25 +00:00
|
|
|
signal(SIGTERM, onterm);
|
|
|
|
signal(SIGHUP, onhup);
|
|
|
|
signal(SIGPIPE, onpipe);
|
1994-10-22 09:53:33 +00:00
|
|
|
setjmp(mainloop);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now start the main processing loop.
|
|
|
|
*/
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 250000;
|
|
|
|
while(1) {
|
|
|
|
fd_set fs;
|
|
|
|
unsigned char rpkt[3];
|
|
|
|
int sel, h, k;
|
|
|
|
STATUS *s;
|
|
|
|
|
|
|
|
FD_ZERO(&fs);
|
|
|
|
FD_SET(tw523, &fs);
|
|
|
|
if(User != NULL) FD_SET(user, &fs);
|
|
|
|
else FD_SET(sock, &fs);
|
|
|
|
sel = select(FD_SETSIZE, &fs, 0, 0, &tv);
|
|
|
|
if(sel == 0) {
|
|
|
|
/*
|
|
|
|
* Cancel brightening and dimming on ALL units on ALL house codes,
|
|
|
|
* because the fact that we haven't gotten a packet for awhile means
|
|
|
|
* that there was a gap in transmission.
|
|
|
|
*/
|
|
|
|
for(h = 0; h < 16; h++) {
|
|
|
|
for(k = 0; k < 16; k++) {
|
|
|
|
s = &Status[h][k];
|
|
|
|
if(s->selected == BRIGHTENING || s->selected == DIMMING) {
|
|
|
|
s->selected = IDLE;
|
|
|
|
s->lastchange = time(NULL);
|
|
|
|
s->changed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fflush(Log);
|
|
|
|
checkpoint_status();
|
|
|
|
/*
|
|
|
|
* Now that we've done this stuff, we'll set the timeout a little
|
|
|
|
* longer, so we don't keep looping too frequently.
|
|
|
|
*/
|
|
|
|
tv.tv_sec = 60;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* While there is stuff happening, we keep a short timeout, so we
|
|
|
|
* don't get stuck for some unknown reason, and so we can keep the
|
|
|
|
* brightening and dimming data up-to-date.
|
|
|
|
*/
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 250000;
|
|
|
|
if(FD_ISSET(tw523, &fs)) { /* X10 data arriving from TW523 */
|
|
|
|
if(read(tw523, rpkt, 3) < 3) {
|
|
|
|
fprintf(Log, "%s: Error reading from TW523\n", thedate());
|
|
|
|
} else {
|
|
|
|
logpacket(rpkt);
|
|
|
|
processpacket(rpkt);
|
|
|
|
}
|
1999-08-08 07:05:46 +00:00
|
|
|
} else if(FD_ISSET(user, &fs) && User != NULL) {
|
1999-08-08 07:23:12 +00:00
|
|
|
if(user_command()) {
|
|
|
|
fprintf(Log, "%s: Closing user connection\n", thedate());
|
|
|
|
fclose(User);
|
|
|
|
User = NULL;
|
1994-10-22 09:53:33 +00:00
|
|
|
}
|
|
|
|
} else if(FD_ISSET(sock, &fs)) { /* Accept a connection */
|
|
|
|
if (User == NULL) {
|
|
|
|
int len = sizeof(struct sockaddr_un);
|
|
|
|
if((user = accept(sock, (struct sockaddr *)(&sa), &len)) >= 0) {
|
|
|
|
fprintf(Log, "%s: Accepting user connection\n", thedate());
|
|
|
|
if((User = fdopen(user, "w+")) == NULL) {
|
|
|
|
fprintf(Log, "%s: Can't attach socket to stream\n", thedate());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(Log, "%s: Failure in attempt to accept connection\n", thedate());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* "Can't happen */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Not reached */
|
|
|
|
}
|
|
|
|
|
|
|
|
char *thedate()
|
|
|
|
{
|
|
|
|
char *cp, *cp1;
|
|
|
|
time_t tod;
|
|
|
|
|
|
|
|
tod = time(NULL);
|
|
|
|
cp = cp1 = ctime(&tod);
|
|
|
|
while(*cp1 != '\n') cp1++;
|
|
|
|
*cp1 = '\0';
|
|
|
|
return(cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When SIGHUP received, close and reopen the Log file
|
|
|
|
*/
|
|
|
|
|
|
|
|
void onhup()
|
|
|
|
{
|
|
|
|
char logpath[MAXPATHLEN+1];
|
|
|
|
|
|
|
|
fprintf(Log, "%s: SIGHUP received, reopening Log\n", thedate());
|
|
|
|
fclose(Log);
|
|
|
|
strcpy(logpath, X10DIR);
|
1995-04-18 01:54:25 +00:00
|
|
|
strcat(logpath, "/");
|
1994-10-22 09:53:33 +00:00
|
|
|
strcat(logpath, X10LOGNAME);
|
1997-12-04 07:25:19 +00:00
|
|
|
if((Log = fopen(logpath, "a")) == NULL)
|
|
|
|
errx(1, "can't open log file '%s'", logpath);
|
1994-10-22 09:53:33 +00:00
|
|
|
longjmp(mainloop, 1);
|
|
|
|
/* No return */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When SIGTERM received, just exit normally
|
|
|
|
*/
|
|
|
|
|
|
|
|
void onterm()
|
|
|
|
{
|
|
|
|
fprintf(Log, "%s: SIGTERM received, shutting down\n", thedate());
|
1995-04-18 01:54:25 +00:00
|
|
|
fclose(Log);
|
1994-10-22 09:53:33 +00:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* When SIGPIPE received, reset user connection
|
|
|
|
*/
|
|
|
|
|
|
|
|
void onpipe()
|
|
|
|
{
|
|
|
|
fprintf(Log, "%s: SIGPIPE received, resetting user connection\n",
|
|
|
|
thedate());
|
|
|
|
if(User != NULL) {
|
|
|
|
fclose(User);
|
|
|
|
User = NULL;
|
|
|
|
}
|
|
|
|
longjmp(mainloop, 1);
|
|
|
|
/* No return */
|
|
|
|
}
|