Make the s3 pidfile use the common code inside lib/util/pidfile.c
[Samba.git] / source3 / lib / pidfile.c
blob79ea3a5330066b23ca94273fe0b7896b0b10a4de
1 /* this code is broken - there is a race condition with the unlink (tridge) */
3 /*
4 Unix SMB/CIFS implementation.
5 pidfile handling
6 Copyright (C) Andrew Tridgell 1998
7 Copyright (C) Jeremy Allison 2012
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
24 #include "system/filesys.h"
25 #include "../lib/util/pidfile.h"
27 /* Malloc a pidfile name. */
28 static char *get_pidfile_name(const char *program_name)
30 char *name = NULL;
32 /* Add a suffix to the program name if this is a process with a
33 * none default configuration file name. */
34 if (strcmp( CONFIGFILE, get_dyn_CONFIGFILE()) == 0) {
35 name = SMB_STRDUP(program_name);
36 } else {
37 const char *short_configfile;
38 short_configfile = strrchr( get_dyn_CONFIGFILE(), '/');
39 if (short_configfile == NULL) {
40 /* conf file in current directory */
41 short_configfile = get_dyn_CONFIGFILE();
42 } else {
43 /* full/relative path provided */
44 short_configfile++;
46 if (asprintf(&name, "%s-%s", program_name,
47 short_configfile) == -1) {
48 smb_panic("asprintf failed");
51 return name;
54 /* return the pid in a pidfile. return 0 if the process (or pidfile)
55 does not exist */
56 pid_t pidfile_pid_s3(const char *program_name)
58 pid_t pid = 0;
59 char *name = get_pidfile_name(program_name);
61 pid = pidfile_pid(lp_piddir(), name);
62 SAFE_FREE(name);
63 return pid;
66 /* create a pid file in the pid directory. open it and leave it locked */
67 void pidfile_create_s3(const char *program_name)
69 char *name = get_pidfile_name(program_name);
71 pidfile_create(lp_piddir(), name);
72 SAFE_FREE(name);
75 /* Remove a pidfile. */
76 void pidfile_unlink_s3(const char *program_name)
78 char *name = get_pidfile_name(program_name);
79 pidfile_unlink(lp_piddir(), name);
80 SAFE_FREE(name);