blob: 06ac4da6b8d31b4a19b1148fb3fdc9e105e37ef4 [file] [log] [blame]
David Sodmanbbcb0522014-09-19 10:34:07 -07001/*
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 Sodman8ef20062015-01-06 09:23:40 -08007#include <fcntl.h>
8#include <limits.h>
9#include <pwd.h>
David Sodmanbbcb0522014-09-19 10:34:07 -070010#include <stdio.h>
11#include <stdlib.h>
David Sodmane46ea8a2015-03-13 15:47:12 -070012#include <string.h>
Daniel Nicoara76622c32015-03-18 17:47:54 -040013#include <sys/file.h>
David Sodmanef1ba362015-03-16 10:50:24 -070014#include <sys/stat.h>
David Sodman8ef20062015-01-06 09:23:40 -080015#include <unistd.h>
David Sodmanbbcb0522014-09-19 10:34:07 -070016
David Sodmane46ea8a2015-03-13 15:47:12 -070017#include "util.h"
18
19void sync_lock(bool acquire)
20{
Daniel Nicoara76622c32015-03-18 17:47:54 -040021 static int lock = -1;
David Sodmane46ea8a2015-03-13 15:47:12 -070022 int stat;
David Sodmanef1ba362015-03-16 10:50:24 -070023 struct passwd* pw;
David Sodmane46ea8a2015-03-13 15:47:12 -070024
Daniel Nicoara76622c32015-03-18 17:47:54 -040025 if (lock < 0)
26 lock = open("/run/frecon", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
27
David Sodmane46ea8a2015-03-13 15:47:12 -070028 if (lock >= 0) {
David Sodmanef1ba362015-03-16 10:50:24 -070029 pw = getpwnam("chronos");
30 if (pw) {
31 stat = fchown(lock, pw->pw_uid, pw->pw_gid);
32 if (stat != 0)
33 LOG(ERROR, "fchown returned %d", stat);
34 }
35
Daniel Nicoara76622c32015-03-18 17:47:54 -040036 if (flock(lock, acquire ? LOCK_EX : LOCK_UN) < 0)
David Sodmanef1ba362015-03-16 10:50:24 -070037 LOG(ERROR, "Failed to operate on synch_lock(acquire = %d):%d, lock=%d: %m",
38 acquire, stat, lock);
David Sodmane46ea8a2015-03-13 15:47:12 -070039 }
40}
41
David Sodmanbbcb0522014-09-19 10:34:07 -070042
43void daemonize()
44{
45 pid_t pid;
46 int fd;
47
48 pid = fork();
49 if (pid == -1)
50 return;
51 else if (pid != 0)
52 exit(EXIT_SUCCESS);
53
54 if (setsid() == -1)
55 return;
56
57 // Re-direct stderr/stdout to the system message log
58 close(0);
59 close(1);
60 close(2);
61
62 open("/dev/kmsg", O_RDWR);
63
64 fd = dup(0);
65 if (fd != STDOUT_FILENO) {
66 close(fd);
67 return;
68 }
69 fd = dup(0);
70 if (fd != STDERR_FILENO) {
71 close(fd);
72 return;
73 }
74}
75
76#ifdef __clang__
77__attribute__((format (__printf__, 2, 0)))
78#endif
79void LOG(int severity, const char* fmt, ...)
80{
81 va_list arg_list;
82 fprintf(stderr, "frecon: ");
83 va_start( arg_list, fmt);
84 vfprintf(stderr, fmt, arg_list);
85 va_end(arg_list);
86 fprintf(stderr, "\n");
87}
David Sodman8ef20062015-01-06 09:23:40 -080088
89void parse_location(char* loc_str, int *x, int *y)
90{
91 int count = 0;
92 char* savedptr;
93 char* token;
94 char* str;
95 int *results[] = {x, y};
96 long tmp;
97
98 for (token = str = loc_str; token != NULL; str = NULL) {
99 if (count > 1)
100 break;
101
102 token = strtok_r(str, ",", &savedptr);
103 if (token) {
104 tmp = MIN(INT_MAX, strtol(token, NULL, 0));
105 *(results[count++]) = (int)tmp;
106 }
107 }
108}
109
110void parse_filespec(char* filespec, char *filename,
111 int32_t *offset_x, int32_t *offset_y, uint32_t *duration,
112 uint32_t default_duration, int32_t default_x, int32_t default_y)
113{
114 char* saved_ptr;
115 char* token;
116
117 // defaults
118 *offset_x = default_x;
119 *offset_y = default_y;
120 *duration = default_duration;
121
122 token = filespec;
123 token = strtok_r(token, ":", &saved_ptr);
124 if (token)
125 strcpy(filename, token);
126
127 LOG(ERROR, "image file: %s", filename);
128
129 token = strtok_r(NULL, ":", &saved_ptr);
130 if (token) {
131 *duration = strtoul(token, NULL, 0);
132 token = strtok_r(NULL, ",", &saved_ptr);
133 if (token) {
134 token = strtok_r(token, ",", &saved_ptr);
135 if (token) {
136 *offset_x = strtol(token, NULL, 0);
137 token = strtok_r(token, ",", &saved_ptr);
138 if (token)
139 *offset_y = strtol(token, NULL, 0);
140 }
141 }
142 }
143}
144
145void parse_image_option(char* optionstr, char** name, char** val)
146{
147 char** result[2] = { name, val };
148 int count = 0;
149 char* token;
150 char* str;
151 char* savedptr;
152
153 for (token = str = optionstr; token != NULL; str = NULL) {
154 if (count > 1)
155 break;
156
157 token = strtok_r(str, ":", &savedptr);
158 if (token) {
159 *(result[count]) = malloc(strlen(token) + 1);
160 strcpy(*(result[count]), token);
161 count++;
162 }
163 }
164}
165