HEIMDAL: move code from source4/heimdal* to third_party/heimdal*
[Samba.git] / third_party / heimdal / lib / roken / write_pid.c
blobcf5299b64bfaf08bb733b33c01f6c55680ad9a20
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 #ifdef HAVE_UTIL_H
39 #include <util.h>
40 #endif
42 ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL
43 pid_file_write(const char *progname)
45 const char *pidfile_dir = NULL;
46 char *ret = NULL;
47 FILE *fp;
50 * Maybe we could have a version of this function (and pidfile())
51 * where we get a directory from the caller. That would allow us to
52 * have command-line options for the daemons for this.
54 * For now we use an environment variable.
56 pidfile_dir = secure_getenv("HEIM_PIDFILE_DIR");
57 if (pidfile_dir == NULL)
58 pidfile_dir = _PATH_VARRUN;
60 if (asprintf(&ret, "%s%s.pid", pidfile_dir, progname) < 0 || ret == NULL)
61 return NULL;
62 fp = fopen(ret, "w");
63 if (fp == NULL) {
64 free(ret);
65 return NULL;
67 fprintf(fp, "%lu\n", (unsigned long)getpid());
68 fclose(fp);
69 return ret;
72 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
73 pid_file_delete(char **filename)
75 if (*filename != NULL) {
76 unlink(*filename);
77 free(*filename);
78 *filename = NULL;
82 static char *pidfile_path;
83 static pid_t pidfile_pid;
85 static void
86 pidfile_cleanup(void)
88 if (pidfile_path != NULL && pidfile_pid == getpid())
89 pid_file_delete(&pidfile_path);
92 ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
93 rk_pidfile(const char *bname)
96 * If the OS has a pidfile(), call that, but still call
97 * pid_file_write(). Even if both want to write the same file,
98 * writing it twice will still work.
100 #ifdef HAVE_PIDFILE
101 pidfile(bname);
102 #endif
104 if (pidfile_path != NULL)
105 return;
106 if (bname == NULL)
107 bname = getprogname();
108 pidfile_path = pid_file_write(bname);
109 pidfile_pid = getpid();
110 #if defined(HAVE_ATEXIT)
111 if (pidfile_path != NULL)
112 atexit(pidfile_cleanup);
113 #elif defined(HAVE_ON_EXIT)
114 if (pidfile_path != NULL)
115 on_exit(pidfile_cleanup);
116 #endif