style
[psensor.git] / src / server / server.c
index 08a84a1..e0f69bb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2013 jeanfi@gmail.com
+ * Copyright (C) 2010-2014 jeanfi@gmail.com
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  * 02110-1301 USA
  */
+#define _LARGEFILE_SOURCE 1
+#include "config.h"
+
 #include <locale.h>
 #include <libintl.h>
 #define _(str) gettext(str)
 
-#include "config.h"
-
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -41,8 +42,9 @@
 #include "cpu.h"
 #endif
 
-#include "log.h"
+#include <plog.h>
 #include "psensor_json.h"
+#include <pmutex.h>
 #include "url.h"
 #include "server.h"
 #include "slog.h"
@@ -73,7 +75,7 @@ static struct option long_options[] = {
 
 static struct server_data server_data;
 
-static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t mutex;
 
 static int server_stop_requested;
 
@@ -149,7 +151,7 @@ file_reader(void *cls, uint64_t pos, char *buf, int max)
 {
        FILE *file = cls;
 
-       fseek(file, pos, SEEK_SET);
+       fseeko(file, pos, SEEK_SET);
        return fread(buf, 1, max, file);
 }
 
@@ -253,15 +255,16 @@ create_response(const char *nurl, const char *method, unsigned int *rp_code)
                free(fpath);
        }
 
-       if (resp) {
+       if (resp)
                return resp;
-       } else {
-               char *page = strdup(PAGE_NOT_FOUND);
-               *rp_code = MHD_HTTP_NOT_FOUND;
 
-               return MHD_create_response_from_data
-                       (strlen(page), page, MHD_YES, MHD_NO);
-       }
+       char *page = strdup(PAGE_NOT_FOUND);
+       *rp_code = MHD_HTTP_NOT_FOUND;
+
+       return MHD_create_response_from_data(strlen(page),
+                                            page,
+                                            MHD_YES,
+                                            MHD_NO);
 }
 
 static int cbk_http_request(void *cls,
@@ -297,9 +300,9 @@ static int cbk_http_request(void *cls,
 
        nurl = url_normalize(url);
 
-       pthread_mutex_lock(&mutex);
+       pmutex_lock(&mutex);
        response = create_response(nurl, method, &resp_code);
-       pthread_mutex_unlock(&mutex);
+       pmutex_unlock(&mutex);
 
        ret = MHD_queue_response(connection, resp_code, response);
        MHD_destroy_response(response);
@@ -325,7 +328,9 @@ int main(int argc, char *argv[])
 #endif
 
        server_data.www_dir = NULL;
+#ifdef HAVE_GTOP
        server_data.psysinfo.interfaces = NULL;
+#endif
        log_file = NULL;
        slog_file = NULL;
        slog_interval = 300;
@@ -385,6 +390,8 @@ int main(int argc, char *argv[])
        if (!log_file)
                log_file = strdup(DEFAULT_LOG_FILE);
 
+       pmutex_init(&mutex);
+
        log_open(log_file);
 
        psensor_init();
@@ -423,7 +430,7 @@ int main(int argc, char *argv[])
        }
 
        while (!server_stop_requested) {
-               pthread_mutex_lock(&mutex);
+               pmutex_lock(&mutex);
 
 #ifdef HAVE_GTOP
                sysinfo_update(&server_data.psysinfo);
@@ -433,7 +440,7 @@ int main(int argc, char *argv[])
 
                psensor_log_measures(server_data.sensors);
 
-               pthread_mutex_unlock(&mutex);
+               pmutex_unlock(&mutex);
                sleep(5);
        }