blob: 029bcc5d38c0ab4715209ebf667fd6b0de7b5fc1 [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;
58 fprintf(stderr, "frecon: ");
59 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,
86 int32_t* offset_x, int32_t* offset_y, uint32_t* duration,
David Sodman8ef20062015-01-06 09:23:40 -080087 uint32_t default_duration, int32_t default_x, int32_t default_y)
88{
89 char* saved_ptr;
90 char* token;
91
92 // defaults
93 *offset_x = default_x;
94 *offset_y = default_y;
95 *duration = default_duration;
96
97 token = filespec;
98 token = strtok_r(token, ":", &saved_ptr);
99 if (token)
100 strcpy(filename, token);
101
David Sodman8ef20062015-01-06 09:23:40 -0800102 token = strtok_r(NULL, ":", &saved_ptr);
103 if (token) {
104 *duration = strtoul(token, NULL, 0);
105 token = strtok_r(NULL, ",", &saved_ptr);
106 if (token) {
107 token = strtok_r(token, ",", &saved_ptr);
108 if (token) {
109 *offset_x = strtol(token, NULL, 0);
110 token = strtok_r(token, ",", &saved_ptr);
111 if (token)
112 *offset_y = strtol(token, NULL, 0);
113 }
114 }
115 }
116}
117
118void parse_image_option(char* optionstr, char** name, char** val)
119{
120 char** result[2] = { name, val };
121 int count = 0;
David Sodman8ef20062015-01-06 09:23:40 -0800122 char* str;
123 char* savedptr;
124
Stéphane Marchesinac14d292015-12-14 15:27:18 -0800125 for (char* token = str = optionstr; token != NULL; str = NULL) {
David Sodman8ef20062015-01-06 09:23:40 -0800126 if (count > 1)
127 break;
128
129 token = strtok_r(str, ":", &savedptr);
130 if (token) {
131 *(result[count]) = malloc(strlen(token) + 1);
132 strcpy(*(result[count]), token);
133 count++;
134 }
135 }
136}
137