Browse Source

- Add logging and config handling code from rpdd

- Add default config file
decke 6 years ago
parent
commit
e4fe1dc5e5
9 changed files with 372 additions and 24 deletions
  1. 11
    8
      Makefile
  2. 115
    0
      log.c
  3. 48
    0
      log.h
  4. 18
    0
      redqd.conf-dist
  5. 23
    9
      server.c
  6. 0
    5
      server.h
  7. 3
    2
      stomp.c
  8. 117
    0
      util.c
  9. 37
    0
      util.h

+ 11
- 8
Makefile View File

@@ -2,24 +2,27 @@
2 2
 # Simple libevent based STOMP server
3 3
 #
4 4
 
5
+# Development flags
6
+CFLAGS+=-g -Wall
7
+
5 8
 LOCALBASE?=/usr/local
6 9
 
7
-CPPFLAGS=-I${LOCALBASE}/include -g -Wall
8
-LDFLAGS=-L${LOCALBASE}/lib/event2 -L${LOCALBASE}/lib
10
+CFLAGS+=-I${LOCALBASE}/include
11
+LDFLAGS+=-L${LOCALBASE}/lib/event2 -L${LOCALBASE}/lib
9 12
 
10
-SRC =	server.c common.c stomp.c
11
-OBJS =	${SRC:.c=.o}
13
+SRC=	log.c util.c server.c common.c stomp.c
14
+OBJS=	${SRC:.c=.o}
12 15
 
13
-all:	redqueue
16
+all:	redqd
14 17
 
15 18
 clean:
16 19
 	@rm -f *.o *.core
17 20
 
18
-redqueue:	${OBJS}
19
-	$(CC) $(LDFLAGS) -levent -lleveldb ${OBJS} -o redqueue
21
+redqd:	${OBJS}
22
+	$(CC) $(LDFLAGS) -levent -lleveldb ${OBJS} -o redqd
20 23
 
21 24
 # SUFFIX RULES
22 25
 .SUFFIXES: .c .o
23 26
 
24 27
 .c.o:
25
-	$(CC) $(CPPFLAGS) -c ${.IMPSRC}
28
+	$(CC) $(CFLAGS) -c ${.IMPSRC}

+ 115
- 0
log.c View File

@@ -0,0 +1,115 @@
1
+/*
2
+ * Copyright (C) 2011 Bernhard Froehlich <decke@bluelife.at>
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ *
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ *    notice, this list of conditions and the following disclaimer.
11
+ * 2. Author's name may not be used endorse or promote products derived
12
+ *    from this software without specific prior written permission.
13
+ *
14
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
+ * POSSIBILITY OF SUCH DAMAGE.
25
+ */
26
+
27
+#include <errno.h>
28
+#include <stdio.h>
29
+#include <stdarg.h>
30
+#include <string.h>
31
+#include <time.h>
32
+
33
+#include "log.h"
34
+
35
+char *loglevelnames[] = { "ERROR", "WARN ", "INFO ", "DEBUG" };
36
+
37
+int loglevel = LOG_INFO;
38
+FILE *logfile = NULL;
39
+
40
+int logopen(char *filename)
41
+{
42
+    if(logfile != NULL)
43
+        return 1;
44
+
45
+    logfile = fopen(filename, "a");
46
+    if(logfile == NULL)
47
+    {
48
+        printf("Error %s: Could not open logfile %s\n", strerror(errno), filename);
49
+        return 1;
50
+    }
51
+
52
+    return 0;
53
+}
54
+
55
+int logclose(void)
56
+{
57
+    int retval;
58
+
59
+    if(logfile == NULL)
60
+        return 1;
61
+
62
+    fflush(logfile);
63
+    retval = fclose(logfile);
64
+    if(retval != 0)
65
+        printf("Error %s on closing logfile\n", strerror(errno));
66
+   
67
+    logfile = NULL;
68
+    return retval;
69
+}
70
+
71
+int logsetlevel(int loglvl)
72
+{
73
+    if(loglvl >= 0 && loglvl < 3)
74
+    {
75
+        loglevel = loglvl;
76
+        return 0;
77
+    }
78
+
79
+    return 1;
80
+}
81
+
82
+int logwrite(int loglvl, const char *logfmt, ...)
83
+{
84
+    char logmsg[4096];
85
+    va_list args;
86
+    time_t now;
87
+    char timeinfo[32];
88
+
89
+    if(logfile == NULL)
90
+        return 1;
91
+
92
+    if(loglvl < 0 || loglvl > loglevel)
93
+        return 0;
94
+
95
+    time(&now);
96
+    strftime(timeinfo, sizeof(timeinfo), "%c", localtime(&now));
97
+
98
+    va_start(args, logfmt);
99
+    vsprintf(logmsg, logfmt, args);
100
+
101
+    if(logmsg[strlen(logmsg)-1] == '\n')
102
+        logmsg[strlen(logmsg)-1] = '\0';
103
+
104
+    if(fprintf(logfile, "[%s] %s - %s\n", timeinfo, loglevelnames[loglvl], logmsg) < 0)
105
+    {
106
+        va_end(args);
107
+        return 1;
108
+    }
109
+
110
+    va_end(args);
111
+
112
+    fflush(logfile);
113
+    return 0;
114
+}
115
+

