libite 2.6.1
pidfile.c
Go to the documentation of this file.
1/* Updated by troglobit for libite/finit/uftpd projects 2016/07/04 */
2/* $OpenBSD: pidfile.c,v 1.11 2015/06/03 02:24:36 millert Exp $ */
3/* $NetBSD: pidfile.c,v 1.4 2001/02/19 22:43:42 cgd Exp $ */
4
5/*-
6 * Copyright (c) 1999 The NetBSD Foundation, Inc.
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Jason R. Thorpe.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
40
41#include <sys/stat.h> /* utimensat() */
42#include <sys/time.h> /* utimensat() on *BSD */
43#include <sys/types.h>
44#include <errno.h>
45#include <paths.h>
46#include <stdio.h>
47#include <stdlib.h>
48#include <unistd.h>
49
50#ifndef pidfile
51static char *pidfile_path = NULL;
52static pid_t pidfile_pid = 0;
53
54static void pidfile_cleanup(void);
55
56const char *__pidfile_path = _PATH_VARRUN; /* Note: includes trailing slash '/' */
57const char *__pidfile_name = NULL;
58extern char *__progname;
59
71int pidfile(const char *basename)
72{
73 int save_errno;
74 int atexit_already;
75 pid_t pid;
76 FILE *f;
77
78 if (basename == NULL)
79 basename = __progname;
80
81 pid = getpid();
82 atexit_already = 0;
83
84 if (pidfile_path != NULL) {
85 if (!access(pidfile_path, R_OK) && pid == pidfile_pid) {
86 utimensat(0, pidfile_path, NULL, 0);
87 return (0);
88 }
89 free(pidfile_path);
90 pidfile_path = NULL;
91 __pidfile_name = NULL;
92 atexit_already = 1;
93 }
94
95 if (basename[0] != '/') {
96 if (asprintf(&pidfile_path, "%s%s.pid", __pidfile_path, basename) == -1)
97 return (-1);
98 } else {
99 if (asprintf(&pidfile_path, "%s", basename) == -1)
100 return (-1);
101 }
102
103 if ((f = fopen(pidfile_path, "w")) == NULL) {
104 save_errno = errno;
105 free(pidfile_path);
106 pidfile_path = NULL;
107 errno = save_errno;
108 return (-1);
109 }
110
111 if (fprintf(f, "%ld\n", (long)pid) <= 0 || fflush(f) != 0) {
112 save_errno = errno;
113 (void) fclose(f);
114 (void) unlink(pidfile_path);
115 free(pidfile_path);
116 pidfile_path = NULL;
117 errno = save_errno;
118 return (-1);
119 }
120 (void) fclose(f);
121 __pidfile_name = pidfile_path;
122
123 /*
124 * LITE extension, no need to set up another atexit() handler
125 * if user only called us to update the mtime of the PID file
126 */
127 if (atexit_already)
128 return (0);
129
130 pidfile_pid = pid;
131 if (atexit(pidfile_cleanup) < 0) {
132 save_errno = errno;
133 (void) unlink(pidfile_path);
134 free(pidfile_path);
135 pidfile_path = NULL;
136 pidfile_pid = 0;
137 errno = save_errno;
138 return (-1);
139 }
140
141 return (0);
142}
143
144static void
145pidfile_cleanup(void)
146{
147 if (pidfile_path != NULL && pidfile_pid == getpid()) {
148 (void) unlink(pidfile_path);
149 free(pidfile_path);
150 pidfile_path = NULL;
151 }
152}
153#endif
int pidfile(const char *basename)
Definition pidfile.c:71