blob: 8a4b39606039bd6e59c70511f622978daccc78d2 [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
65void parse_location(char* loc_str, int *x, int *y)
66{
67 int count = 0;
68 char* savedptr;
69 char* token;
70 char* str;
71 int *results[] = {x, y};
72 long tmp;
73
74 for (token = str = loc_str; token != NULL; str = NULL) {
75 if (count > 1)
76 break;
77
78 token = strtok_r(str, ",", &savedptr);
79 if (token) {
80 tmp = MIN(INT_MAX, strtol(token, NULL, 0));
81 *(results[count++]) = (int)tmp;
82 }
83 }
84}
85
86void parse_filespec(char* filespec, char *filename,
87 int32_t *offset_x, int32_t *offset_y, uint32_t *duration,
88 uint32_t default_duration, int32_t default_x, int32_t default_y)
89{
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;
123 char* token;
124 char* str;
125 char* savedptr;
126
127 for (token = str = optionstr; token != NULL; str = NULL) {
128 if (count > 1)
129 break;
130
131 token = strtok_r(str, ":", &savedptr);
132 if (token) {
133 *(result[count]) = malloc(strlen(token) + 1);
134 strcpy(*(result[count]), token);
135 count++;
136 }
137 }
138}
139