Tue, 22 Apr 2014 17:26:30 +0200
Basic package is complete
1 | 1 | /***************************************************************************** |
2 | * Copyright (C) 2014 | |
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 EC-65K; 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 <stdio.h> | |
24 | #include <stdlib.h> | |
25 | #include <unistd.h> | |
3 | 26 | #include <getopt.h> |
27 | #include <sys/types.h> | |
28 | #include <signal.h> | |
1 | 29 | |
30 | ||
31 | #include "main.h" | |
32 | ||
33 | ||
34 | void help(void) | |
35 | { | |
36 | fprintf(stdout, "Usage: thermomeneters [-d] [-h]\n"); | |
37 | fprintf(stdout, " -d --debug Debug on\n"); | |
38 | fprintf(stdout, " -h --help Display this help\n"); | |
39 | } | |
40 | ||
41 | ||
42 | ||
43 | void die(int onsig) | |
44 | { | |
45 | switch (onsig) { | |
3 | 46 | case SIGHUP: fprintf(stdout, "[main] Hangup detected"); |
1 | 47 | break; |
3 | 48 | case SIGINT: fprintf(stdout, "[main] Interrupt from keyboard"); |
1 | 49 | break; |
3 | 50 | case SIGTERM: fprintf(stdout, "[main] Termination signal received"); |
1 | 51 | break; |
3 | 52 | default: fprintf(stdout, "[main] die on signal %d", onsig); |
1 | 53 | } |
54 | ||
55 | exit(onsig); | |
56 | } | |
57 | ||
58 | ||
59 | ||
60 | int main(int argc, char *argv[]) | |
61 | { | |
3 | 62 | int i, c; |
1 | 63 | |
64 | while (1) { | |
65 | int option_index = 0; | |
66 | static struct option long_options[] = { | |
67 | {"debug", 0, 0, 'c'}, | |
68 | {"help", 0, 0, 'h'}, | |
69 | {0, 0, 0, 0} | |
70 | }; | |
71 | ||
72 | c = getopt_long(argc, argv, "dh", long_options, &option_index); | |
73 | if (c == -1) | |
74 | break; | |
75 | ||
76 | switch (c) { | |
77 | case 'd': | |
78 | break; | |
79 | case 'h': help(); | |
80 | return 1; | |
81 | } | |
82 | } | |
83 | ||
84 | /* | |
85 | * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored | |
86 | * but that's live. This daemon should only be stopped by SIGTERM. | |
87 | * Don't catch SIGCHLD. | |
88 | */ | |
89 | for (i = 0; i < NSIG; i++) { | |
90 | if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) | |
91 | signal(i, (void (*))die); | |
92 | } | |
93 | ||
94 | ||
95 | return 0; | |
96 | } | |
97 |