11#include <assert.h>
22#include <stdio.h>
3+ #include <stdlib.h>
34#include <fcntl.h>
45#include <stdlib.h>
56#include <unistd.h>
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+
911915static 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
9921001int 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}
0 commit comments