blob: f0dc23d9df2142784967f9360739bf963e1554e4 [file] [log] [blame]
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -07001/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
4 */
5
6#include <ctype.h>
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -04007#include <errno.h>
8#include <fcntl.h>
Luis Hector Chavez40b25742013-09-22 19:44:06 -07009#include <stdio.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070010#include <string.h>
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -040011#include <sys/stat.h>
12#include <sys/types.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070013
14#include "util.h"
15
Luis Hector Chavez40b25742013-09-22 19:44:06 -070016#include "libconstants.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070017#include "libsyscalls.h"
18
Mike Frysingerfccb4c92013-10-19 02:42:07 -040019/*
20 * These are syscalls used by the syslog() C library call. You can find them
21 * by running a simple test program. See below for x86_64 behavior:
22 * $ cat test.c
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080023 * #include <syslog.h>
Mike Frysingerfccb4c92013-10-19 02:42:07 -040024 * main() { syslog(0, "foo"); }
25 * $ gcc test.c -static
26 * $ strace ./a.out
27 * ...
28 * socket(PF_FILE, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 3 <- look for socket connection
29 * connect(...) <- important
30 * sendto(...) <- important
31 * exit_group(0) <- finish!
32 */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070033#if defined(__x86_64__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080034#if defined(__ANDROID__)
35const char *log_syscalls[] = {"socket", "connect", "fcntl", "writev"};
Alex Deymo7c6899c2016-01-27 18:24:19 -080036#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080037const char *log_syscalls[] = {"connect", "sendto"};
38#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070039#elif defined(__i386__)
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080040#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080041const char *log_syscalls[] = {"socketcall", "writev", "fcntl64",
42 "clock_gettime"};
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080043#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080044const char *log_syscalls[] = {"socketcall", "time"};
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080045#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070046#elif defined(__arm__)
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080047#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080048const char *log_syscalls[] = {"clock_gettime", "connect", "fcntl64", "socket",
49 "writev"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080050#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080051const char *log_syscalls[] = {"connect", "gettimeofday", "send"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080052#endif
53#elif defined(__aarch64__)
54#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080055const char *log_syscalls[] = {"connect", "fcntl", "sendto", "socket", "writev"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080056#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080057const char *log_syscalls[] = {"connect", "send"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080058#endif
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080059#elif defined(__powerpc__) || defined(__ia64__) || defined(__hppa__) || \
60 defined(__sparc__) || defined(__mips__)
61const char *log_syscalls[] = {"connect", "send"};
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070062#else
63#error "Unsupported platform"
64#endif
65
66const size_t log_syscalls_len = sizeof(log_syscalls)/sizeof(log_syscalls[0]);
67
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070068int lookup_syscall(const char *name)
69{
70 const struct syscall_entry *entry = syscall_table;
71 for (; entry->name && entry->nr >= 0; ++entry)
72 if (!strcmp(entry->name, name))
73 return entry->nr;
74 return -1;
75}
76
77const char *lookup_syscall_name(int nr)
78{
79 const struct syscall_entry *entry = syscall_table;
80 for (; entry->name && entry->nr >= 0; ++entry)
81 if (entry->nr == nr)
82 return entry->name;
83 return NULL;
84}
85
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040086long int parse_single_constant(char *constant_str, char **endptr)
87{
88 const struct constant_entry *entry = constant_table;
89 for (; entry->name; ++entry) {
90 if (!strcmp(entry->name, constant_str)) {
91 if (endptr)
92 *endptr = constant_str + strlen(constant_str);
93
94 return entry->value;
95 }
96 }
97
98 return strtol(constant_str, endptr, 0);
99}
100
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700101long int parse_constant(char *constant_str, char **endptr)
102{
Luis Hector Chavez21224552015-06-27 18:10:39 +0000103 long int value = 0;
104 char *group, *lastpos = constant_str;
105 char *original_constant_str = constant_str;
106
107 /*
108 * Try to parse constants separated by pipes. Note that since
109 * |constant_str| is an atom, there can be no spaces between the
110 * constant and the pipe. Constants can be either a named constant
Jorge Lucangeli Obesfd6f8e32016-10-12 11:19:28 -0400111 * defined in libconstants.gen.c or a number parsed with strtol(3).
Luis Hector Chavez21224552015-06-27 18:10:39 +0000112 *
113 * If there is an error parsing any of the constants, the whole process
114 * fails.
115 */
116 while ((group = tokenize(&constant_str, "|")) != NULL) {
117 char *end = group;
118 value |= parse_single_constant(group, &end);
119 if (end == group) {
120 lastpos = original_constant_str;
121 value = 0;
122 break;
123 }
124 lastpos = end;
125 }
126 if (endptr)
127 *endptr = lastpos;
128 return value;
129}
130
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -0700131char *strip(char *s)
132{
133 char *end;
134 while (*s && isblank(*s))
135 s++;
136 end = s + strlen(s) - 1;
137 while (end >= s && *end && (isblank(*end) || *end == '\n'))
138 end--;
139 *(end + 1) = '\0';
140 return s;
141}
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800142
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700143char *tokenize(char **stringp, const char *delim)
144{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800145 char *ret = NULL;
146
147 /* If the string is NULL or empty, there are no tokens to be found. */
148 if (stringp == NULL || *stringp == NULL || **stringp == '\0')
149 return NULL;
150
151 /*
152 * If the delimiter is NULL or empty,
153 * the full string makes up the only token.
154 */
155 if (delim == NULL || *delim == '\0') {
156 ret = *stringp;
157 *stringp = NULL;
158 return ret;
159 }
160
161 char *found;
162 while (**stringp != '\0') {
163 found = strstr(*stringp, delim);
164
165 if (!found) {
166 /*
167 * The delimiter was not found, so the full string
168 * makes up the only token, and we're done.
169 */
170 ret = *stringp;
171 *stringp = NULL;
172 break;
173 }
174
175 if (found != *stringp) {
176 /* There's a non-empty token before the delimiter. */
177 *found = '\0';
178 ret = *stringp;
179 *stringp = found + strlen(delim);
180 break;
181 }
182
183 /*
184 * The delimiter was found at the start of the string,
185 * skip it and keep looking for a non-empty token.
186 */
187 *stringp += strlen(delim);
188 }
189
190 return ret;
191}
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400192
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400193char *path_join(const char *external_path, const char *internal_path)
194{
195 char *path;
196 size_t pathlen;
197
198 /* One extra char for '/' and one for '\0', hence + 2. */
199 pathlen = strlen(external_path) + strlen(internal_path) + 2;
200 path = malloc(pathlen);
201 snprintf(path, pathlen, "%s/%s", external_path, internal_path);
202
203 return path;
204}
205
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -0400206int write_proc_file(pid_t pid, const char *content, const char *basename)
207{
208 int fd, ret;
209 size_t sz, len;
210 ssize_t written;
211 char filename[32];
212
213 sz = sizeof(filename);
214 ret = snprintf(filename, sz, "/proc/%d/%s", pid, basename);
215 if (ret < 0 || (size_t)ret >= sz) {
216 warn("failed to generate %s filename", basename);
217 return -1;
218 }
219
220 fd = open(filename, O_WRONLY | O_CLOEXEC);
221 if (fd < 0) {
222 pwarn("failed to open '%s'", filename);
223 return -errno;
224 }
225
226 len = strlen(content);
227 written = write(fd, content, len);
228 if (written < 0) {
229 pwarn("failed to write '%s'", filename);
230 return -1;
231 }
232
233 if ((size_t)written < len) {
234 warn("failed to write %zu bytes to '%s'", len, filename);
235 return -1;
236 }
237 close(fd);
238 return 0;
239}
240
241int write_pid_to_path(pid_t pid, const char *path)
242{
243 FILE *fp = fopen(path, "w");
244
245 if (!fp) {
246 pwarn("failed to open '%s'", path);
247 return -errno;
248 }
249 if (fprintf(fp, "%d\n", (int)pid) < 0) {
250 /* fprintf(3) does not set errno on failure. */
251 warn("fprintf(%s) failed", path);
252 return -1;
253 }
254 if (fclose(fp)) {
255 pwarn("fclose(%s) failed", path);
256 return -errno;
257 }
258
259 return 0;
260}
261
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400262void *consumebytes(size_t length, char **buf, size_t *buflength)
263{
264 char *p = *buf;
265 if (length > *buflength)
266 return NULL;
267 *buf += length;
268 *buflength -= length;
269 return p;
270}
271
272char *consumestr(char **buf, size_t *buflength)
273{
274 size_t len = strnlen(*buf, *buflength);
275 if (len == *buflength)
276 /* There's no null-terminator. */
277 return NULL;
278 return consumebytes(len + 1, buf, buflength);
279}