00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "includes.h"
00024
00025 #ifndef O_NONBLOCK
00026 #define O_NONBLOCK
00027 #endif
00028
00029
00030
00031 pid_t pidfile_pid(const char *name)
00032 {
00033 int fd;
00034 char pidstr[20];
00035 pid_t pid;
00036 unsigned int ret;
00037 pstring pidFile;
00038
00039 slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_piddir(), name);
00040
00041 fd = sys_open(pidFile, O_NONBLOCK | O_RDONLY, 0644);
00042 if (fd == -1) {
00043 return 0;
00044 }
00045
00046 ZERO_ARRAY(pidstr);
00047
00048 if (read(fd, pidstr, sizeof(pidstr)-1) <= 0) {
00049 goto noproc;
00050 }
00051
00052 ret = atoi(pidstr);
00053
00054 if (ret == 0) {
00055
00056 DEBUG(1, ("Could not parse contents of pidfile %s\n",
00057 pidFile));
00058 goto noproc;
00059 }
00060
00061 pid = (pid_t)ret;
00062 if (!process_exists_by_pid(pid)) {
00063 goto noproc;
00064 }
00065
00066 if (fcntl_lock(fd,SMB_F_SETLK,0,1,F_RDLCK)) {
00067
00068 goto noproc;
00069 }
00070
00071 close(fd);
00072 return (pid_t)ret;
00073
00074 noproc:
00075 close(fd);
00076 unlink(pidFile);
00077 return 0;
00078 }
00079
00080
00081 void pidfile_create(const char *program_name)
00082 {
00083 int fd;
00084 char buf[20];
00085 char *short_configfile;
00086 pstring name;
00087 pstring pidFile;
00088 pid_t pid;
00089
00090
00091
00092 if (strcmp( CONFIGFILE, dyn_CONFIGFILE) == 0) {
00093 strncpy( name, program_name, sizeof( name)-1);
00094 } else {
00095 short_configfile = strrchr( dyn_CONFIGFILE, '/');
00096 if (short_configfile == NULL) {
00097
00098 short_configfile = dyn_CONFIGFILE;
00099 } else {
00100
00101 short_configfile++;
00102 }
00103 slprintf( name, sizeof( name)-1, "%s-%s", program_name,
00104 short_configfile );
00105 }
00106
00107 slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_piddir(), name);
00108
00109 pid = pidfile_pid(name);
00110 if (pid != 0) {
00111 DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n",
00112 name, pidFile, (int)pid));
00113 exit(1);
00114 }
00115
00116 fd = sys_open(pidFile, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL, 0644);
00117 if (fd == -1) {
00118 DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile,
00119 strerror(errno)));
00120 exit(1);
00121 }
00122
00123 if (fcntl_lock(fd,SMB_F_SETLK,0,1,F_WRLCK)==False) {
00124 DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n",
00125 name, pidFile, strerror(errno)));
00126 exit(1);
00127 }
00128
00129 memset(buf, 0, sizeof(buf));
00130 slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int) sys_getpid());
00131 if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
00132 DEBUG(0,("ERROR: can't write to file %s: %s\n",
00133 pidFile, strerror(errno)));
00134 exit(1);
00135 }
00136
00137 }