* return the pid in a pidfile. return 0 if the process (or pidfile)
* does not exist
*/
-pid_t pidfile_pid(const char *name)
+pid_t pidfile_pid(const char *piddir, const char *name)
{
int fd;
char pidstr[20];
pid_t ret;
char *pidFile;
- asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name);
+ if (asprintf(&pidFile, "%s/%s.pid", piddir, name) < 0) {
+ return 0;
+ }
fd = open(pidFile, O_NONBLOCK | O_RDONLY, 0644);
ret = (pid_t)atoi(pidstr);
- if (!process_exists(ret)) {
+ if (!process_exists_by_pid(ret)) {
goto noproc;
}
/**
* create a pid file in the pid directory. open it and leave it locked
*/
-void pidfile_create(const char *name)
+void pidfile_create(const char *piddir, const char *name)
{
int fd;
char buf[20];
char *pidFile;
pid_t pid;
- asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name);
+ if (asprintf(&pidFile, "%s/%s.pid", piddir, name) < 0) {
+ DEBUG(0,("ERROR: Out of memory\n"));
+ exit(1);
+ }
- pid = pidfile_pid(name);
+ pid = pidfile_pid(piddir, name);
if (pid != 0) {
DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n",
name, pidFile, (int)pid));
exit(1);
}
- if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)==False) {
+ if (fcntl_lock(fd,F_SETLK,0,1,F_WRLCK)==false) {
DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n",
name, pidFile, strerror(errno)));
exit(1);