Skip to content

Commit 5a7796a

Browse files
committed
Daemonize BESS
- By default bessd now starts daemonized. - Use -w to run without daemonization. - When daemonized output goes to syslogd
1 parent c38b684 commit 5a7796a

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

core/main.c

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <assert.h>
22
#include <stdio.h>
3+
#include <stdlib.h>
34
#include <fcntl.h>
45
#include <stdlib.h>
56
#include <unistd.h>
@@ -13,6 +14,7 @@
1314
#include "master.h"
1415
#include "worker.h"
1516
#include "driver.h"
17+
#include "syslog.h"
1618

1719
#if 0
1820
#include "old_modules/vport_guest_sndrv.h"
@@ -908,6 +910,8 @@ static struct {
908910
uint16_t port;
909911
} cmdline_opts;
910912

913+
static int daemonize = 1;
914+
911915
static void parse_core_list()
912916
{
913917
char *ptr;
@@ -939,6 +943,8 @@ static void print_usage(char *exec_name)
939943
fprintf(stderr, " %-16s Specifies the TCP port on which SoftNIC listens"
940944
"for controller connections\n",
941945
"-p <port>");
946+
fprintf(stderr, " %-16s Do not daemonize BESS",
947+
"-w");
942948

943949
exit(2);
944950
}
@@ -951,7 +957,7 @@ static void init_config(int argc, char **argv)
951957

952958
num_workers = 0;
953959

954-
while ((c = getopt(argc, argv, ":tc:p:")) != -1) {
960+
while ((c = getopt(argc, argv, ":tc:p:w")) != -1) {
955961
switch (c) {
956962
case 't':
957963
dump_types();
@@ -965,6 +971,9 @@ static void init_config(int argc, char **argv)
965971
case 'p':
966972
sscanf(optarg, "%hu", &cmdline_opts.port);
967973
break;
974+
case 'w':
975+
daemonize = 0;
976+
break;
968977

969978
case ':':
970979
fprintf(stderr, "argument is required for -%c\n",
@@ -991,7 +1000,30 @@ static void init_config(int argc, char **argv)
9911000

9921001
int main(int argc, char **argv)
9931002
{
1003+
pid_t pid, sid;
9941004
init_config(argc, argv);
1005+
1006+
if (daemonize) {
1007+
pid = fork();
1008+
if (pid < 0) {
1009+
fprintf(stderr, "Could not fork damon\n");
1010+
exit(EXIT_FAILURE);
1011+
}
1012+
if (pid > 0) {
1013+
exit(EXIT_SUCCESS);
1014+
}
1015+
// Reparent
1016+
sid = setsid();
1017+
if (sid < 0) {
1018+
fprintf(stderr, "Could not set SID\n");
1019+
exit(EXIT_FAILURE);
1020+
}
1021+
1022+
close(STDIN_FILENO);
1023+
close(STDERR_FILENO);
1024+
close(STDOUT_FILENO);
1025+
setup_syslog();
1026+
}
9951027
init_dpdk(argv[0]);
9961028
init_mempool();
9971029
init_drivers();
@@ -1005,5 +1037,8 @@ int main(int argc, char **argv)
10051037
rte_eal_mp_wait_lcore();
10061038
close_mempool();
10071039

1040+
if (daemonize)
1041+
end_syslog();
1042+
10081043
return 0;
10091044
}

core/syslog.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <syslog.h>
2+
#ifndef __SYSLOG_H__
3+
#define __SYSLOG_H__
4+
static ssize_t stdout_writer(void *cookie, const char *data, size_t len)
5+
{
6+
syslog(LOG_INFO, "%.*s", (int)len, data);
7+
return len;
8+
}
9+
10+
static ssize_t stderr_writer(void *cookie, const char *data, size_t len)
11+
{
12+
syslog(LOG_ERR, "%.*s", (int)len, data);
13+
return len;
14+
}
15+
16+
#define BESS_ID "bessd"
17+
static void setup_syslog()
18+
{
19+
cookie_io_functions_t stdout_funcs = {
20+
.write = &stdout_writer,
21+
.read = NULL,
22+
.close = NULL,
23+
.seek = NULL
24+
};
25+
26+
cookie_io_functions_t stderr_funcs = {
27+
.write = &stderr_writer,
28+
.read = NULL,
29+
.close = NULL,
30+
.seek = NULL
31+
};
32+
33+
openlog(BESS_ID, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
34+
35+
setvbuf(stderr = fopencookie(NULL, "w", stderr_funcs), NULL, _IOLBF, 0);
36+
37+
setvbuf(stdout = fopencookie(NULL, "w", stdout_funcs), NULL, _IOLBF, 0);
38+
}
39+
40+
static void end_syslog()
41+
{
42+
closelog();
43+
}
44+
#endif

0 commit comments

Comments
 (0)