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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
1998-03-14 16:00:09 +03:00
( 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
2007-07-10 04:52:41 +04:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
1998-03-14 16:00:09 +03:00
*/
# include "includes.h"
2011-02-26 01:20:06 +03:00
# include "system/filesys.h"
1998-03-14 16:00:09 +03:00
# ifndef O_NONBLOCK
# define O_NONBLOCK
# endif
2009-06-18 13:45:57 +04:00
static char * pidFile_name = NULL ;
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 ;
2007-06-24 17:40:58 +04:00
char * pidFile ;
1998-07-03 03:57:56 +04:00
2007-06-24 17:40:58 +04:00
if ( asprintf ( & pidFile , " %s/%s.pid " , lp_piddir ( ) , name ) = = - 1 ) {
return 0 ;
}
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 ) {
2007-06-24 17:40:58 +04:00
SAFE_FREE ( pidFile ) ;
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
2007-06-24 17:40:58 +04:00
SAFE_FREE ( pidFile ) ;
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 ) ;
2007-06-24 17:40:58 +04:00
SAFE_FREE ( pidFile ) ;
1998-12-05 11:09:59 +03:00
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 */
2006-11-02 12:37:52 +03:00
void pidfile_create ( const char * program_name )
1998-03-14 16:00:09 +03:00
{
int fd ;
char buf [ 20 ] ;
2007-12-10 22:30:37 +03:00
const char * short_configfile ;
2007-06-24 17:40:58 +04:00
char * name ;
1999-12-13 16:27:58 +03:00
pid_t pid ;
1998-03-14 16:00:09 +03:00
2006-11-02 12:37:52 +03:00
/* Add a suffix to the program name if this is a process with a
* none default configuration file name . */
2009-07-03 13:35:11 +04:00
if ( strcmp ( CONFIGFILE , get_dyn_CONFIGFILE ( ) ) = = 0 ) {
2007-06-24 17:40:58 +04:00
name = SMB_STRDUP ( program_name ) ;
2006-11-02 12:37:52 +03:00
} else {
2007-12-10 22:30:37 +03:00
short_configfile = strrchr ( get_dyn_CONFIGFILE ( ) , ' / ' ) ;
2007-04-07 02:08:44 +04:00
if ( short_configfile = = NULL ) {
/* conf file in current directory */
2007-12-10 22:30:37 +03:00
short_configfile = get_dyn_CONFIGFILE ( ) ;
2007-04-07 02:08:44 +04:00
} else {
/* full/relative path provided */
short_configfile + + ;
}
2007-06-24 17:40:58 +04:00
if ( asprintf ( & name , " %s-%s " , program_name ,
2007-07-23 23:09:19 +04:00
short_configfile ) = = - 1 ) {
2007-06-24 17:40:58 +04:00
smb_panic ( " asprintf failed " ) ;
}
2006-11-02 12:37:52 +03:00
}
2009-06-18 13:45:57 +04:00
if ( asprintf ( & pidFile_name , " %s/%s.pid " , lp_piddir ( ) , name ) = = - 1 ) {
2007-06-24 17:40:58 +04:00
smb_panic ( " asprintf failed " ) ;
}
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 " ,
2009-06-18 13:45:57 +04:00
name , pidFile_name , ( int ) pid ) ) ;
1998-12-05 11:09:59 +03:00
exit ( 1 ) ;
}
2009-06-18 13:45:57 +04:00
fd = sys_open ( pidFile_name , O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL ,
0644 ) ;
1998-12-05 11:09:59 +03:00
if ( fd = = - 1 ) {
2009-06-18 13:45:57 +04:00
DEBUG ( 0 , ( " ERROR: can't open %s: Error was %s \n " , pidFile_name ,
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 " ,
2009-06-18 13:45:57 +04:00
name , pidFile_name , 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 " ,
2009-06-18 13:45:57 +04:00
pidFile_name , strerror ( errno ) ) ) ;
1998-03-14 16:00:09 +03:00
exit ( 1 ) ;
}
/* Leave pid file open & locked for the duration... */
2007-06-24 17:40:58 +04:00
SAFE_FREE ( name ) ;
2010-05-17 20:22:37 +04:00
/* set the close on exec so that we don't leak the fd */
fcntl ( fd , F_SETFD , FD_CLOEXEC ) ;
2009-06-18 13:45:57 +04:00
}
void pidfile_unlink ( void )
{
if ( pidFile_name = = NULL ) {
return ;
}
unlink ( pidFile_name ) ;
SAFE_FREE ( pidFile_name ) ;
1998-03-14 16:00:09 +03:00
}