2 * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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
42 ROKEN_LIB_FUNCTION
char * ROKEN_LIB_CALL
43 pid_file_write(const char *progname
)
45 const char *pidfile_dir
= NULL
;
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
)
67 fprintf(fp
, "%lu\n", (unsigned long)getpid());
72 ROKEN_LIB_FUNCTION
void ROKEN_LIB_CALL
73 pid_file_delete(char **filename
)
75 if (*filename
!= NULL
) {
82 static char *pidfile_path
;
83 static pid_t pidfile_pid
;
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.
104 if (pidfile_path
!= 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
);