/************************************************************************* * (C) 2011 AITNET - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: pceng.c,v 1.2 2012/07/22 22:46:47 misho Exp $ * ************************************************************************* The ELWIX and AITNET software is distributed under the following terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by Michael Pounov . 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 Michael Pounov ELWIX - Embedded LightWeight unIX and its contributors. 4. Neither the name of AITNET nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``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 REGENTS OR CONTRIBUTORS 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. */ #include "global.h" #include "pceng.h" int Verbose, Kill; extern char compiled[], compiledby[], compilehost[]; cfg_root_t cfg; char szConf[MAXPATHLEN] = DEFAULT_PCENG_CFG; static void Usage() { printf( "-= PCEngines =- events managment tool\n" "=== %s === %s@%s ===\n\n" " Syntax: pceng [options] [led_no[=<0|1>] ...]\n" "\n" "\t-v\t\tVerbose ...\n" "\t-D\t\tRun in background mode ...\n" "\t-c \tConfig events file ...\n" "\n", compiled, compiledby, compilehost); } static void sigHand(int sig) { int stat; switch (sig) { case SIGHUP: cfgUnloadConfig(&cfg); if (cfgLoadConfig(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 main(int argc, char **argv) { char ch, ledno, *pos; int i, ret = 0, mode = 1; struct sigaction sact; #ifdef __FreeBSD__ int io; #endif while ((ch = getopt(argc, argv, "vhDc:")) != -1) switch (ch) { case 'c': strlcpy(szConf, optarg, sizeof szConf); break; case 'D': mode = 0; break; case 'v': Verbose++; break; case 'h': default: Usage(); return 1; } argc -= optind; argv += optind; if (!mode) openlog("pceng", LOG_CONS | LOG_PID, LOG_DAEMON); else openlog("pceng", LOG_CONS | LOG_PID | LOG_PERROR, LOG_USER); #ifdef __FreeBSD__ io = open(_PATH_DEVIO, O_RDONLY); if (io == -1) { printf("Error:: in open dev %s #%d - %s\n", _PATH_DEVIO, errno, strerror(errno)); ret = 2; goto end; } #endif if (cfgLoadConfig(szConf, &cfg)) { printf("Error:: #%d - %s\n", cfg_GetErrno(), cfg_GetError()); ret = 1; goto end; } if (argc) { VERB(1) syslog(LOG_WARNING, "LED client ..."); for (i = 0; !ret && i < argc; pos = NULL, i++) { if ((pos = strchr(argv[i], '='))) { *pos++ = 0; ledno = (char) strtol(argv[i], NULL, 0); ch = (char) strtol(pos, NULL, 0); } else { ledno = (char) strtol(argv[i], NULL, 0); ch = -1; } ret = LED((u_char) ledno, (u_char) ch); printf("%s:: LED#%d=%d\n", (ch == -1) ? "Get" : "Set", ledno, ret); } 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(); end: cfgUnloadConfig(&cfg); #ifdef __FreeBSD__ if (io > 2) close(io); #endif closelog(); return ret; }