2 * Copyright (C) 2010-2016 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define _LARGEFILE_SOURCE 1
24 #define _(str) gettext(str)
32 #include <sys/types.h>
33 #include <sys/select.h>
34 #include <sys/socket.h>
39 #include <microhttpd.h>
49 #include "psensor_json.h"
55 static const char *DEFAULT_LOG_FILE = "/var/log/psensor-server.log";
57 #define HTML_STOP_REQUESTED \
58 (_("<html><body><p>Server stop requested</p></body></html>"))
60 static const char *program_name;
62 static const int DEFAULT_PORT = 3131;
64 #define PAGE_NOT_FOUND (_("<html><body><p>"\
65 "Page not found - Go to <a href='/'>Main page</a></p></body>"))
67 static struct option long_options[] = {
68 {"version", no_argument, NULL, 'v'},
69 {"help", no_argument, NULL, 'h'},
70 {"port", required_argument, NULL, 'p'},
71 {"wdir", required_argument, NULL, 'w'},
72 {"debug", required_argument, NULL, 'd'},
73 {"log-file", required_argument, NULL, 'l'},
74 {"sensor-log-file", required_argument, NULL, 0},
75 {"sensor-log-interval", required_argument, NULL, 0},
79 static struct server_data server_data;
81 static pthread_mutex_t mutex;
83 static int server_stop_requested;
85 static void print_version(void)
87 printf("psensor-server %s\n", VERSION);
88 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
89 "License GPLv2: GNU GPL version 2 or later "
90 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
91 "This is free software: you are free to change and redistribute it.\n"
92 "There is NO WARRANTY, to the extent permitted by law.\n"),
96 static void print_help(void)
98 printf(_("Usage: %s [OPTION]...\n"), program_name);
100 puts(_("psensor-server is an HTTP server for monitoring hardware "
101 "sensors remotely."));
105 puts(_(" -h, --help display this help and exit\n"
106 " -v, --version display version information and exit"));
109 puts(_(" -p,--port=PORT webserver port\n"
110 " -w,--wdir=DIR directory containing webserver pages"));
113 puts(_(" -d, --debug=LEVEL "
114 "set the debug level, integer between 0 and 3"));
115 puts(_(" -l, --log-file=PATH set the log file to PATH"));
116 puts(_(" --sensor-log-file=PATH set the sensor log file to PATH"));
117 puts(_(" --sensor-log-interval=S "
118 "set the sensor log interval to S (seconds)"));
121 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
123 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
127 * Returns the file path corresponding to a given URL
129 static char *get_path(const char *url, const char *www_dir)
134 if (!strlen(url) || !strcmp(url, ".") || !strcmp(url, "/"))
139 res = malloc(strlen(www_dir)+strlen(p)+1);
141 strcpy(res, www_dir);
147 #if MHD_VERSION >= 0x00090200
149 file_reader(void *cls, uint64_t pos, char *buf, size_t max)
152 file_reader(void *cls, uint64_t pos, char *buf, int max)
157 fseeko(file, pos, SEEK_SET);
158 return fread(buf, 1, max, file);
161 static struct MHD_Response *
162 create_response_api(const char *nurl, const char *method, unsigned int *rp_code)
164 struct MHD_Response *resp;
168 if (!strcmp(nurl, URL_BASE_API_1_1_SENSORS)) {
169 page = sensors_to_json_string(server_data.sensors);
171 } else if (!strcmp(nurl, URL_API_1_1_SYSINFO)) {
172 page = sysinfo_to_json_string(&server_data.psysinfo);
173 } else if (!strcmp(nurl, URL_API_1_1_CPU_USAGE)) {
174 page = sensor_to_json_string(server_data.cpu_usage);
176 } else if (!strncmp(nurl, URL_BASE_API_1_1_SENSORS,
177 strlen(URL_BASE_API_1_1_SENSORS))
178 && nurl[strlen(URL_BASE_API_1_1_SENSORS)] == '/') {
180 const char *sid = nurl + strlen(URL_BASE_API_1_1_SENSORS) + 1;
182 s = psensor_list_get_by_id(server_data.sensors, sid);
185 page = sensor_to_json_string(s);
187 } else if (!strcmp(nurl, URL_API_1_1_SERVER_STOP)) {
189 server_stop_requested = 1;
190 page = strdup(HTML_STOP_REQUESTED);
194 *rp_code = MHD_HTTP_OK;
196 resp = MHD_create_response_from_buffer(strlen(page),
198 MHD_RESPMEM_MUST_FREE);
200 MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE,
209 static struct MHD_Response *create_response_file(const char *nurl,
211 unsigned int *rp_code,
218 ret = stat(fpath, &st);
220 if (!ret && (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode))) {
221 file = fopen(fpath, "rb");
224 *rp_code = MHD_HTTP_OK;
228 return MHD_create_response_from_buffer
232 return MHD_create_response_from_callback
237 (MHD_ContentReaderFreeCallback)&fclose);
240 log_err("Failed to open: %s.", fpath);
247 static int is_access_allowed(char *path)
252 rpath = realpath(path, NULL);
254 n = strlen(server_data.www_dir);
255 if (!strncmp(server_data.www_dir, rpath, n)
257 "/usr/share/javascript/jquery/jquery.js")) {
262 log_err(_("Resource access refused %s real path is %s"),
269 log_err(_("Cannot get real path of %s"), path);
277 static struct MHD_Response *
278 create_response(const char *nurl, const char *method, unsigned int *rp_code)
281 struct MHD_Response *resp = NULL;
283 if (!strncmp(nurl, URL_BASE_API_1_1, strlen(URL_BASE_API_1_1))) {
284 resp = create_response_api(nurl, method, rp_code);
286 fpath = get_path(nurl, server_data.www_dir);
288 if (is_access_allowed(fpath))
289 resp = create_response_file(nurl,
300 page = strdup(PAGE_NOT_FOUND);
301 *rp_code = MHD_HTTP_NOT_FOUND;
303 return MHD_create_response_from_buffer(strlen(page),
305 MHD_RESPMEM_MUST_FREE);
308 static int cbk_http_request(void *cls,
309 struct MHD_Connection *connection,
313 const char *upload_data,
314 size_t *upload_data_size,
318 struct MHD_Response *response;
321 unsigned int resp_code;
323 if (strcmp(method, "GET"))
326 if (&dummy != *ptr) {
327 /* The first time only the headers are valid, do not
328 * respond in the first round...
334 if (*upload_data_size)
337 *ptr = NULL; /* clear context pointer */
339 log_debug(_("HTTP Request: %s"), url);
341 nurl = url_normalize(url);
344 response = create_response(nurl, method, &resp_code);
345 pmutex_unlock(&mutex);
347 ret = MHD_queue_response(connection, resp_code, response);
348 MHD_destroy_response(response);
355 int main(int argc, char *argv[])
357 struct MHD_Daemon *d;
358 int port, opti, optc, cmdok, ret, slog_interval;
359 char *log_file, *slog_file;
361 program_name = argv[0];
363 setlocale(LC_ALL, "");
366 bindtextdomain(PACKAGE, LOCALEDIR);
370 server_data.www_dir = NULL;
372 server_data.psysinfo.interfaces = NULL;
380 while ((optc = getopt_long(argc,
388 server_data.www_dir = realpath(optarg, NULL);
401 log_level = atoi(optarg);
402 log_info(_("Enables debug mode: %d"), log_level);
406 log_file = strdup(optarg);
409 if (!strcmp(long_options[opti].name, "sensor-log-file"))
410 slog_file = strdup(optarg);
411 else if (!strcmp(long_options[opti].name,
412 "sensor-log-interval"))
413 slog_interval = atoi(optarg);
421 if (!cmdok || optind != argc) {
422 fprintf(stderr, _("Try `%s --help' for more information.\n"),
427 if (!server_data.www_dir) {
428 server_data.www_dir = realpath(DEFAULT_WWW_DIR, NULL);
429 if (!server_data.www_dir) {
431 _("Webserver directory does not exist.\n"));
437 log_file = strdup(DEFAULT_LOG_FILE);
443 hddtemp_psensor_list_append(&server_data.sensors, 600);
445 lmsensor_psensor_list_append(&server_data.sensors, 600);
448 server_data.cpu_usage = create_cpu_usage_sensor(600);
451 if (!server_data.sensors || !*server_data.sensors)
452 log_err(_("No sensors detected."));
454 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
456 NULL, NULL, &cbk_http_request, server_data.sensors,
459 log_err(_("Failed to create Web server."));
463 log_info(_("Web server started on port: %d"), port);
464 log_info(_("WWW directory: %s"), server_data.www_dir);
465 log_info(_("URL: http://localhost:%d"), port);
468 if (slog_interval <= 0)
470 ret = slog_activate(slog_file,
475 log_err(_("Failed to activate logging of sensors."));
478 while (!server_stop_requested) {
482 sysinfo_update(&server_data.psysinfo);
483 cpu_usage_sensor_update(server_data.cpu_usage);
487 atasmart_psensor_list_update(server_data.sensors);
490 hddtemp_psensor_list_update(server_data.sensors);
492 lmsensor_psensor_list_update(server_data.sensors);
494 psensor_log_measures(server_data.sensors);
496 pmutex_unlock(&mutex);
504 /* sanity cleanup for valgrind */
505 psensor_list_free(server_data.sensors);
507 psensor_free(server_data.cpu_usage);
509 free(server_data.www_dir);
516 if (log_file != DEFAULT_LOG_FILE)