2000-05-27 13:53:11 +04:00
/* this code is broken - there is a race condition with the unlink (tridge) */
1998-03-14 16:00:09 +03:00
/*
2002-01-30 09:08:46 +03:00
Unix SMB / CIFS implementation .
1998-03-14 16:00:09 +03: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-03 03:57:56 +04:00
/* return the pid in a pidfile. return 0 if the process (or pidfile)
does not exist */
2003-01-03 11:28:12 +03:00
pid_t pidfile_pid ( const char * name )
1998-07-03 03:57:56 +04:00
{
1998-12-05 11:09:59 +03:00
int fd ;
char pidstr [ 20 ] ;
2006-02-04 01:19:41 +03:00
pid_t pid ;
unsigned int ret ;
1998-07-03 03:57:56 +04:00
pstring pidFile ;
2002-07-15 14:35:28 +04:00
slprintf ( pidFile , sizeof ( pidFile ) - 1 , " %s/%s.pid " , lp_piddir ( ) , name ) ;
1998-07-03 03:57:56 +04:00
2001-08-01 21:43:57 +04:00
fd = sys_open ( pidFile , O_NONBLOCK | O_RDONLY , 0644 ) ;
1998-12-05 11:09:59 +03:00
if ( fd = = - 1 ) {
1998-07-03 03:57:56 +04:00
return 0 ;
}
1998-12-05 11:09:59 +03:00
ZERO_ARRAY ( pidstr ) ;
if ( read ( fd , pidstr , sizeof ( pidstr ) - 1 ) < = 0 ) {
2001-08-01 21:32:45 +04:00
goto noproc ;
1998-07-03 03:57:56 +04:00
}
1998-12-05 11:09:59 +03:00
ret = atoi ( pidstr ) ;
2004-08-25 00:58:12 +04:00
if ( ret = = 0 ) {
/* Obviously we had some garbage in the pidfile... */
DEBUG ( 1 , ( " Could not parse contents of pidfile %s \n " ,
pidFile ) ) ;
goto noproc ;
}
1998-07-03 03:57:56 +04:00
2006-02-04 01:19:41 +03:00
pid = ( pid_t ) ret ;
if ( ! process_exists_by_pid ( pid ) ) {
2001-08-01 21:32:45 +04:00
goto noproc ;
1998-12-05 11:09:59 +03:00
}
2001-08-01 21:32:45 +04:00
if ( fcntl_lock ( fd , SMB_F_SETLK , 0 , 1 , F_RDLCK ) ) {
1998-12-05 11:09:59 +03:00
/* we could get the lock - it can't be a Samba process */
2001-08-01 21:32:45 +04:00
goto noproc ;
1998-12-05 11:09:59 +03:00
}
1998-07-03 03:57:56 +04:00
1998-12-05 11:09:59 +03:00
close ( fd ) ;
1998-09-29 01:43:48 +04:00
return ( pid_t ) ret ;
1998-12-05 11:09:59 +03:00
2001-08-01 21:32:45 +04:00
noproc :
1998-12-05 11:09:59 +03:00
close ( fd ) ;
unlink ( pidFile ) ;
return 0 ;
1998-07-03 03:57:56 +04:00
}
1998-03-14 16:00:09 +03:00
2002-07-15 14:35:28 +04:00
/* create a pid file in the pid directory. open it and leave it locked */
2003-01-03 11:28:12 +03:00
void pidfile_create ( const char * name )
1998-03-14 16:00:09 +03:00
{
int fd ;
char buf [ 20 ] ;
pstring pidFile ;
1999-12-13 16:27:58 +03:00
pid_t pid ;
1998-03-14 16:00:09 +03:00
2002-07-15 14:35:28 +04:00
slprintf ( pidFile , sizeof ( pidFile ) - 1 , " %s/%s.pid " , lp_piddir ( ) , name ) ;
1998-03-14 16:00:09 +03:00
1998-03-15 05:37:52 +03:00
pid = pidfile_pid ( name ) ;
1998-12-05 11:09:59 +03:00
if ( pid ! = 0 ) {
DEBUG ( 0 , ( " ERROR: %s is already running. File %s exists and process id %d is running. \n " ,
1999-12-13 16:27:58 +03:00
name , pidFile , ( int ) pid ) ) ;
1998-12-05 11:09:59 +03:00
exit ( 1 ) ;
}
fd = sys_open ( pidFile , O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL , 0644 ) ;
if ( fd = = - 1 ) {
1998-07-03 03:57:56 +04:00
DEBUG ( 0 , ( " ERROR: can't open %s: Error was %s \n " , pidFile ,
1998-03-14 16:00:09 +03:00
strerror ( errno ) ) ) ;
exit ( 1 ) ;
}
1998-09-04 04:23:28 +04:00
if ( fcntl_lock ( fd , SMB_F_SETLK , 0 , 1 , F_WRLCK ) = = False ) {
1998-07-03 03:57:56 +04:00
DEBUG ( 0 , ( " ERROR: %s : fcntl lock of file %s failed. Error was %s \n " ,
name , pidFile , strerror ( errno ) ) ) ;
1998-03-14 16:00:09 +03:00
exit ( 1 ) ;
}
1998-03-15 05:37:52 +03:00
memset ( buf , 0 , sizeof ( buf ) ) ;
2000-05-02 06:23:41 +04:00
slprintf ( buf , sizeof ( buf ) - 1 , " %u \n " , ( unsigned int ) sys_getpid ( ) ) ;
2003-02-24 06:09:08 +03:00
if ( write ( fd , buf , strlen ( buf ) ) ! = ( ssize_t ) strlen ( buf ) ) {
1998-07-03 03:57:56 +04:00
DEBUG ( 0 , ( " ERROR: can't write to file %s: %s \n " ,
1998-03-14 16:00:09 +03:00
pidFile , strerror ( errno ) ) ) ;
exit ( 1 ) ;
}
/* Leave pid file open & locked for the duration... */
}