Sun, 23 May 2021 16:41:35 +0200
Fermentables, hops, miscs and yeast now have tests against the added moment with the brewing stage. Added to inventory edit rows, delete rows, and pick choices for the moment to add or edit. Some more popups to explain certain blocks.
0 | 1 | /***************************************************************************** |
745
3addb8cfcc3e
More and better error loging during daemon startup
Michiel Broek <mbroek@mbse.eu>
parents:
680
diff
changeset
|
2 | * Copyright (C) 2017-2021 |
0 | 3 | * |
4 | * Michiel Broek <mbroek at mbse dot eu> | |
5 | * | |
6 | * This file is part of the bms (Brewery Management System) | |
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 | * bms 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 "bms.h" | |
24 | #include "xutil.h" | |
194
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
25 | #include "futil.h" |
0 | 26 | #include "rdconfig.h" |
27 | #include "lock.h" | |
28 | #include "mqtt.h" | |
29 | #include "mysql.h" | |
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
30 | #include "nodes.h" |
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
31 | #include "websocket.h" |
0 | 32 | |
33 | ||
747
b6fbe6821468
Replace TRUE and FALSE for lowercase version.
Michiel Broek <mbroek@mbse.eu>
parents:
746
diff
changeset
|
34 | int my_shutdown = false; |
b6fbe6821468
Replace TRUE and FALSE for lowercase version.
Michiel Broek <mbroek@mbse.eu>
parents:
746
diff
changeset
|
35 | int debug = false; |
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
36 | static pid_t mypid; |
0 | 37 | char *Private_Path = NULL; /* Users data path */ |
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
38 | pthread_t ws_thread; |
0 | 39 | |
194
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
40 | extern sys_config Config; |
0 | 41 | |
42 | ||
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
43 | |
0 | 44 | void help(void) |
45 | { | |
46 | fprintf(stdout, "bmsd v%s starting\n\n", VERSION); | |
47 | fprintf(stdout, "Usage: bmsd [-d] [-h]\n"); | |
48 | fprintf(stdout, " -d --debug Debug and run in foreground\n"); | |
49 | fprintf(stdout, " -h --help Display this help\n"); | |
50 | } | |
51 | ||
52 | ||
53 | ||
54 | void die(int onsig) | |
55 | { | |
56 | switch (onsig) { | |
57 | case SIGHUP: syslog(LOG_NOTICE, "Got SIGHUP, shutting down"); | |
58 | break; | |
59 | case SIGINT: syslog(LOG_NOTICE, "Keyboard interrupt, shutting down"); | |
60 | break; | |
61 | case SIGTERM: syslog(LOG_NOTICE, "Got SIGTERM, shutting down"); | |
62 | break; | |
63 | case SIGSEGV: syslog(LOG_NOTICE, "Got SIGSEGV, shutting down"); | |
747
b6fbe6821468
Replace TRUE and FALSE for lowercase version.
Michiel Broek <mbroek@mbse.eu>
parents:
746
diff
changeset
|
64 | my_shutdown = true; |
0 | 65 | exit(SIGSEGV); |
66 | break; | |
67 | default: syslog(LOG_NOTICE, "die() on signal %d", onsig); | |
68 | } | |
747
b6fbe6821468
Replace TRUE and FALSE for lowercase version.
Michiel Broek <mbroek@mbse.eu>
parents:
746
diff
changeset
|
69 | my_shutdown = true; |
0 | 70 | } |
71 | ||
72 | ||
73 | ||
746
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
74 | /** |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
75 | * @brief Drop privileges in a safe way. |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
76 | * @return 0 on success and -1 on failure. |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
77 | */ |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
78 | int drop_root_privileges(uid_t pw_uid, gid_t pw_gid, char *pw_dir) |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
79 | { |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
80 | // no need to "drop" the privileges that you don't have in the first place! |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
81 | if (getuid() == pw_uid && getgid() == pw_gid) { |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
82 | syslog(LOG_NOTICE, "No need to drop privileges"); |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
83 | } else { |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
84 | if (setgid(pw_gid) != 0) { |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
85 | syslog(LOG_NOTICE, "setgid: %s", strerror(errno)); |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
86 | return -1; |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
87 | } |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
88 | if (setuid(pw_uid) != 0) { |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
89 | syslog(LOG_NOTICE, "setgid: %s", strerror(errno)); |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
90 | return -1; |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
91 | } |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
92 | } |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
93 | |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
94 | /* Change to the home directory */ |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
95 | if (chdir(pw_dir) != 0) { |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
96 | syslog(LOG_NOTICE, "chdir(%s): %s", pw_dir, strerror(errno)); |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
97 | return -1; |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
98 | } |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
99 | |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
100 | /* check if we successfully dropped the root privileges */ |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
101 | if (setuid(0) == 0 || seteuid(0) == 0) { |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
102 | syslog(LOG_NOTICE, "could not drop root privileges!"); |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
103 | return -1; |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
104 | } |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
105 | |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
106 | syslog(LOG_NOTICE, "Privileges dropped to %d:%d", pw_uid, pw_gid); |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
107 | return 0; |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
108 | } |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
109 | |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
110 | |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
111 | |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
112 | int server(void) |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
113 | { |
0 | 114 | struct passwd *mypwd; |
115 | int rc = 0; | |
194
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
116 | char *tmppath = NULL; |
0 | 117 | |
746
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
118 | mypwd = getpwnam("brewery"); |
0 | 119 | if (mypwd == NULL) { |
120 | fprintf(stderr, "[main] Could not find passwd entry\n"); | |
121 | return 1; | |
122 | } | |
123 | ||
124 | Private_Path = xstrcpy(mypwd->pw_dir); | |
125 | Private_Path = xstrcat(Private_Path, (char *)"/.bms"); | |
126 | ||
746
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
127 | if (drop_root_privileges(mypwd->pw_uid, mypwd->pw_gid, mypwd->pw_dir) < 0) { |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
128 | syslog(LOG_NOTICE, "Can't drop privileges"); |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
129 | return 1; |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
130 | } |
44d929ff268e
Can start the daemon as root, and it will drop privileges.
Michiel Broek <mbroek@mbse.eu>
parents:
745
diff
changeset
|
131 | |
0 | 132 | if (lockprog((char *)"bmsd")) { |
133 | syslog(LOG_NOTICE, "Can't lock"); | |
134 | return 1; | |
135 | } | |
136 | ||
137 | if (rdconfig()) { | |
138 | rc = 2; | |
139 | goto endit1; | |
140 | } | |
141 | ||
194
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
142 | if (! Config.web_root) { |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
143 | rc = 5; |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
144 | syslog(LOG_NOTICE, "Configuration `web_root' is not set"); |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
145 | goto endit1; |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
146 | } |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
147 | |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
148 | /* |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
149 | * Make sure the needed log paths exist. |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
150 | */ |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
151 | tmppath = xstrcpy(Config.web_root); |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
152 | tmppath = xstrcat(tmppath, (char *)"/log/fermentation/"); |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
153 | if (! mkdirs(tmppath, 0755)) { |
745
3addb8cfcc3e
More and better error loging during daemon startup
Michiel Broek <mbroek@mbse.eu>
parents:
680
diff
changeset
|
154 | rc = 6; |
194
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
155 | goto endit1; |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
156 | } |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
157 | free(tmppath); |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
158 | tmppath = xstrcpy(Config.web_root); |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
159 | tmppath = xstrcat(tmppath, (char *)"/log/brews/"); |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
160 | if (! mkdirs(tmppath, 0755)) { |
745
3addb8cfcc3e
More and better error loging during daemon startup
Michiel Broek <mbroek@mbse.eu>
parents:
680
diff
changeset
|
161 | rc = 7; |
194
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
162 | goto endit1; |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
163 | } |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
164 | free(tmppath); |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
165 | tmppath = NULL; |
d202777ebae5
Added design notes for the csv log format from fermenters. In bmsd configuration added the web_root entry. Switched fermenter DLOG messages to store the log in flat csv files instead of the database. This is much faster and the graphs look better.
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
166 | |
0 | 167 | if (bms_mysql_init()) { |
168 | rc = 3; | |
169 | goto endit1; | |
170 | } | |
171 | if (mqtt_connect()) { | |
172 | rc = 4; | |
173 | goto endit2; | |
174 | } | |
175 | ||
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
176 | rc = pthread_create(&ws_thread, NULL, ws_loop, NULL); |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
177 | if (rc) { |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
178 | fprintf(stderr, "ws_loop thread didn't start rc=%d\n", rc); |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
179 | syslog(LOG_NOTICE, "ws_loop thread didn't start rc=%d", rc); |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
180 | rc = 5; |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
181 | goto endit3; |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
182 | } |
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
183 | |
0 | 184 | if (debug) |
185 | fprintf(stdout, "[main] Entering main loop\n"); | |
186 | ||
747
b6fbe6821468
Replace TRUE and FALSE for lowercase version.
Michiel Broek <mbroek@mbse.eu>
parents:
746
diff
changeset
|
187 | while (my_shutdown == false) { |
0 | 188 | |
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
189 | usleep(3000000); |
384
442d23455ae4
The bms daemon now checks if a node went away of no data is received for 10 minutes.
Michiel Broek <mbroek@mbse.eu>
parents:
194
diff
changeset
|
190 | nodes_check_online(); |
680
0bb48333d133
Fixed missing co2meters websocket data. Added websockets and removed polling for the mon-ispindel screen. The iSpindel SG gauge adjusts the resolution to the highest detected OG.
Michiel Broek <mbroek@mbse.eu>
parents:
671
diff
changeset
|
191 | usleep(2000000); |
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
192 | ws_check(); |
0 | 193 | } |
194 | if (debug) | |
195 | fprintf(stdout, "[main] Exit from main loop\n"); | |
196 | ||
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
197 | endit3: |
0 | 198 | /* |
199 | * Remove our topics and close MQTT connection. | |
200 | */ | |
201 | mqtt_disconnect(); | |
202 | ||
203 | endit2: | |
204 | bms_mysql_end(); | |
205 | ||
206 | endit1: | |
207 | ||
208 | killconfig(); | |
209 | ||
210 | ulockprog((char *)"bmsd"); | |
211 | ||
212 | free(Private_Path); | |
213 | Private_Path = NULL; | |
214 | ||
215 | if (debug) | |
216 | fprintf(stdout, "[main] Server shutdown complete\n"); | |
217 | return rc; | |
218 | } | |
219 | ||
220 | ||
221 | ||
222 | int main(int argc, char *argv[]) { | |
223 | int rc, c, i; | |
224 | pid_t frk; | |
225 | ||
226 | while (1) { | |
227 | int option_index = 0; | |
228 | static struct option long_options[] = { | |
229 | {"debug", 0, 0, 'c'}, | |
230 | {"help", 0, 0, 'h'}, | |
231 | {0, 0, 0, 0} | |
232 | }; | |
233 | ||
234 | c = getopt_long(argc, argv, "dh", long_options, &option_index); | |
235 | if (c == -1) | |
236 | break; | |
237 | switch (c) { | |
747
b6fbe6821468
Replace TRUE and FALSE for lowercase version.
Michiel Broek <mbroek@mbse.eu>
parents:
746
diff
changeset
|
238 | case 'd': debug = true; |
0 | 239 | break; |
240 | case 'h': help(); | |
241 | return 1; | |
242 | } | |
243 | } | |
244 | ||
245 | openlog("bmsd", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER); | |
246 | syslog(LOG_NOTICE, "bmsd v%s starting", VERSION); | |
247 | if (debug) | |
248 | fprintf(stdout, "bmsd v%s starting\n", VERSION); | |
249 | ||
250 | ||
251 | /* | |
252 | * Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored | |
253 | * but that's live. This daemon should only be stopped by SIGTERM. | |
254 | * Don't catch SIGCHLD. | |
255 | */ | |
256 | for (i = 0; i < NSIG; i++) { | |
257 | if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP)) | |
258 | signal(i, (void (*))die); | |
259 | } | |
260 | ||
261 | if (debug) { | |
262 | /* | |
263 | * For debugging run in foreground. | |
264 | */ | |
265 | rc = server(); | |
266 | } else { | |
267 | /* | |
268 | * Server initialization is complete. Now we can fork the | |
671
4b54d6f79d25
Version 0.3.33 Added websockets framework. Added fermenter status messages to the websockets broadcast.
Michiel Broek <mbroek@mbse.eu>
parents:
579
diff
changeset
|
269 | * daemon and return to the user. |
0 | 270 | */ |
271 | frk = fork(); | |
272 | switch (frk) { | |
273 | case -1: | |
274 | syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno)); | |
275 | exit(1); | |
276 | case 0: /* | |
277 | * Run the daemon | |
278 | */ | |
279 | fclose(stdin); | |
280 | if (open("/dev/null", O_RDONLY) != 0) { | |
281 | syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed"); | |
282 | _exit(2); | |
283 | } | |
284 | fclose(stdout); | |
285 | if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) { | |
286 | syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed"); | |
287 | _exit(2); | |
288 | } | |
289 | fclose(stderr); | |
290 | if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) { | |
291 | syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed"); | |
292 | _exit(2); | |
293 | } | |
294 | mypid = getpid(); | |
295 | rc = server(); | |
296 | break; | |
297 | /* Not reached */ | |
298 | default: | |
299 | /* | |
300 | * Here we detach this process and let the child | |
301 | * run the deamon process. | |
302 | */ | |
303 | syslog(LOG_NOTICE, "Starting daemon with pid %d", frk); | |
304 | exit(0); | |
305 | } | |
306 | } | |
307 | ||
308 | syslog(LOG_NOTICE, "Finished, rc=%d", rc); | |
309 | return rc; | |
310 | } | |
311 | ||
312 |