David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 3 | * Use of this source code is governed by a BSD-style license that can be |
| 4 | * found in the LICENSE file. |
| 5 | */ |
| 6 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <unistd.h> |
David Sodman | e46ea8a | 2015-03-13 15:47:12 -0700 | [diff] [blame] | 10 | #include <string.h> |
Daniel Nicoara | 76622c3 | 2015-03-18 17:47:54 -0400 | [diff] [blame] | 11 | #include <sys/file.h> |
David Sodman | ef1ba36 | 2015-03-16 10:50:24 -0700 | [diff] [blame] | 12 | #include <sys/stat.h> |
| 13 | #include <pwd.h> |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 14 | |
David Sodman | e46ea8a | 2015-03-13 15:47:12 -0700 | [diff] [blame] | 15 | #include "util.h" |
| 16 | |
| 17 | void sync_lock(bool acquire) |
| 18 | { |
Daniel Nicoara | 76622c3 | 2015-03-18 17:47:54 -0400 | [diff] [blame] | 19 | static int lock = -1; |
David Sodman | e46ea8a | 2015-03-13 15:47:12 -0700 | [diff] [blame] | 20 | int stat; |
David Sodman | ef1ba36 | 2015-03-16 10:50:24 -0700 | [diff] [blame] | 21 | struct passwd* pw; |
David Sodman | e46ea8a | 2015-03-13 15:47:12 -0700 | [diff] [blame] | 22 | |
Daniel Nicoara | 76622c3 | 2015-03-18 17:47:54 -0400 | [diff] [blame] | 23 | if (lock < 0) |
| 24 | lock = open("/run/frecon", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR); |
| 25 | |
David Sodman | e46ea8a | 2015-03-13 15:47:12 -0700 | [diff] [blame] | 26 | if (lock >= 0) { |
David Sodman | ef1ba36 | 2015-03-16 10:50:24 -0700 | [diff] [blame] | 27 | pw = getpwnam("chronos"); |
| 28 | if (pw) { |
| 29 | stat = fchown(lock, pw->pw_uid, pw->pw_gid); |
| 30 | if (stat != 0) |
| 31 | LOG(ERROR, "fchown returned %d", stat); |
| 32 | } |
| 33 | |
Daniel Nicoara | 76622c3 | 2015-03-18 17:47:54 -0400 | [diff] [blame] | 34 | if (flock(lock, acquire ? LOCK_EX : LOCK_UN) < 0) |
David Sodman | ef1ba36 | 2015-03-16 10:50:24 -0700 | [diff] [blame] | 35 | LOG(ERROR, "Failed to operate on synch_lock(acquire = %d):%d, lock=%d: %m", |
| 36 | acquire, stat, lock); |
David Sodman | e46ea8a | 2015-03-13 15:47:12 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
David Sodman | bbcb052 | 2014-09-19 10:34:07 -0700 | [diff] [blame] | 40 | |
| 41 | void daemonize() |
| 42 | { |
| 43 | pid_t pid; |
| 44 | int fd; |
| 45 | |
| 46 | pid = fork(); |
| 47 | if (pid == -1) |
| 48 | return; |
| 49 | else if (pid != 0) |
| 50 | exit(EXIT_SUCCESS); |
| 51 | |
| 52 | if (setsid() == -1) |
| 53 | return; |
| 54 | |
| 55 | // Re-direct stderr/stdout to the system message log |
| 56 | close(0); |
| 57 | close(1); |
| 58 | close(2); |
| 59 | |
| 60 | open("/dev/kmsg", O_RDWR); |
| 61 | |
| 62 | fd = dup(0); |
| 63 | if (fd != STDOUT_FILENO) { |
| 64 | close(fd); |
| 65 | return; |
| 66 | } |
| 67 | fd = dup(0); |
| 68 | if (fd != STDERR_FILENO) { |
| 69 | close(fd); |
| 70 | return; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | #ifdef __clang__ |
| 75 | __attribute__((format (__printf__, 2, 0))) |
| 76 | #endif |
| 77 | void LOG(int severity, const char* fmt, ...) |
| 78 | { |
| 79 | va_list arg_list; |
| 80 | fprintf(stderr, "frecon: "); |
| 81 | va_start( arg_list, fmt); |
| 82 | vfprintf(stderr, fmt, arg_list); |
| 83 | va_end(arg_list); |
| 84 | fprintf(stderr, "\n"); |
| 85 | } |