|
1 /***************************************************************************** |
|
2 * Copyright (C) 2015 |
|
3 * |
|
4 * Michiel Broek <mbroek at mbse dot eu> |
|
5 * |
|
6 * This file is part of the mbsePi-apps |
|
7 * |
|
8 * This is free software; you can redistribute it and/or modify it |
|
9 * under the terms of the GNU General Public License as published by the |
|
10 * Free Software Foundation; either version 2, or (at your option) any |
|
11 * later version. |
|
12 * |
|
13 * mbsePi-apps is distributed in the hope that it will be useful, but |
|
14 * WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 * General Public License for more details. |
|
17 * |
|
18 * You should have received a copy of the GNU General Public License |
|
19 * along with ThermFerm; see the file COPYING. If not, write to the Free |
|
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
|
21 *****************************************************************************/ |
|
22 |
|
23 #include "rdconfig.h" |
|
24 #include "brewco.h" |
|
25 #include "futil.h" |
|
26 #include "xutil.h" |
|
27 |
|
28 |
|
29 int my_shutdown = FALSE; |
|
30 extern int debug; |
|
31 extern sys_config Config; |
|
32 #ifdef HAVE_WIRINGPI_H |
|
33 extern int lcdHandle; |
|
34 #endif |
|
35 |
|
36 |
|
37 void help(void); |
|
38 void die(int); |
|
39 |
|
40 |
|
41 |
|
42 void help(void) |
|
43 { |
|
44 fprintf(stdout, "mbsePi-apps brewco v%s starting\n\n", VERSION); |
|
45 fprintf(stdout, "Usage: brewco [-d] [-h]\n"); |
|
46 fprintf(stdout, " -d --debug Debug and run in foreground\n"); |
|
47 fprintf(stdout, " -h --help Display this help\n"); |
|
48 } |
|
49 |
|
50 |
|
51 |
|
52 void die(int onsig) |
|
53 { |
|
54 switch (onsig) { |
|
55 case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); |
|
56 break; |
|
57 case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); |
|
58 break; |
|
59 case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); |
|
60 break; |
|
61 case SIGSEGV: syslog(LOG_NOTICE, "Got SIGSEGV, shutting down"); |
|
62 my_shutdown = TRUE; |
|
63 exit(SIGSEGV); |
|
64 break; |
|
65 default: syslog(LOG_NOTICE, "die() on signal %d", onsig); |
|
66 } |
|
67 |
|
68 my_shutdown = TRUE; |
|
69 } |
|
70 |
|
71 |
|
72 |
|
73 int main(int argc, char *argv[]) |
|
74 { |
|
75 int rc = 0, c, i; |
|
76 |
|
77 while (1) { |
|
78 int option_index = 0; |
|
79 static struct option long_options[] = { |
|
80 {"debug", 0, 0, 'c'}, |
|
81 {"help", 0, 0, 'h'}, |
|
82 {0, 0, 0, 0} |
|
83 }; |
|
84 |
|
85 c = getopt_long(argc, argv, "dh", long_options, &option_index); |
|
86 if (c == -1) |
|
87 break; |
|
88 |
|
89 switch (c) { |
|
90 case 'd': debug = TRUE; |
|
91 break; |
|
92 case 'h': help(); |
|
93 return 1; |
|
94 } |
|
95 } |
|
96 |
|
97 openlog("brewco", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); |
|
98 syslog(LOG_NOTICE, "mbsePi-apps brewco v%s starting", VERSION); |
|
99 if (debug) |
|
100 fprintf(stdout, "mbsePi-apps brewco v%s starting\n", VERSION); |
|
101 |
|
102 if (rdconfig()) { |
|
103 fprintf(stderr, "Error reading configuration\n"); |
|
104 syslog(LOG_NOTICE, "Error reading configuration: halted"); |
|
105 return 1; |
|
106 } |
|
107 |
|
108 /* |
|
109 * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored |
|
110 * but that's live. This daemon should only be stopped by SIGTERM. |
|
111 * Don't catch SIGCHLD. |
|
112 */ |
|
113 for (i = 0; i < NSIG; i++) { |
|
114 if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) |
|
115 signal(i, (void (*))die); |
|
116 } |
|
117 |
|
118 #ifdef HAVE_WIRINGPI_H |
|
119 if (wiringPiSetup () ) |
|
120 return 1; |
|
121 |
|
122 if ((rc = initLCD (Config.lcd_cols, Config.lcd_rows))) { |
|
123 fprintf(stderr, "Cannot initialize LCD display, rc=%d\n", rc); |
|
124 return 1; |
|
125 } |
|
126 #endif |
|
127 |
|
128 |
|
129 syslog(LOG_NOTICE, "Finished, rc=%d", rc); |
|
130 return rc; |
|
131 } |
|
132 |
|
133 |
|
134 |