2 Create and remove pidfile
4 Copyright (C) Amitay Isaacs 2016
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
25 #include "lib/util/blocking.h"
26 #include "lib/util/pidfile.h"
28 #include "common/pidfile.h"
30 struct pidfile_context {
36 static int pidfile_context_destructor(struct pidfile_context *pid_ctx);
38 int pidfile_context_create(TALLOC_CTX *mem_ctx, const char *pidfile,
39 struct pidfile_context **result)
41 struct pidfile_context *pid_ctx;
44 pid_ctx = talloc_zero(mem_ctx, struct pidfile_context);
45 if (pid_ctx == NULL) {
49 pid_ctx->pidfile = talloc_strdup(pid_ctx, pidfile);
50 if (pid_ctx->pidfile == NULL) {
55 pid_ctx->pid = getpid();
57 ret = pidfile_path_create(pid_ctx->pidfile, &fd);
64 talloc_set_destructor(pid_ctx, pidfile_context_destructor);
74 static int pidfile_context_destructor(struct pidfile_context *pid_ctx)
76 if (getpid() != pid_ctx->pid) {
80 (void) unlink(pid_ctx->pidfile);
82 pidfile_fd_close(pid_ctx->fd);