1*d3273b5bSchristos /* $NetBSD: write_pid.c,v 1.2 2017/01/28 21:31:50 christos Exp $ */
2ca1c9b0cSelric
3ca1c9b0cSelric /*
4ca1c9b0cSelric * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
5ca1c9b0cSelric * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric * All rights reserved.
7ca1c9b0cSelric *
8ca1c9b0cSelric * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric * modification, are permitted provided that the following conditions
10ca1c9b0cSelric * are met:
11ca1c9b0cSelric *
12ca1c9b0cSelric * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric * notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric *
15ca1c9b0cSelric * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric * notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric * documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric *
19ca1c9b0cSelric * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric * may be used to endorse or promote products derived from this software
21ca1c9b0cSelric * without specific prior written permission.
22ca1c9b0cSelric *
23ca1c9b0cSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric * SUCH DAMAGE.
34ca1c9b0cSelric */
35ca1c9b0cSelric
36ca1c9b0cSelric #include <config.h>
37ca1c9b0cSelric
38ca1c9b0cSelric #include <krb5/roken.h>
39ca1c9b0cSelric
40b9d004c6Schristos #ifdef HAVE_UTIL_H
41b9d004c6Schristos #include <util.h>
42b9d004c6Schristos #endif
43b9d004c6Schristos
44ca1c9b0cSelric ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL
pid_file_write(const char * progname)45ca1c9b0cSelric pid_file_write(const char *progname)
46ca1c9b0cSelric {
47b9d004c6Schristos const char *pidfile_dir = NULL;
48ca1c9b0cSelric char *ret = NULL;
49ca1c9b0cSelric FILE *fp;
50ca1c9b0cSelric
51b9d004c6Schristos /*
52b9d004c6Schristos * Maybe we could have a version of this function (and pidfile())
53b9d004c6Schristos * where we get a directory from the caller. That would allow us to
54b9d004c6Schristos * have command-line options for the daemons for this.
55b9d004c6Schristos *
56b9d004c6Schristos * For now we use an environment variable.
57b9d004c6Schristos */
58b9d004c6Schristos if (!issuid())
59b9d004c6Schristos pidfile_dir = getenv("HEIM_PIDFILE_DIR");
60b9d004c6Schristos if (pidfile_dir == NULL)
61b9d004c6Schristos pidfile_dir = _PATH_VARRUN;
62b9d004c6Schristos
63b9d004c6Schristos if (asprintf(&ret, "%s%s.pid", pidfile_dir, progname) < 0 || ret == NULL)
64ca1c9b0cSelric return NULL;
65ca1c9b0cSelric fp = fopen(ret, "w");
66ca1c9b0cSelric if (fp == NULL) {
67ca1c9b0cSelric free(ret);
68ca1c9b0cSelric return NULL;
69ca1c9b0cSelric }
70b9d004c6Schristos fprintf(fp, "%lu\n", (unsigned long)getpid());
71ca1c9b0cSelric fclose(fp);
72ca1c9b0cSelric return ret;
73ca1c9b0cSelric }
74ca1c9b0cSelric
75ca1c9b0cSelric ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
pid_file_delete(char ** filename)76ca1c9b0cSelric pid_file_delete(char **filename)
77ca1c9b0cSelric {
78ca1c9b0cSelric if (*filename != NULL) {
79ca1c9b0cSelric unlink(*filename);
80ca1c9b0cSelric free(*filename);
81ca1c9b0cSelric *filename = NULL;
82ca1c9b0cSelric }
83ca1c9b0cSelric }
84ca1c9b0cSelric
85ca1c9b0cSelric static char *pidfile_path;
86b9d004c6Schristos static pid_t pidfile_pid;
87ca1c9b0cSelric
88ca1c9b0cSelric static void
pidfile_cleanup(void)89ca1c9b0cSelric pidfile_cleanup(void)
90ca1c9b0cSelric {
91b9d004c6Schristos if (pidfile_path != NULL && pidfile_pid == getpid())
92ca1c9b0cSelric pid_file_delete(&pidfile_path);
93ca1c9b0cSelric }
94ca1c9b0cSelric
95ca1c9b0cSelric ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
rk_pidfile(const char * bname)96b9d004c6Schristos rk_pidfile(const char *bname)
97ca1c9b0cSelric {
98b9d004c6Schristos /*
99b9d004c6Schristos * If the OS has a pidfile(), call that, but still call
100b9d004c6Schristos * pid_file_write(). Even if both want to write the same file,
101b9d004c6Schristos * writing it twice will still work.
102b9d004c6Schristos */
103b9d004c6Schristos #ifdef HAVE_PIDFILE
104b9d004c6Schristos pidfile(bname);
105b9d004c6Schristos #endif
106b9d004c6Schristos
107ca1c9b0cSelric if (pidfile_path != NULL)
108ca1c9b0cSelric return;
109b9d004c6Schristos if (bname == NULL)
110b9d004c6Schristos bname = getprogname();
111b9d004c6Schristos pidfile_path = pid_file_write(bname);
112b9d004c6Schristos pidfile_pid = getpid();
113ca1c9b0cSelric #if defined(HAVE_ATEXIT)
114b9d004c6Schristos if (pidfile_path != NULL)
115ca1c9b0cSelric atexit(pidfile_cleanup);
116ca1c9b0cSelric #elif defined(HAVE_ON_EXIT)
117b9d004c6Schristos if (pidfile_path != NULL)
118ca1c9b0cSelric on_exit(pidfile_cleanup);
119ca1c9b0cSelric #endif
120ca1c9b0cSelric }
121