version 1.1.2.1, 2011/07/22 13:29:29
|
version 1.1.2.5, 2011/07/22 15:03:30
|
Line 47 SUCH DAMAGE.
|
Line 47 SUCH DAMAGE.
|
#include "pceng.h" |
#include "pceng.h" |
|
|
|
|
int Verbose; | int Verbose, Kill; |
extern char compiled[], compiledby[], compilehost[]; |
extern char compiled[], compiledby[], compilehost[]; |
|
sl_config cfg; |
|
char szConf[MAXPATHLEN] = DEFAULT_PCENG_CFG; |
|
|
|
|
static void |
static void |
Line 56 Usage()
|
Line 58 Usage()
|
{ |
{ |
printf( "-= PCEngines =- events managment tool\n" |
printf( "-= PCEngines =- events managment tool\n" |
"=== %s === %s@%s ===\n\n" |
"=== %s === %s@%s ===\n\n" |
" Syntax: pceng [options]\n" | " Syntax: pceng [options] [[led_no <0|1>] ...]\n" |
"\n" |
"\n" |
"\t-v\t\tVerbose ...\n" |
"\t-v\t\tVerbose ...\n" |
"\t-b\t\tRun in batch mode ...\n" | "\t-D\t\tRun in background mode ...\n" |
"\t-c <conf>\t\tConfig events file ...\n" | "\t-c <conf>\tConfig events file ...\n" |
"\n", compiled, compiledby, compilehost); |
"\n", compiled, compiledby, compilehost); |
} |
} |
|
|
|
static void |
|
sigHand(int sig) |
|
{ |
|
int stat; |
|
|
|
switch (sig) { |
|
case SIGHUP: |
|
UnloadConfig(&cfg); |
|
if (LoadConfig(szConf, &cfg)) { |
|
syslog(LOG_ERR, "Error:: #%d - %s", cfg_GetErrno(), cfg_GetError()); |
|
raise(SIGTERM); |
|
} |
|
VERB(1) syslog(LOG_WARNING, "Reload config %s", szConf); |
|
break; |
|
case SIGTERM: |
|
VERB(1) syslog(LOG_WARNING, "Kill process ..."); |
|
Kill++; |
|
break; |
|
case SIGCHLD: |
|
while (waitpid(-1, &stat, WNOHANG) > 0); |
|
} |
|
} |
|
|
|
|
int |
int |
main(int argc, char **argv) |
main(int argc, char **argv) |
{ |
{ |
char ch, szConf[MAXPATHLEN] = DEFAULT_PCENG_CFG; | char ch, ledno; |
int mode = 0; | int i, io, ret = 0, mode = 1; |
| struct sigaction sact; |
|
|
while ((ch = getopt(argc, argv, "vhbc:")) != -1) | while ((ch = getopt(argc, argv, "vhDc:")) != -1) |
switch (ch) { |
switch (ch) { |
case 'c': |
case 'c': |
strlcpy(szConf, optarg, sizeof szConf); |
strlcpy(szConf, optarg, sizeof szConf); |
break; |
break; |
case 'b': | case 'D': |
mode = 1; | mode = 0; |
break; |
break; |
case 'v': |
case 'v': |
Verbose++; |
Verbose++; |
Line 90 main(int argc, char **argv)
|
Line 116 main(int argc, char **argv)
|
argc -= optind; |
argc -= optind; |
argv += optind; |
argv += optind; |
|
|
return 0; | if (!mode) |
| openlog("pceng", LOG_CONS | LOG_PID, LOG_DAEMON); |
| else |
| openlog("pceng", LOG_CONS | LOG_PID | LOG_PERROR, LOG_USER); |
| |
| io = open(_PATH_DEVIO, O_RDWR); |
| if (io == -1) { |
| printf("Error:: in open dev %s #%d - %s\n", _PATH_DEVIO, errno, strerror(errno)); |
| ret = 2; |
| goto end; |
| } |
| |
| if (argc > 1 && !(argc % 2)) { |
| for (i = 0; !ret && i < argc; i += 2) { |
| ledno = (char) strtol(argv[i], NULL, 0); |
| ch = (char) strtol(argv[i + 1], NULL, 0); |
| ret = LED(io, (u_char) ledno, (u_char) ch); |
| } |
| if (ret) |
| goto end; |
| } |
| |
| if (LoadConfig(szConf, &cfg)) { |
| printf("Error:: #%d - %s\n", cfg_GetErrno(), cfg_GetError()); |
| ret = 1; |
| goto end; |
| } |
| |
| sact.sa_handler = sigHand; |
| sigemptyset(&sact.sa_mask); |
| sigaction(SIGHUP, &sact, NULL); |
| sigaction(SIGTERM, &sact, NULL); |
| sigaction(SIGCHLD, &sact, NULL); |
| |
| if (!mode) |
| switch (fork()) { |
| case -1: |
| printf("Error:: #%d - %s\n", errno, strerror(errno)); |
| ret = 1; |
| goto end; |
| case 0: |
| VERB(1) printf("Welcome into darkness ...\n"); |
| |
| setsid(); |
| chdir("/"); |
| |
| mode = open(_PATH_DEVNULL, O_RDWR); |
| if (mode > 2) { |
| dup2(mode, STDIN_FILENO); |
| dup2(mode, STDOUT_FILENO); |
| dup2(mode, STDERR_FILENO); |
| close(mode); |
| } |
| break; |
| default: |
| VERB(1) printf("PCENG Going to shadow land ...\n"); |
| goto end; |
| } |
| |
| ret = Run(io); |
| |
| end: |
| UnloadConfig(&cfg); |
| if (io > 2) |
| close(io); |
| closelog(); |
| return ret; |
} |
} |