+ 48
- 0
log.h View File

@@ -0,0 +1,48 @@
1
+/*
2
+ * Copyright (C) 2011 Bernhard Froehlich <decke@bluelife.at>
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ *
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ *    notice, this list of conditions and the following disclaimer.
11
+ * 2. Author's name may not be used endorse or promote products derived
12
+ *    from this software without specific prior written permission.
13
+ *
14
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
+ * POSSIBILITY OF SUCH DAMAGE.
25
+ */
26
+
27
+#ifndef _LOG_H_
28
+#define _LOG_H_
29
+
30
+enum loglevels
31
+{
32
+    LOG_ERROR,
33
+    LOG_WARN,
34
+    LOG_INFO,
35
+    LOG_DEBUG
36
+};
37
+
38
+#define logdebug(format, args...) logwrite(LOG_DEBUG, format, ##args)
39
+#define loginfo(format, args...) logwrite(LOG_INFO, format, ##args)
40
+#define logwarn(format, args...) logwrite(LOG_WARN, format, ##args)
41
+#define logerror(format, args...) logwrite(LOG_ERROR, format, ##args)
42
+
43
+extern int logopen(char *filename);
44
+extern int logclose(void);
45
+extern int logsetlevel(int loglvl);
46
+extern int logwrite(int loglvl, const char *logfmt, ...);
47
+
48
+#endif /* _LOG_H_ */

+ 18
- 0
redqd.conf-dist View File

@@ -0,0 +1,18 @@
1
+# Redqueue STOMP server
2
+#
3
+# redqd.conf
4
+
5
+# Listen
6
+listenIP   127.0.0.1
7
+listenPort 8080
8
+
9
+# Authentication
10
+#authUser   test
11
+#authPass   test
12
+
13
+# LevelDB Database file
14
+dbFile     /tmp/redqueue.db
15
+
16
+# Logfile
17
+logFile    /var/log/redqd.log
18
+

+ 23
- 9
server.c View File

@@ -28,8 +28,8 @@
28 28
 #include <sys/socket.h>
29 29
 #include <netinet/in.h>
30 30
 #include <arpa/inet.h>
31
-#include <syslog.h>
32 31
 #include <signal.h>
32
+#include <limits.h>
33 33
 
34 34
 /* Required by event.h. */
35 35
 #include <sys/time.h>
@@ -53,6 +53,8 @@
53 53
 /* LevelDB */
54 54
 #include <leveldb/c.h>
55 55
 
56
+#include "log.h"
57
+#include "util.h"
56 58
 #include "common.h"
57 59
 #include "server.h"
58 60
 #include "client.h"
@@ -72,11 +74,13 @@ void signal_handler(int sig) {
72 74
 	switch(sig) {
73 75
 		case SIGTERM:
74 76
 		case SIGHUP:
77
+			logclose();
78
+			logopen(configget("logFile"));
75 79
 		case SIGINT:
76 80
 			event_base_loopbreak(base);
77 81
 			break;
78 82
         default:
79
-            syslog(LOG_WARNING, "Unhandled signal (%d) %s", sig, strsignal(sig));
83
+            logwarn("Unhandled signal (%d) %s", sig, strsignal(sig));
80 84
             break;
81 85
     }
82 86
 }
@@ -248,9 +252,9 @@ void on_accept(int fd, short ev, void *arg)
248 252
 
249 253
 int main(int argc, char **argv)
