2000-05-27 09:53:11 +00:00
/* this code is broken - there is a race condition with the unlink (tridge) */
1998-03-14 13:00:09 +00:00
/*
2002-01-30 06:08:46 +00:00
Unix SMB / CIFS implementation .
1998-03-14 13:00:09 +00:00
pidfile handling
Copyright ( C ) Andrew Tridgell 1998
This program is free software ; you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation ; either version 2 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program ; if not , write to the Free Software
Foundation , Inc . , 675 Mass Ave , Cambridge , MA 0213 9 , USA .
*/
# include "includes.h"
# ifndef O_NONBLOCK
# define O_NONBLOCK
# endif
1998-07-02 23:57:56 +00:00
/* return the pid in a pidfile. return 0 if the process (or pidfile)
does not exist */
1998-09-28 21:43:48 +00:00
pid_t pidfile_pid ( char * name )
1998-07-02 23:57:56 +00:00
{
1998-12-05 08:09:59 +00:00
int fd ;
char pidstr [ 20 ] ;
1998-07-02 23:57:56 +00:00
unsigned ret ;
pstring pidFile ;
2002-07-15 10:35:28 +00:00
slprintf ( pidFile , sizeof ( pidFile ) - 1 , " %s/%s.pid " , lp_piddir ( ) , name ) ;
1998-07-02 23:57:56 +00:00
2001-08-01 17:43:57 +00:00
fd = sys_open ( pidFile , O_NONBLOCK | O_RDONLY , 0644 ) ;
1998-12-05 08:09:59 +00:00
if ( fd = = - 1 ) {
1998-07-02 23:57:56 +00:00
return 0 ;
}
1998-12-05 08:09:59 +00:00
ZERO_ARRAY ( pidstr ) ;
if ( read ( fd , pidstr , sizeof ( pidstr ) - 1 ) < = 0 ) {
2001-08-01 17:32:45 +00:00
goto noproc ;
1998-07-02 23:57:56 +00:00
}
1998-12-05 08:09:59 +00:00
ret = atoi ( pidstr ) ;
1998-07-02 23:57:56 +00:00
1999-12-13 13:27:58 +00:00
if ( ! process_exists ( ( pid_t ) ret ) ) {
2001-08-01 17:32:45 +00:00
goto noproc ;
1998-12-05 08:09:59 +00:00
}
2001-08-01 17:32:45 +00:00
if ( fcntl_lock ( fd , SMB_F_SETLK , 0 , 1 , F_RDLCK ) ) {
1998-12-05 08:09:59 +00:00
/* we could get the lock - it can't be a Samba process */
2001-08-01 17:32:45 +00:00
goto noproc ;
1998-12-05 08:09:59 +00:00
}
1998-07-02 23:57:56 +00:00
1998-12-05 08:09:59 +00:00
close ( fd ) ;
1998-09-28 21:43:48 +00:00
return ( pid_t ) ret ;
1998-12-05 08:09:59 +00:00
2001-08-01 17:32:45 +00:00
noproc :
1998-12-05 08:09:59 +00:00
close ( fd ) ;
unlink ( pidFile ) ;
return 0 ;
1998-07-02 23:57:56 +00:00
}
1998-03-14 13:00:09 +00:00
2002-07-15 10:35:28 +00:00
/* create a pid file in the pid directory. open it and leave it locked */
1998-03-14 13:00:09 +00:00
void pidfile_create ( char * name )
{
int fd ;
char buf [ 20 ] ;
pstring pidFile ;
1999-12-13 13:27:58 +00:00
pid_t pid ;
1998-03-14 13:00:09 +00:00
2002-07-15 10:35:28 +00:00
slprintf ( pidFile , sizeof ( pidFile ) - 1 , " %s/%s.pid " , lp_piddir ( ) , name ) ;
1998-03-14 13:00:09 +00:00
1998-03-15 02:37:52 +00:00
pid = pidfile_pid ( name ) ;
1998-12-05 08:09:59 +00:00
if ( pid ! = 0 ) {
DEBUG ( 0 , ( " ERROR: %s is already running. File %s exists and process id %d is running. \n " ,
1999-12-13 13:27:58 +00:00
name , pidFile , ( int ) pid ) ) ;
1998-12-05 08:09:59 +00:00
exit ( 1 ) ;
}
fd = sys_open ( pidFile , O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL , 0644 ) ;
if ( fd = = - 1 ) {
1998-07-02 23:57:56 +00:00
DEBUG ( 0 , ( " ERROR: can't open %s: Error was %s \n " , pidFile ,
1998-03-14 13:00:09 +00:00
strerror ( errno ) ) ) ;
exit ( 1 ) ;
}
1998-09-04 00:23:28 +00:00
if ( fcntl_lock ( fd , SMB_F_SETLK , 0 , 1 , F_WRLCK ) = = False ) {
1998-07-02 23:57:56 +00:00
DEBUG ( 0 , ( " ERROR: %s : fcntl lock of file %s failed. Error was %s \n " ,
name , pidFile , strerror ( errno ) ) ) ;
1998-03-14 13:00:09 +00:00
exit ( 1 ) ;
}
1998-03-15 02:37:52 +00:00
memset ( buf , 0 , sizeof ( buf ) ) ;
2000-05-02 02:23:41 +00:00
slprintf ( buf , sizeof ( buf ) - 1 , " %u \n " , ( unsigned int ) sys_getpid ( ) ) ;
2002-10-21 00:20:12 +00:00
if ( write ( fd , buf , strlen ( buf ) ) ! = strlen ( buf ) ) {
1998-07-02 23:57:56 +00:00
DEBUG ( 0 , ( " ERROR: can't write to file %s: %s \n " ,
1998-03-14 13:00:09 +00:00
pidFile , strerror ( errno ) ) ) ;
exit ( 1 ) ;
}
/* Leave pid file open & locked for the duration... */
}