summaryrefslogtreecommitdiff
path: root/source3/lib/pidfile.c
blob: 79ea3a5330066b23ca94273fe0b7896b0b10a4de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* this code is broken - there is a race condition with the unlink (tridge) */

/*
   Unix SMB/CIFS implementation.
   pidfile handling
   Copyright (C) Andrew Tridgell 1998
   Copyright (C) Jeremy Allison 2012

   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
   the Free Software Foundation; either version 3 of the License, or
   (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
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "system/filesys.h"
#include "../lib/util/pidfile.h"

/* Malloc a pidfile name. */
static char *get_pidfile_name(const char *program_name)
{
	char *name = NULL;

	/* 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 {
		const char *short_configfile;
		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");
		}
	}
	return name;
}

/* return the pid in a pidfile. return 0 if the process (or pidfile)
   does not exist */
pid_t pidfile_pid_s3(const char *program_name)
{
	pid_t pid = 0;
	char *name = get_pidfile_name(program_name);

	pid = pidfile_pid(lp_piddir(), name);
	SAFE_FREE(name);
	return pid;
}

/* create a pid file in the pid directory. open it and leave it locked */
void pidfile_create_s3(const char *program_name)
{
	char *name = get_pidfile_name(program_name);

	pidfile_create(lp_piddir(), name);
	SAFE_FREE(name);
}

/* Remove a pidfile. */
void pidfile_unlink_s3(const char *program_name)
{
	char *name = get_pidfile_name(program_name);
	pidfile_unlink(lp_piddir(), name);
	SAFE_FREE(name);
}