blob: 012a1978bd5cb80fc92fab2fd1fdde7557defd40 [file] [log] [blame]
Colin Crossa3d386e2013-02-06 21:03:34 -08001/*
2 * Copyright (c) 2013, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google, Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
25 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
28 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
Elliott Hughes0badbd62014-12-29 12:24:25 -080032#include <errno.h>
Colin Crossa3d386e2013-02-06 21:03:34 -080033#include <fcntl.h>
34#include <string.h>
35#include <unistd.h>
36#include <sys/types.h>
37
38#include "protocol.h"
Szymon Starzyckib6c5f282013-07-24 17:08:04 -070039#include "utils.h"
Colin Crossa3d386e2013-02-06 21:03:34 -080040
41#include "debug.h"
42
43// TODO: change config path
44#define CONFIG_PATH "/data/fastboot.cfg"
45
Colin Crossa3d386e2013-02-06 21:03:34 -080046static int config_parse_line(char *line)
47{
48 char *c;
49 char *key;
50 char *value;
51
52 c = strchr(line, '#');
53 if (c)
54 *c = '\0';
55
56 if (strspn(line, " \t") == strlen(line))
57 return 0;
58
59 c = strchr(line, '=');
60 if (c == NULL)
61 return -1;
62
63 key = line;
64 *c = '\0';
65 value = c + 1;
66
67 key = strip(key);
68 value = strip(value);
69
70 key = strdup(key);
71 value = strdup(value);
72
73 fastboot_publish(key, value);
74
75 return 0;
76}
77
78static void config_parse(char *buffer)
79{
80 char *saveptr;
81 char *str = buffer;
82 char *line = buffer;
83 int c;
84 int ret;
85
86 for (c = 1; line != NULL; c++) {
87 line = strtok_r(str, "\r\n", &saveptr);
88 if (line != NULL) {
89 D(VERBOSE, "'%s'", line);
90 ret = config_parse_line(line);
91 if (ret < 0) {
92 D(WARN, "error parsing " CONFIG_PATH " line %d", c);
93 }
94 }
95 str = NULL;
96 }
97}
98
99void config_init()
100{
101 int fd;
102 off_t len;
103 ssize_t ret;
104 size_t count = 0;
105 char *buffer;
106
107 fd = open(CONFIG_PATH, O_RDONLY);
108 if (fd < 0) {
109 D(ERR, "failed to open " CONFIG_PATH);
110 return;
111 }
112
113 len = lseek(fd, 0, SEEK_END);
114 if (len < 0) {
115 D(ERR, "failed to seek to end of " CONFIG_PATH);
116 return;
117 }
118
119 lseek(fd, 0, SEEK_SET);
120
121 buffer = malloc(len + 1);
122 if (buffer == NULL) {
123 D(ERR, "failed to allocate %ld bytes", len);
124 return;
125 }
126
127 while (count < (size_t)len) {
128 ret = read(fd, buffer + count, len - count);
129 if (ret < 0 && errno != EINTR) {
130 D(ERR, "failed to read " CONFIG_PATH ": %d %s", errno, strerror(errno));
131 return;
132 }
133 if (ret == 0) {
134 D(ERR, "early EOF reading " CONFIG_PATH);
135 return;
136 }
137
138 count += ret;
139 }
140
141 buffer[len] = '\0';
142
143 config_parse(buffer);
144
145 free(buffer);
146}