250 254
 {
255
+	char config[PATH_MAX] = CONF_FILE;
251 256
 	int listen_fd, ch;
252 257
 	int daemon = 0;
253
-	int port = SERVER_PORT;
254 258
 	struct sockaddr_in listen_addr;
255 259
 	struct event *ev_accept;
256 260
 	int reuseaddr_on;
@@ -267,17 +271,25 @@ int main(int argc, char **argv)
267 271
 	signal(SIGINT, signal_handler);
268 272
 	signal(SIGQUIT, signal_handler);
269 273
 
270
-	while ((ch = getopt(argc, argv, "dp:")) != -1) {
274
+	while ((ch = getopt(argc, argv, "d:")) != -1) {
271 275
 	    switch (ch) {
272 276
 	    case 'd':
273 277
 	        daemon = 1;
274 278
 	        break;
275
-	    case 'p':
276
-	        port = atoi(optarg);
277
-	        break;
278 279
 	    }
279 280
 	}
280 281
 
282
+	if(configparse(config)){
283
+		printf("Could not load config file %s\n", config);
284
+		exit(EXIT_FAILURE);
285
+	}
286
+  
287
+	if(logopen(configget("logFile")) != 0)
288
+		exit(EXIT_FAILURE);
289
+            
290
+	logwrite(LOG_INFO, "-------------------------------");
291
+	logwrite(LOG_INFO, "%s/%s started", DAEMON_NAME, REDQUEUE_VERSION);
292
+
281 293
 	if (daemon) {
282 294
 	    pid = fork();
283 295
 	    if (pid < 0) {
@@ -307,7 +319,7 @@ int main(int argc, char **argv)
307 319
 	leveldb_options_set_create_if_missing(options, 1);
308 320
 	leveldb_options_set_error_if_exists(options, 0);
309 321
 
310
-	db = leveldb_open(options, "/tmp/redqueue.db", &error);
322
+	db = leveldb_open(options, configget("dbFile"), &error);
311 323
 	CheckNoError(error);
312 324
 	
313 325
 	/* Initialize libevent. */
@@ -321,7 +333,7 @@ int main(int argc, char **argv)
321 333
 	memset(&listen_addr, 0, sizeof(listen_addr));
322 334
 	listen_addr.sin_family = AF_INET;
323 335
 	listen_addr.sin_addr.s_addr = INADDR_ANY;
324
-	listen_addr.sin_port = htons(port);
336
+	listen_addr.sin_port = htons(atoi(configget("listenPort")));
325 337
 
326 338
 	if (bind(listen_fd, (struct sockaddr *)&listen_addr, sizeof(listen_addr)) < 0)
327 339
 		err(1, "bind failed");
@@ -353,5 +365,7 @@ int main(int argc, char **argv)
353 365
 	leveldb_cache_destroy(cache);
354 366
 	leveldb_env_destroy(env);
355 367
 
368
+	logclose();
369
+
356 370
 	return 0;
357 371
 }

+ 0
- 5
server.h View File

@@ -32,9 +32,4 @@
32 32
 #define CONF_FILE "redqd.conf"
33 33
 #define PID_FILE "/var/run/redqd.pid"
34 34
 
35
-#define SERVER_PORT 8080
36
-
37
-#define AUTH_USER ""
38
-#define AUTH_PASS ""
39
- 
40 35
 #endif /* _SERVER_H_ */

+ 3
- 2
stomp.c View File

@@ -35,6 +35,7 @@
35 35
 #include <event2/http.h>
36 36
 #include <event2/keyvalq_struct.h>
37 37
 
38
+#include "util.h"
38 39
 #include "server.h"
39 40
 #include "client.h"
40 41
 #include "stomp.h"
@@ -140,7 +141,7 @@ int stomp_connect(struct client *client)
140 141
    const char *login;
141 142
    const char *passcode;
142 143
 
143
-   if(strlen(AUTH_USER) > 0 && strlen(AUTH_PASS) > 0){
144
+   if(strlen(configget("authUser")) > 0 && strlen(configget("authPass")) > 0){
144 145
       login = evhttp_find_header(client->request_headers, "login");
145 146
       if(login == NULL){
146 147
          client->response_cmd = STOMP_CMD_ERROR;
@@ -155,7 +156,7 @@ int stomp_connect(struct client *client)
155 156
          return 1;
156 157
       }
157 158
 
158
-      if(strcmp(login, AUTH_USER) != 0 || strcmp(passcode, AUTH_PASS) != 0){
159
+      if(strcmp(login, configget("authUser")) != 0 || strcmp(passcode, configget("authPass")) != 0){
159 160
          client->response_cmd = STOMP_CMD_ERROR;
160 161
          evhttp_add_header(client->response_headers, "message", "Authentication failed");
161 162
          return 1;

+ 117
- 0
util.c View File

@@ -0,0 +1,117 @@
1
+/*
2
+ * Copyright (C) 2011 Bernhard Froehlich <decke@bluelife.at>
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ *
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ *    notice, this list of conditions and the following disclaimer.
11
+ * 2. Author's name may not be used endorse or promote products derived
12
+ *    from this software without specific prior written permission.
13
+ *
14
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
+ * POSSIBILITY OF SUCH DAMAGE.
25
+ */
26
+
27
+#include <stdio.h>
28
+#include <stdlib.h>
29
+#include <string.h>
30
+
31
+#include "util.h"
32
+
33
+struct configparam
34
+{
35
+    char key[CONFIGMAXKEY];
36
+    char value[CONFIGMAXVALUE];
37
+};
38
+
39
+struct configparam config[] = {
40
+    { "authUser",      "" },
41
+    { "authPass",      "" },
42
+    { "dbFile",        "/tmp/redqueue.db" },
43
+    { "listenIP",      "127.0.0.1" },
44
+    { "listenPort",    "8080" },
45
+    { "logFile",       "/var/log/redqd.log" },
46
+    { "", "" }
47
+};
48
+
49
+int configparse(char *filename)
50
+{
51
+    char *value;
52
+    char line[255];
53
+    FILE *file = fopen(filename, "r");
54
+
55
+    if(file == NULL)
56
+        return 1;
57
+    
58
+    while(fgets(line, sizeof(line), file) != NULL)
59
+    {
60
+        if(line[0] == '#')
61
+            continue;
62
+        line[strlen(line)-1] = '\0';
63
+        
64
+        if((value = strstr(line, " ")) != NULL || (value = strstr(line, "\t")) != NULL)
65
+        {
66
+            *value = '\0';
67
+            value++;
68
+
69
+            while(*value == ' ' || *value == '\t')
70
+                value++;
71
+
72
+            if(strlen(line) == 0 || strlen(value) == 0)
73
+                 continue;
74
+
75
+            if(configset(line, value) != 0)
76
+                return 1;
77
+        }
78
+    }
79
+
80
+    fclose(file);
81
+    return 0;
82
+}
83
+
84
+char* configget(char *key)
85
+{
86
+    int i;
87
+
88
+    for(i=0; config[i].key[0] != '\0'; i++)
89
+    {
90
+        if(strcmp(key, config[i].key) == 0)
91
+        {
92
+            return config[i].value;
93
+        }
94
+    }
95
+
96
+    return NULL;
97
+}
98
+
99
+int configset(char *key, char *value)
100
+{
101
+    int i;
102
+
103
+    for(i=0; config[i].key[0] != '\0'; i++)
104
+    {
105
+        if(strcmp(key, config[i].key) == 0)
106
+        {
107
+            strncpy(config[i].value, value, sizeof(config[i].value)-1);
108
+            config[i].value[sizeof(config[i].value)-1] = '\0';
109
+            return 0;
110
+        }
111
+    }
112
+
113
+    printf("configset: Unknown config param <%s>\n", key);
114
+
115
+    return 1;
116
+}
117
+

+ 37
- 0
util.h View File

@@ -0,0 +1,37 @@
1
+/*
2
+ * Copyright (C) 2011 Bernhard Froehlich <decke@bluelife.at>
3
+ * All rights reserved.
4
+ *
5
+ * Redistribution and use in source and binary forms, with or without
6
+ * modification, are permitted provided that the following conditions
7
+ * are met:
8
+ *
9
+ * 1. Redistributions of source code must retain the above copyright
10
+ *    notice, this list of conditions and the following disclaimer.
11
+ * 2. Author's name may not be used endorse or promote products derived
12
+ *    from this software without specific prior written permission.
13
+ *
14
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17
+ * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
18
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24
+ * POSSIBILITY OF SUCH DAMAGE.
25
+ */
26
+
27
+#ifndef _UTIL_H_
28
+#define _UTIL_H_
29
+
30
+#define CONFIGMAXKEY 25
31
+#define CONFIGMAXVALUE 50
32
+
33
+extern int configparse(char *filename);
34
+extern char* configget(char *key);
35
+extern int configset(char *key, char *value);
36
+
37
+#endif /* _UTIL_H_ */

Loading…
Cancel
Save