| 1 |
/* |
| 2 |
* Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan |
| 3 |
* (Royal Institute of Technology, Stockholm, Sweden). |
| 4 |
* All rights reserved. |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 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. |
| 16 |
* |
| 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. |
| 20 |
* |
| 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. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include <config.h> |
| 35 |
|
| 36 |
#include "roken.h" |
| 37 |
|
| 38 |
#ifdef HAVE_UTIL_H |
| 39 |
#include <util.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL |
| 43 |
pid_file_write(const char *progname) |
| 44 |
{ |
| 45 |
const char *pidfile_dir = NULL; |
| 46 |
char *ret = NULL; |
| 47 |
FILE *fp; |
| 48 |
|
| 49 |
/* |
| 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. |
| 53 |
* |
| 54 |
* For now we use an environment variable. |
| 55 |
*/ |
| 56 |
if (!issuid()) |
| 57 |
pidfile_dir = getenv("HEIM_PIDFILE_DIR"); |
| 58 |
if (pidfile_dir == NULL) |
| 59 |
pidfile_dir = _PATH_VARRUN; |
| 60 |
|
| 61 |
if (asprintf(&ret, "%s%s.pid", pidfile_dir, progname) < 0 || ret == NULL) |
| 62 |
return NULL; |
| 63 |
fp = fopen(ret, "w"); |
| 64 |
if (fp == NULL) { |
| 65 |
free(ret); |
| 66 |
return NULL; |
| 67 |
} |
| 68 |
fprintf(fp, "%lu\n", (unsigned long)getpid()); |
| 69 |
fclose(fp); |
| 70 |
return ret; |
| 71 |
} |
| 72 |
|
| 73 |
ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL |
| 74 |
pid_file_delete(char **filename) |
| 75 |
{ |
| 76 |
if (*filename != NULL) { |
| 77 |
unlink(*filename); |
| 78 |
free(*filename); |
| 79 |
*filename = NULL; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
static char *pidfile_path; |
| 84 |
static pid_t pidfile_pid; |
| 85 |
|
| 86 |
static void |
| 87 |
pidfile_cleanup(void) |
| 88 |
{ |
| 89 |
if (pidfile_path != NULL && pidfile_pid == getpid()) |
| 90 |
pid_file_delete(&pidfile_path); |
| 91 |
} |
| 92 |
|
| 93 |
ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL |
| 94 |
rk_pidfile(const char *bname) |
| 95 |
{ |
| 96 |
/* |
| 97 |
* If the OS has a pidfile(), call that, but still call |
| 98 |
* pid_file_write(). Even if both want to write the same file, |
| 99 |
* writing it twice will still work. |
| 100 |
*/ |
| 101 |
#ifdef HAVE_PIDFILE |
| 102 |
pidfile(bname); |
| 103 |
#endif |
| 104 |
|
| 105 |
if (pidfile_path != NULL) |
| 106 |
return; |
| 107 |
if (bname == NULL) |
| 108 |
bname = getprogname(); |
| 109 |
pidfile_path = pid_file_write(bname); |
| 110 |
pidfile_pid = getpid(); |
| 111 |
#if defined(HAVE_ATEXIT) |
| 112 |
if (pidfile_path != NULL) |
| 113 |
atexit(pidfile_cleanup); |
| 114 |
#elif defined(HAVE_ON_EXIT) |
| 115 |
if (pidfile_path != NULL) |
| 116 |
on_exit(pidfile_cleanup); |
| 117 |
#endif |
| 118 |
} |