- /* Add a suffix to the program name if this is a process with a
- * none default configuration file name. */
- if (strcmp( CONFIGFILE, get_dyn_CONFIGFILE()) == 0) {
- name = SMB_STRDUP(program_name);
- } else {
- short_configfile = strrchr( get_dyn_CONFIGFILE(), '/');
- if (short_configfile == NULL) {
- /* conf file in current directory */
- short_configfile = get_dyn_CONFIGFILE();
- } else {
- /* full/relative path provided */
- short_configfile++;
- }
- if (asprintf(&name, "%s-%s", program_name,
- short_configfile) == -1) {
- smb_panic("asprintf failed");
- }
- }
-
- if (asprintf(&pidFile_name, "%s/%s.pid", lp_piddir(), name) == -1) {
- smb_panic("asprintf failed");
- }
-
- pid = pidfile_pid_s3(program_name);
- if (pid != 0) {
- DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n",
- name, pidFile_name, (int)pid));
- exit(1);
- }
-
- fd = open(pidFile_name, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL,
- 0644);
- if (fd == -1) {
- DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile_name,
- strerror(errno)));
- exit(1);
- }
-
- 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_name, strerror(errno)));
- exit(1);
- }
-
- memset(buf, 0, sizeof(buf));
- slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int)getpid());
- if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
- DEBUG(0,("ERROR: can't write to file %s: %s\n",
- pidFile_name, strerror(errno)));
- exit(1);
- }
- /* Leave pid file open & locked for the duration... */