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