Don't chroot() by default.
[darkstat] / pidfile.c
1 /* darkstat 3
2 * copyright (c) 2007-2011 Emil Mikulic.
3 *
4 * pidfile.h: pidfile manglement
5 *
6 * Permission to use, copy, modify, and distribute this file for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include "err.h"
20 #include "str.h"
21 #include "pidfile.h"
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <pwd.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28
29 static int pidfd = -1;
30 static const char *pidname = NULL;
31
32 void pidfile_create(const char *chroot_dir,
33 const char *filename,
34 const char *privdrop_user) {
35 struct passwd *pw;
36
37 if (pidfd != -1)
38 errx(1, "pidfile already created");
39
40 errno = 0;
41 pw = getpwnam(privdrop_user);
42
43 if (pw == NULL) {
44 if (errno == 0)
45 errx(1, "getpwnam(\"%s\") failed: no such user", privdrop_user);
46 else
47 err(1, "getpwnam(\"%s\") failed", privdrop_user);
48 }
49
50 if (chroot_dir != NULL) {
51 if (chdir(chroot_dir) == -1) {
52 err(1, "chdir(\"%s\") failed", chroot_dir);
53 }
54 }
55 pidname = filename;
56 pidfd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0600);
57 if (pidfd == -1)
58 err(1, "couldn't not create pidfile");
59 if (chown(filename, pw->pw_uid, pw->pw_gid) == -1)
60 err(1, "couldn't chown pidfile");
61 }
62
63 void
64 pidfile_write_close(void)
65 {
66 struct str *s;
67 size_t len;
68 char *buf;
69
70 if (pidfd == -1)
71 errx(1, "cannot write pidfile: not created");
72
73 s = str_make();
74 str_appendf(s, "%u\n", (unsigned int)getpid());
75 str_extract(s, &len, &buf);
76
77 if (write(pidfd, buf, len) != (int)len)
78 err(1, "couldn't write to pidfile");
79 free(buf);
80 if (close(pidfd) == -1)
81 warn("problem closing pidfile");
82 }
83
84 void
85 pidfile_unlink(void)
86 {
87 if (pidname == NULL)
88 return; /* pidfile wasn't created */
89 if (unlink(pidname) == -1)
90 warn("problem unlinking pidfile");
91 }
92
93 /* vim:set ts=3 sw=3 tw=78 et: */