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