Home | History | Annotate | Line # | Download | only in libutil
pidfile.c revision 1.8.8.2
      1  1.8.8.2  martin /*	$NetBSD: pidfile.c,v 1.8.8.2 2008/04/28 20:23:04 martin Exp $	*/
      2  1.8.8.2  martin 
      3  1.8.8.2  martin /*-
      4  1.8.8.2  martin  * Copyright (c) 1999 The NetBSD Foundation, Inc.
      5  1.8.8.2  martin  * All rights reserved.
      6  1.8.8.2  martin  *
      7  1.8.8.2  martin  * This code is derived from software contributed to The NetBSD Foundation
      8  1.8.8.2  martin  * by Jason R. Thorpe and Matthias Scheler.
      9  1.8.8.2  martin  *
     10  1.8.8.2  martin  * Redistribution and use in source and binary forms, with or without
     11  1.8.8.2  martin  * modification, are permitted provided that the following conditions
     12  1.8.8.2  martin  * are met:
     13  1.8.8.2  martin  * 1. Redistributions of source code must retain the above copyright
     14  1.8.8.2  martin  *    notice, this list of conditions and the following disclaimer.
     15  1.8.8.2  martin  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.8.8.2  martin  *    notice, this list of conditions and the following disclaimer in the
     17  1.8.8.2  martin  *    documentation and/or other materials provided with the distribution.
     18  1.8.8.2  martin  *
     19  1.8.8.2  martin  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.8.8.2  martin  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.8.8.2  martin  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.8.8.2  martin  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.8.8.2  martin  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.8.8.2  martin  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.8.8.2  martin  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.8.8.2  martin  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.8.8.2  martin  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.8.8.2  martin  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.8.8.2  martin  * POSSIBILITY OF SUCH DAMAGE.
     30  1.8.8.2  martin  */
     31  1.8.8.2  martin 
     32  1.8.8.2  martin #include <sys/cdefs.h>
     33  1.8.8.2  martin #if defined(LIBC_SCCS) && !defined(lint)
     34  1.8.8.2  martin __RCSID("$NetBSD: pidfile.c,v 1.8.8.2 2008/04/28 20:23:04 martin Exp $");
     35  1.8.8.2  martin #endif
     36  1.8.8.2  martin 
     37  1.8.8.2  martin #include <sys/param.h>
     38  1.8.8.2  martin #include <paths.h>
     39  1.8.8.2  martin #include <stdlib.h>
     40  1.8.8.2  martin #include <stdio.h>
     41  1.8.8.2  martin #include <string.h>
     42  1.8.8.2  martin #include <unistd.h>
     43  1.8.8.2  martin #include <util.h>
     44  1.8.8.2  martin 
     45  1.8.8.2  martin static int   pidfile_atexit_done;
     46  1.8.8.2  martin static pid_t pidfile_pid;
     47  1.8.8.2  martin static char *pidfile_basename;
     48  1.8.8.2  martin static char *pidfile_path;
     49  1.8.8.2  martin 
     50  1.8.8.2  martin static void pidfile_cleanup(void);
     51  1.8.8.2  martin 
     52  1.8.8.2  martin int
     53  1.8.8.2  martin pidfile(const char *basename)
     54  1.8.8.2  martin {
     55  1.8.8.2  martin 	FILE *f;
     56  1.8.8.2  martin 
     57  1.8.8.2  martin 	/*
     58  1.8.8.2  martin 	 * Register handler which will remove the pidfile later.
     59  1.8.8.2  martin 	 */
     60  1.8.8.2  martin 	if (!pidfile_atexit_done) {
     61  1.8.8.2  martin 		if (atexit(pidfile_cleanup) < 0)
     62  1.8.8.2  martin 			return -1;
     63  1.8.8.2  martin 		pidfile_atexit_done = 1;
     64  1.8.8.2  martin 	}
     65  1.8.8.2  martin 
     66  1.8.8.2  martin 	if (basename == NULL)
     67  1.8.8.2  martin 		basename = getprogname();
     68  1.8.8.2  martin 
     69  1.8.8.2  martin 	/*
     70  1.8.8.2  martin 	 * If pidfile has already been created for the supplied basename
     71  1.8.8.2  martin 	 * we don't need to create a pidfile again.
     72  1.8.8.2  martin 	 */
     73  1.8.8.2  martin 	if (pidfile_path != NULL) {
     74  1.8.8.2  martin 		if (strcmp(pidfile_basename, basename) == 0)
     75  1.8.8.2  martin 			return 0;
     76  1.8.8.2  martin 		/*
     77  1.8.8.2  martin 		 * Remove existing pidfile if it was created by this process.
     78  1.8.8.2  martin 		 */
     79  1.8.8.2  martin 		pidfile_cleanup();
     80  1.8.8.2  martin 
     81  1.8.8.2  martin 		free(pidfile_path);
     82  1.8.8.2  martin 		pidfile_path = NULL;
     83  1.8.8.2  martin 		free(pidfile_basename);
     84  1.8.8.2  martin 		pidfile_basename = NULL;
     85  1.8.8.2  martin 	}
     86  1.8.8.2  martin 
     87  1.8.8.2  martin 	pidfile_pid = getpid();
     88  1.8.8.2  martin 
     89  1.8.8.2  martin 	pidfile_basename = strdup(basename);
     90  1.8.8.2  martin 	if (pidfile_basename == NULL)
     91  1.8.8.2  martin 		return -1;
     92  1.8.8.2  martin 
     93  1.8.8.2  martin 	/* _PATH_VARRUN includes trailing / */
     94  1.8.8.2  martin 	(void) asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename);
     95  1.8.8.2  martin 	if (pidfile_path == NULL) {
     96  1.8.8.2  martin 		free(pidfile_basename);
     97  1.8.8.2  martin 		pidfile_basename = NULL;
     98  1.8.8.2  martin 		return -1;
     99  1.8.8.2  martin 	}
    100  1.8.8.2  martin 
    101  1.8.8.2  martin 	if ((f = fopen(pidfile_path, "w")) == NULL) {
    102  1.8.8.2  martin 		free(pidfile_path);
    103  1.8.8.2  martin 		pidfile_path = NULL;
    104  1.8.8.2  martin 		free(pidfile_basename);
    105  1.8.8.2  martin 		pidfile_basename = NULL;
    106  1.8.8.2  martin 		return -1;
    107  1.8.8.2  martin 	}
    108  1.8.8.2  martin 
    109  1.8.8.2  martin 	(void) fprintf(f, "%d\n", pidfile_pid);
    110  1.8.8.2  martin 	(void) fclose(f);
    111  1.8.8.2  martin 	return 0;
    112  1.8.8.2  martin }
    113  1.8.8.2  martin 
    114  1.8.8.2  martin static void
    115  1.8.8.2  martin pidfile_cleanup(void)
    116  1.8.8.2  martin {
    117  1.8.8.2  martin 	/* Only remove the pidfile if it was created by this process. */
    118  1.8.8.2  martin 	if ((pidfile_path != NULL) && (pidfile_pid == getpid()))
    119  1.8.8.2  martin 		(void) unlink(pidfile_path);
    120  1.8.8.2  martin }
    121