2 * Copyright (C) 2010-2011 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
21 #define _(str) gettext(str)
30 #include <sys/types.h>
31 #include <sys/select.h>
32 #include <sys/socket.h>
37 #include <microhttpd.h>
44 #include "psensor_json.h"
49 static const char *program_name;
51 #define DEFAULT_PORT 3131
53 #define PAGE_NOT_FOUND (_("<html><body><p>\
54 Page not found - Go to <a href='/'>Main page</a>\
57 static struct option long_options[] = {
58 {"version", no_argument, 0, 'v'},
59 {"help", no_argument, 0, 'h'},
60 {"port", required_argument, 0, 'p'},
61 {"wdir", required_argument, 0, 'w'},
62 {"debug", no_argument, 0, 'd'},
66 static struct server_data server_data;
68 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
72 static int server_stop_requested;
76 printf("psensor-server %s\n", VERSION);
77 printf(_("Copyright (C) %s jeanfi@gmail.com\n\
78 License GPLv2: GNU GPL version 2 or later \
79 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n\
80 This is free software: you are free to change and redistribute it.\n\
81 There is NO WARRANTY, to the extent permitted by law.\n"),
87 printf(_("Usage: %s [OPTION]...\n"), program_name);
89 puts(_("psensor-server is an HTTP server "
90 "for monitoring hardware sensors remotely."));
95 -h, --help display this help and exit\n\
96 -v, --version display version information and exit"));
101 -d,--debug run in debug mode\n\
102 -p,--port=PORT webserver port\n\
103 -w,--wdir=DIR directory containing webserver pages"));
107 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
109 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
113 Returns the file path corresponding to a given URL
115 char *get_path(const char *url, const char *www_dir)
120 if (!strlen(url) || !strcmp(url, ".") || !strcmp(url, "/"))
125 res = malloc(strlen(www_dir)+strlen(p)+1);
127 strcpy(res, www_dir);
133 #if MHD_VERSION >= 0x00090200
135 file_reader(void *cls, uint64_t pos, char *buf, size_t max)
138 file_reader(void *cls, uint64_t pos, char *buf, int max)
143 fseek(file, pos, SEEK_SET);
144 return fread(buf, 1, max, file);
147 struct MHD_Response *
148 create_response_api(const char *nurl,
150 unsigned int *rp_code)
152 struct MHD_Response *resp;
156 if (!strcmp(nurl, URL_BASE_API_1_0_SENSORS)) {
158 page = sensors_to_json_string(server_data.sensors);
161 } else if (!strcmp(nurl, URL_API_1_0_SYSINFO)) {
163 page = sysinfo_to_json_string(&server_data.psysinfo);
164 } else if (!strcmp(nurl, URL_API_1_0_CPU_USAGE)) {
165 page = sensor_to_json_string(server_data.cpu_usage);
167 } else if (!strncmp(nurl, URL_BASE_API_1_0_SENSORS,
168 strlen(URL_BASE_API_1_0_SENSORS))
169 && nurl[strlen(URL_BASE_API_1_0_SENSORS)] == '/') {
171 const char *sid = nurl + strlen(URL_BASE_API_1_0_SENSORS) + 1;
173 s = psensor_list_get_by_id(server_data.sensors, sid);
176 page = sensor_to_json_string(s);
178 } else if (debug && !strcmp(nurl, URL_API_1_0_SERVER_STOP)) {
180 server_stop_requested = 1;
181 page = strdup(_("<html><body><p>"
182 "Server stop requested</p></body></html>"));
186 *rp_code = MHD_HTTP_OK;
188 resp = MHD_create_response_from_data(strlen(page), page,
191 MHD_add_response_header(resp, MHD_HTTP_HEADER_CONTENT_TYPE,
200 struct MHD_Response *
201 create_response_file(const char *nurl,
203 unsigned int *rp_code,
206 if (is_file(fpath)) {
207 FILE *file = fopen(fpath, "rb");
213 *rp_code = MHD_HTTP_OK;
217 return MHD_create_response_from_data
218 (0, NULL, MHD_NO, MHD_NO);
221 return MHD_create_response_from_callback
226 (MHD_ContentReaderFreeCallback)&fclose);
234 struct MHD_Response *
235 create_response(const char *nurl, const char *method, unsigned int *rp_code)
237 struct MHD_Response *resp = NULL;
239 if (!strncmp(nurl, URL_BASE_API_1_0, strlen(URL_BASE_API_1_0))) {
240 resp = create_response_api(nurl, method, rp_code);
242 char *fpath = get_path(nurl, server_data.www_dir);
244 resp = create_response_file(nurl, method, rp_code, fpath);
252 char *page = strdup(PAGE_NOT_FOUND);
253 *rp_code = MHD_HTTP_NOT_FOUND;
255 return MHD_create_response_from_data
256 (strlen(page), page, MHD_YES, MHD_NO);
261 cbk_http_request(void *cls,
262 struct MHD_Connection *connection,
266 const char *upload_data,
267 size_t *upload_data_size, void **ptr)
270 struct MHD_Response *response;
273 unsigned int resp_code;
275 if (strcmp(method, "GET"))
278 if (&dummy != *ptr) {
279 /* The first time only the headers are valid, do not
280 respond in the first round... */
285 if (*upload_data_size)
288 *ptr = NULL; /* clear context pointer */
291 printf(_("HTTP Request: %s\n"), url);
293 nurl = url_normalize(url);
295 pthread_mutex_lock(&mutex);
296 response = create_response(nurl, method, &resp_code);
297 pthread_mutex_unlock(&mutex);
299 ret = MHD_queue_response(connection, resp_code, response);
300 MHD_destroy_response(response);
307 int main(int argc, char *argv[])
309 struct MHD_Daemon *d;
310 int port = DEFAULT_PORT;
314 program_name = argv[0];
316 setlocale(LC_ALL, "");
319 bindtextdomain(PACKAGE, LOCALEDIR);
323 server_data.www_dir = DEFAULT_WWW_DIR;
324 server_data.psysinfo.interfaces = NULL;
326 while ((optc = getopt_long(argc, argv,
327 "vhp:w:d", long_options, NULL)) != -1) {
331 server_data.www_dir = strdup(optarg);
352 if (!cmdok || optind != argc) {
353 fprintf(stderr, _("Try `%s --help' for more information.\n"),
360 server_data.sensors = get_all_sensors(600);
363 server_data.cpu_usage = create_cpu_usage_sensor(600);
366 if (!*server_data.sensors)
367 fprintf(stderr, _("ERROR: no sensors detected\n"));
369 d = MHD_start_daemon(MHD_USE_THREAD_PER_CONNECTION,
371 NULL, NULL, &cbk_http_request, server_data.sensors,
374 fprintf(stderr, _("ERROR: Fail to create web server\n"));
378 log_printf(LOG_INFO, _("Web server started on port: %d"), port);
379 log_printf(LOG_INFO, _("WWW directory: %s"), server_data.www_dir);
380 log_printf(LOG_INFO, _("URL: http://localhost:%d"), port);
382 while (!server_stop_requested) {
383 pthread_mutex_lock(&mutex);
386 sysinfo_update(&server_data.psysinfo);
387 cpu_usage_sensor_update(server_data.cpu_usage);
389 psensor_list_update_measures(server_data.sensors);
391 pthread_mutex_unlock(&mutex);
397 /* sanity cleanup for valgrind */
398 psensor_list_free(server_data.sensors);
400 psensor_free(server_data.cpu_usage);
402 free(server_data.www_dir);