Skip to content

Commit af31a2e

Browse files
committed
Add daemon mode
1 parent 5c89b15 commit af31a2e

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

src/server.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
#include <time.h>
29+
#include <fcntl.h>
2930
#include <errno.h>
3031
#include <unistd.h>
3132
#include <string.h>
@@ -860,3 +861,20 @@ int start_server(const char *addr, const char *port) {
860861

861862
return 0;
862863
}
864+
865+
void daemonize(void) {
866+
867+
int fd;
868+
869+
if (fork() != 0)
870+
exit(0);
871+
872+
setsid();
873+
874+
if ((fd = open("/dev/null", O_RDWR, 0)) != -1) {
875+
dup2(fd, STDIN_FILENO);
876+
dup2(fd, STDOUT_FILENO);
877+
dup2(fd, STDERR_FILENO);
878+
if (fd > STDERR_FILENO) close(fd);
879+
}
880+
}

src/server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,6 @@ int start_server(const char *, const char *);
105105

106106
void enqueue_event_write(struct ev_ctx *, struct client *);
107107

108+
void daemonize(void);
109+
108110
#endif

src/sol.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,15 @@ static const char *flag_description[] = {
4646
"Set a configuration file to load and use",
4747
"Set the listening host address",
4848
"Set the listening port",
49-
"Enable all logs, setting log level to DEBUG"
49+
"Enable all logs, setting log level to DEBUG",
50+
"Run in daemon mode"
5051
};
5152

5253
void print_help(char *me) {
5354
printf("\nSol v%s MQTT broker 3.1.1\n\n", VERSION);
54-
printf("Usage: %s [-a addr] [-p port] [-c conf] [-v]\n\n", me);
55-
const char flags[5] = "hcapv";
56-
for (int i = 0; i < 5; ++i)
55+
printf("Usage: %s [-a addr] [-p port] [-c conf] [-v|-d|-h]\n\n", me);
56+
const char flags[6] = "hcapvd";
57+
for (int i = 0; i < 6; ++i)
5758
printf(" -%c: %s\n", flags[i], flag_description[i]);
5859
printf("\n");
5960
}
@@ -66,13 +67,13 @@ int main (int argc, char **argv) {
6667
char *addr = DEFAULT_HOSTNAME;
6768
char *port = DEFAULT_PORT;
6869
char *confpath = DEFAULT_CONF_PATH;
69-
int debug = 0;
70+
int debug = 0, daemon = 0;
7071
int opt;
7172

7273
// Set default configuration
7374
config_set_default();
7475

75-
while ((opt = getopt(argc, argv, "a:c:p:m:vhn:")) != -1) {
76+
while ((opt = getopt(argc, argv, "a:c:p:m:vhdn:")) != -1) {
7677
switch (opt) {
7778
case 'a':
7879
addr = optarg;
@@ -88,6 +89,9 @@ int main (int argc, char **argv) {
8889
case 'v':
8990
debug = 1;
9091
break;
92+
case 'd':
93+
daemon = 1;
94+
break;
9195
case 'h':
9296
print_help(argv[0]);
9397
exit(EXIT_SUCCESS);
@@ -107,6 +111,9 @@ int main (int argc, char **argv) {
107111

108112
sol_log_init(conf->logpath);
109113

114+
if (daemon == 1)
115+
daemonize();
116+
110117
// Print configuration
111118
config_print();
112119

0 commit comments

Comments
 (0)