Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion core/main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
Expand All @@ -13,6 +14,7 @@
#include "master.h"
#include "worker.h"
#include "driver.h"
#include "syslog.h"

#if 0
#include "old_modules/vport_guest_sndrv.h"
Expand Down Expand Up @@ -908,6 +910,8 @@ static struct {
uint16_t port;
} cmdline_opts;

static int daemonize = 1;

static void parse_core_list()
{
char *ptr;
Expand Down Expand Up @@ -939,6 +943,8 @@ static void print_usage(char *exec_name)
fprintf(stderr, " %-16s Specifies the TCP port on which SoftNIC listens"
"for controller connections\n",
"-p <port>");
fprintf(stderr, " %-16s Do not daemonize BESS",
"-w");

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

num_workers = 0;

while ((c = getopt(argc, argv, ":tc:p:")) != -1) {
while ((c = getopt(argc, argv, ":tc:p:w")) != -1) {
switch (c) {
case 't':
dump_types();
Expand All @@ -965,6 +971,9 @@ static void init_config(int argc, char **argv)
case 'p':
sscanf(optarg, "%hu", &cmdline_opts.port);
break;
case 'w':
daemonize = 0;
break;

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

int main(int argc, char **argv)
{
pid_t pid, sid;
init_config(argc, argv);

if (daemonize) {
pid = fork();
if (pid < 0) {
fprintf(stderr, "Could not fork damon\n");
exit(EXIT_FAILURE);
}
if (pid > 0) {
exit(EXIT_SUCCESS);
}
// Reparent
sid = setsid();
if (sid < 0) {
fprintf(stderr, "Could not set SID\n");
exit(EXIT_FAILURE);
}

close(STDIN_FILENO);
close(STDERR_FILENO);
close(STDOUT_FILENO);
setup_syslog();
}
init_dpdk(argv[0]);
init_mempool();
init_drivers();
Expand All @@ -1005,5 +1037,8 @@ int main(int argc, char **argv)
rte_eal_mp_wait_lcore();
close_mempool();

if (daemonize)
end_syslog();

return 0;
}
44 changes: 44 additions & 0 deletions core/syslog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <syslog.h>
#ifndef __SYSLOG_H__
#define __SYSLOG_H__
static ssize_t stdout_writer(void *cookie, const char *data, size_t len)
{
syslog(LOG_INFO, "%.*s", (int)len, data);
return len;
}

static ssize_t stderr_writer(void *cookie, const char *data, size_t len)
{
syslog(LOG_ERR, "%.*s", (int)len, data);
return len;
}

#define BESS_ID "bessd"
static void setup_syslog()
{
cookie_io_functions_t stdout_funcs = {
.write = &stdout_writer,
.read = NULL,
.close = NULL,
.seek = NULL
};

cookie_io_functions_t stderr_funcs = {
.write = &stderr_writer,
.read = NULL,
.close = NULL,
.seek = NULL
};

openlog(BESS_ID, LOG_CONS | LOG_NDELAY, LOG_DAEMON);

setvbuf(stderr = fopencookie(NULL, "w", stderr_funcs), NULL, _IOLBF, 0);

setvbuf(stdout = fopencookie(NULL, "w", stdout_funcs), NULL, _IOLBF, 0);
}

static void end_syslog()
{
closelog();
}
#endif