2626#include <syslog.h>
2727#include <sys/stat.h>
2828#include <sys/resource.h>
29+ #include "assert.h"
2930
3031enum {
3132 STATUS_OK = 1 ,
@@ -35,17 +36,16 @@ enum {
3536/* ========================================================================== *
3637 * Set mask to 0, let a new file may has any privilege
3738 * ========================================================================== */
38- int
39+ static int
3940setMask (void )
4041{
41- umask (0 );
42- return STATUS_OK ;
42+ return umask (0 ) ? STATUS_ERROR : STATUS_OK ;
4343}
4444
4545/* ========================================================================== *
4646 * Change work directory to root
4747 * ========================================================================== */
48- int
48+ static int
4949chrootdir (void )
5050{
5151 const char * const rootdir = "/" ;
@@ -62,10 +62,10 @@ chrootdir (void)
6262/* ========================================================================== *
6363 * Create a new session
6464 * ========================================================================== */
65- int
65+ static int
6666newSession (void )
6767{
68- pid_t pid ;
68+ pid_t pid = 0 ;
6969
7070 if ((pid = fork ()) < 0 )
7171 {
@@ -90,11 +90,13 @@ newSession (void)
9090/* ========================================================================== *
9191 * Disable controlling terminal
9292 * ========================================================================== */
93- int
93+ static int
9494noTTY (void )
9595{
96- struct sigaction action ;
97- pid_t pid ;
96+ struct sigaction action = { 0 };
97+ pid_t pid = 0 ;
98+
99+ memset (& action , 0 , sizeof (struct sigaction ));
98100
99101 action .sa_handler = SIG_IGN ;
100102 sigemptyset (& action .sa_mask );
@@ -123,18 +125,20 @@ noTTY (void)
123125/* ========================================================================== *
124126 * Close all file descriptor
125127 * ========================================================================== */
126- int
128+ static int
127129closeFD (void )
128130{
129- struct rlimit limit ;
131+ struct rlimit limit = { 0 };
132+ int fd = 0 ;
133+
134+ memset (& limit , 0 , sizeof (struct rlimit ));
130135
131136 if (getrlimit (RLIMIT_NOFILE , & limit ) < 0 )
132137 {
133138 perror ("getrlimit falied:" );
134139 return STATUS_ERROR ;
135140 }
136141
137- int fd = 0 ;
138142 while (fd < (int )(RLIM_INFINITY == limit .rlim_max ? 1024 : limit .rlim_max ))
139143 {
140144 close (fd ++ );
@@ -147,11 +151,11 @@ closeFD (void)
147151 * Reopen stdin, stdout and stderr to /dev/null
148152 * Invoke this after closeFD
149153 * ========================================================================== */
150- int
154+ static int
151155reopen (void )
152156{
153157 const char * const nullFile = "/dev/null" ;
154- int in , out , err ;
158+ int in = 0 , out = 0 , err = 0 ;
155159
156160 in = open (nullFile , O_RDWR );
157161 out = dup (in );
@@ -169,7 +173,7 @@ reopen (void)
169173/* ========================================================================== *
170174 * Init log
171175 * ========================================================================== */
172- int
176+ static int
173177initLog (const char * const ident )
174178{
175179 openlog (ident , LOG_CONS , LOG_DAEMON );
@@ -185,6 +189,8 @@ daemonize (const char * const ident)
185189{
186190 int status = 0 ;
187191
192+ /* Also work well when ident is NULL so here no need to check */
193+
188194 status |= setMask ();
189195 status |= newSession ();
190196 status |= noTTY ();
0 commit comments