blob: 1a492929cd55baf7bc5504fbc0cf71402e38bb4a [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
David Sodmanbbcb0522014-09-19 10:34:07 -070019void daemonize()
20{
21 pid_t pid;
22 int fd;
23
24 pid = fork();
25 if (pid == -1)
26 return;
27 else if (pid != 0)
28 exit(EXIT_SUCCESS);
29
30 if (setsid() == -1)
31 return;
32
33 // Re-direct stderr/stdout to the system message log
34 close(0);
35 close(1);
36 close(2);
37
38 open("/dev/kmsg", O_RDWR);
39
40 fd = dup(0);
41 if (fd != STDOUT_FILENO) {
42 close(fd);
43 return;
44 }
45 fd = dup(0);
46 if (fd != STDERR_FILENO) {
47 close(fd);
48 return;
49 }
50}
51
52#ifdef __clang__
53__attribute__((format (__printf__, 2, 0)))
54#endif
55void LOG(int severity, const char* fmt, ...)
56{
57 va_list arg_list;
Dominik Behrb1abcba2016-04-14 14:57:21 -070058 fprintf(stderr, "frecon(%d): ", getpid());
David Sodmanbbcb0522014-09-19 10:34:07 -070059 va_start( arg_list, fmt);
60 vfprintf(stderr, fmt, arg_list);
61 va_end(arg_list);
62 fprintf(stderr, "\n");
63}
David Sodman8ef20062015-01-06 09:23:40 -080064
Stéphane Marchesin00ff1872015-12-14 13:40:09 -080065void parse_location(char* loc_str, int* x, int* y)
David Sodman8ef20062015-01-06 09:23:40 -080066{
67 int count = 0;
68 char* savedptr;
David Sodman8ef20062015-01-06 09:23:40 -080069 char* str;
Stéphane Marchesin00ff1872015-12-14 13:40:09 -080070 int* results[] = {x, y};
David Sodman8ef20062015-01-06 09:23:40 -080071 long tmp;
72
Stéphane Marchesinac14d292015-12-14 15:27:18 -080073 for (char* token = str = loc_str; token != NULL; str = NULL) {
David Sodman8ef20062015-01-06 09:23:40 -080074 if (count > 1)
75 break;
76
77 token = strtok_r(str, ",", &savedptr);
78 if (token) {
79 tmp = MIN(INT_MAX, strtol(token, NULL, 0));
80 *(results[count++]) = (int)tmp;
81 }
82 }
83}
84
Stéphane Marchesin00ff1872015-12-14 13:40:09 -080085void parse_filespec(char* filespec, char* filename,
Stéphane Marchesin8fc13522015-12-14 17:02:28 -080086 int32_t* offset_x, int32_t* offset_y, uint32_t* duration,
87 uint32_t default_duration,
88 int32_t default_x, int32_t default_y)
David Sodman8ef20062015-01-06 09:23:40 -080089{
90 char* saved_ptr;
91 char* token;
92
93 // defaults
94 *offset_x = default_x;
95 *offset_y = default_y;
96 *duration = default_duration;
97
98 token = filespec;
99 token = strtok_r(token, ":", &saved_ptr);
100 if (token)
101 strcpy(filename, token);
102
David Sodman8ef20062015-01-06 09:23:40 -0800103 token = strtok_r(NULL, ":", &saved_ptr);
104 if (token) {
105 *duration = strtoul(token, NULL, 0);
106 token = strtok_r(NULL, ",", &saved_ptr);
107 if (token) {
108 token = strtok_r(token, ",", &saved_ptr);
109 if (token) {
110 *offset_x = strtol(token, NULL, 0);
111 token = strtok_r(token, ",", &saved_ptr);
112 if (token)
113 *offset_y = strtol(token, NULL, 0);
114 }
115 }
116 }
117}
118
119void parse_image_option(char* optionstr, char** name, char** val)
120{
121 char** result[2] = { name, val };
122 int count = 0;
David Sodman8ef20062015-01-06 09:23:40 -0800123 char* str;
124 char* savedptr;
125
Stéphane Marchesinac14d292015-12-14 15:27:18 -0800126 for (char* token = str = optionstr; token != NULL; str = NULL) {
David Sodman8ef20062015-01-06 09:23:40 -0800127 if (count > 1)
128 break;
129
130 token = strtok_r(str, ":", &savedptr);
131 if (token) {
132 *(result[count]) = malloc(strlen(token) + 1);
133 strcpy(*(result[count]), token);
134 count++;
135 }
136 }
137}
138