xref: /freebsd/crypto/heimdal/lib/roken/write_pid.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
15e9cd1aeSAssar Westerlund /*
2*ae771770SStanislav Sedov  * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
35e9cd1aeSAssar Westerlund  * (Royal Institute of Technology, Stockholm, Sweden).
45e9cd1aeSAssar Westerlund  * All rights reserved.
55e9cd1aeSAssar Westerlund  *
65e9cd1aeSAssar Westerlund  * Redistribution and use in source and binary forms, with or without
75e9cd1aeSAssar Westerlund  * modification, are permitted provided that the following conditions
85e9cd1aeSAssar Westerlund  * are met:
95e9cd1aeSAssar Westerlund  *
105e9cd1aeSAssar Westerlund  * 1. Redistributions of source code must retain the above copyright
115e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer.
125e9cd1aeSAssar Westerlund  *
135e9cd1aeSAssar Westerlund  * 2. Redistributions in binary form must reproduce the above copyright
145e9cd1aeSAssar Westerlund  *    notice, this list of conditions and the following disclaimer in the
155e9cd1aeSAssar Westerlund  *    documentation and/or other materials provided with the distribution.
165e9cd1aeSAssar Westerlund  *
175e9cd1aeSAssar Westerlund  * 3. Neither the name of the Institute nor the names of its contributors
185e9cd1aeSAssar Westerlund  *    may be used to endorse or promote products derived from this software
195e9cd1aeSAssar Westerlund  *    without specific prior written permission.
205e9cd1aeSAssar Westerlund  *
215e9cd1aeSAssar Westerlund  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
225e9cd1aeSAssar Westerlund  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235e9cd1aeSAssar Westerlund  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245e9cd1aeSAssar Westerlund  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
255e9cd1aeSAssar Westerlund  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265e9cd1aeSAssar Westerlund  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275e9cd1aeSAssar Westerlund  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285e9cd1aeSAssar Westerlund  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295e9cd1aeSAssar Westerlund  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305e9cd1aeSAssar Westerlund  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315e9cd1aeSAssar Westerlund  * SUCH DAMAGE.
325e9cd1aeSAssar Westerlund  */
335e9cd1aeSAssar Westerlund 
345e9cd1aeSAssar Westerlund #include <config.h>
355e9cd1aeSAssar Westerlund 
365e9cd1aeSAssar Westerlund #include "roken.h"
375e9cd1aeSAssar Westerlund 
38*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION char * ROKEN_LIB_CALL
pid_file_write(const char * progname)395e9cd1aeSAssar Westerlund pid_file_write (const char *progname)
405e9cd1aeSAssar Westerlund {
41*ae771770SStanislav Sedov     char *ret = NULL;
425e9cd1aeSAssar Westerlund     FILE *fp;
435e9cd1aeSAssar Westerlund 
44*ae771770SStanislav Sedov     if (asprintf (&ret, "%s%s.pid", _PATH_VARRUN, progname) < 0 || ret == NULL)
455e9cd1aeSAssar Westerlund 	return NULL;
465e9cd1aeSAssar Westerlund     fp = fopen (ret, "w");
475e9cd1aeSAssar Westerlund     if (fp == NULL) {
485e9cd1aeSAssar Westerlund 	free (ret);
495e9cd1aeSAssar Westerlund 	return NULL;
505e9cd1aeSAssar Westerlund     }
515e9cd1aeSAssar Westerlund     fprintf (fp, "%u", (unsigned)getpid());
525e9cd1aeSAssar Westerlund     fclose (fp);
535e9cd1aeSAssar Westerlund     return ret;
545e9cd1aeSAssar Westerlund }
555e9cd1aeSAssar Westerlund 
56*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
pid_file_delete(char ** filename)575e9cd1aeSAssar Westerlund pid_file_delete (char **filename)
585e9cd1aeSAssar Westerlund {
595e9cd1aeSAssar Westerlund     if (*filename != NULL) {
605e9cd1aeSAssar Westerlund 	unlink (*filename);
615e9cd1aeSAssar Westerlund 	free (*filename);
625e9cd1aeSAssar Westerlund 	*filename = NULL;
635e9cd1aeSAssar Westerlund     }
645e9cd1aeSAssar Westerlund }
655e9cd1aeSAssar Westerlund 
665e9cd1aeSAssar Westerlund #ifndef HAVE_PIDFILE
675e9cd1aeSAssar Westerlund static char *pidfile_path;
685e9cd1aeSAssar Westerlund 
695e9cd1aeSAssar Westerlund static void
pidfile_cleanup(void)705e9cd1aeSAssar Westerlund pidfile_cleanup(void)
715e9cd1aeSAssar Westerlund {
725e9cd1aeSAssar Westerlund     if(pidfile_path != NULL)
735e9cd1aeSAssar Westerlund 	pid_file_delete(&pidfile_path);
745e9cd1aeSAssar Westerlund }
755e9cd1aeSAssar Westerlund 
76*ae771770SStanislav Sedov ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
pidfile(const char * basename)775e9cd1aeSAssar Westerlund pidfile(const char *basename)
785e9cd1aeSAssar Westerlund {
795e9cd1aeSAssar Westerlund     if(pidfile_path != NULL)
805e9cd1aeSAssar Westerlund 	return;
815e9cd1aeSAssar Westerlund     if(basename == NULL)
82adb0ddaeSAssar Westerlund 	basename = getprogname();
835e9cd1aeSAssar Westerlund     pidfile_path = pid_file_write(basename);
844137ff4cSJacques Vidrine #if defined(HAVE_ATEXIT)
855e9cd1aeSAssar Westerlund     atexit(pidfile_cleanup);
864137ff4cSJacques Vidrine #elif defined(HAVE_ON_EXIT)
874137ff4cSJacques Vidrine     on_exit(pidfile_cleanup);
884137ff4cSJacques Vidrine #endif
895e9cd1aeSAssar Westerlund }
905e9cd1aeSAssar Westerlund #endif
91