2929/*
3030 * This should ultimately go in a header file.
3131 */
32- void daemon_init (const char * prog , int nofork );
32+ void daemon_init (const char * prog , const char * pid_file , int nofork );
3333void daemon_cleanup (void );
34- int check_process_running (const char * prog , pid_t * pid );
34+ int check_process_running (const char * cmd , const char * pid_file , pid_t * pid );
3535
3636/*
3737 * Local prototypes.
3838 */
39- static void update_pidfile (const char * prog );
39+ static void update_pidfile (const char * filename );
4040static int setup_sigmask (void );
4141static char pid_filename [PATH_MAX ];
4242
@@ -99,12 +99,10 @@ check_pid_valid(pid_t pid, const char *prog)
9999
100100
101101int
102- check_process_running (const char * prog , pid_t * pid )
102+ check_process_running (const char * cmd , const char * filename , pid_t * pid )
103103{
104104 pid_t oldpid ;
105105 FILE * fp ;
106- char filename [PATH_MAX ];
107- char * cmd ;
108106 int ret ;
109107 struct stat st ;
110108
@@ -114,11 +112,6 @@ check_process_running(const char *prog, pid_t * pid)
114112 * Now see if there is a pidfile associated with this cmd in /var/run
115113 */
116114 fp = NULL ;
117- memset (filename , 0 , PATH_MAX );
118-
119- cmd = basename ((char * )prog );
120- snprintf (filename , sizeof (filename ), "/var/run/%s.pid" , cmd );
121-
122115 ret = stat (filename , & st );
123116 if ((ret < 0 ) || (!st .st_size ))
124117 return 0 ;
@@ -146,15 +139,11 @@ check_process_running(const char *prog, pid_t * pid)
146139
147140
148141static void
149- update_pidfile (const char * prog )
142+ update_pidfile (const char * filename )
150143{
151144 FILE * fp = NULL ;
152- char * cmd ;
153-
154- memset (pid_filename , 0 , PATH_MAX );
155145
156- cmd = basename ((char * )prog );
157- snprintf (pid_filename , sizeof (pid_filename ), "/var/run/%s.pid" , cmd );
146+ strncpy (pid_filename , filename , PATH_MAX );
158147
159148 fp = fopen (pid_filename , "w" );
160149 if (fp == NULL ) {
@@ -197,28 +186,16 @@ setup_sigmask(void)
197186
198187
199188void
200- daemon_init (const char * prog , int nofork )
189+ daemon_init (const char * prog , const char * pid_file , int nofork )
201190{
202- uid_t uid ;
203191 pid_t pid ;
204192
205- uid = getuid ();
206- if (uid ) {
207- syslog (LOG_ERR ,
208- "daemon_init: Sorry, only root wants to run this.\n" );
209- exit (1 );
210- }
211-
212- /* Prevents multiple instances of the daemon from running.
213- needs to be fixed by the devs.
214- */
215- /* if (check_process_running(prog, &pid) && (pid != getpid())) {
193+ if (check_process_running (prog , pid_file , & pid ) && (pid != getpid ())) {
216194 syslog (LOG_ERR ,
217195 "daemon_init: Process \"%s\" already running.\n" ,
218196 prog );
219197 exit (1 );
220198 }
221- */
222199
223200 if (setup_sigmask () < 0 ) {
224201 syslog (LOG_ERR , "daemon_init: Unable to set signal mask.\n" );
@@ -230,8 +207,7 @@ daemon_init(const char *prog, int nofork)
230207 exit (1 );
231208 }
232209
233- /* update_pidfile(prog); */
234-
210+ update_pidfile (pid_file );
235211}
236212
237213
0 commit comments