blob: b82e308c58a0eae6c71625c71b5483f3f122651d [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 Obes7b2e29c2016-08-04 12:21:03 -040011#include <sys/utsname.h>
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -040012#include <sys/stat.h>
13#include <sys/types.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070014
15#include "util.h"
16
Luis Hector Chavez40b25742013-09-22 19:44:06 -070017#include "libconstants.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070018#include "libsyscalls.h"
19
Mike Frysingerfccb4c92013-10-19 02:42:07 -040020/*
21 * These are syscalls used by the syslog() C library call. You can find them
22 * by running a simple test program. See below for x86_64 behavior:
23 * $ cat test.c
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080024 * #include <syslog.h>
Mike Frysingerfccb4c92013-10-19 02:42:07 -040025 * main() { syslog(0, "foo"); }
26 * $ gcc test.c -static
27 * $ strace ./a.out
28 * ...
29 * socket(PF_FILE, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 3 <- look for socket connection
30 * connect(...) <- important
31 * sendto(...) <- important
32 * exit_group(0) <- finish!
33 */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070034#if defined(__x86_64__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080035#if defined(__ANDROID__)
36const char *log_syscalls[] = {"socket", "connect", "fcntl", "writev"};
Alex Deymo7c6899c2016-01-27 18:24:19 -080037#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080038const char *log_syscalls[] = {"connect", "sendto"};
39#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070040#elif defined(__i386__)
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080041#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080042const char *log_syscalls[] = {"socketcall", "writev", "fcntl64",
43 "clock_gettime"};
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080044#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080045const char *log_syscalls[] = {"socketcall", "time"};
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080046#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070047#elif defined(__arm__)
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080048#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080049const char *log_syscalls[] = {"clock_gettime", "connect", "fcntl64", "socket",
50 "writev"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080051#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080052const char *log_syscalls[] = {"connect", "gettimeofday", "send"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080053#endif
54#elif defined(__aarch64__)
55#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080056const char *log_syscalls[] = {"connect", "fcntl", "sendto", "socket", "writev"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080057#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080058const char *log_syscalls[] = {"connect", "send"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080059#endif
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080060#elif defined(__powerpc__) || defined(__ia64__) || defined(__hppa__) || \
61 defined(__sparc__) || defined(__mips__)
62const char *log_syscalls[] = {"connect", "send"};
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070063#else
64#error "Unsupported platform"
65#endif
66
67const size_t log_syscalls_len = sizeof(log_syscalls)/sizeof(log_syscalls[0]);
68
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070069int lookup_syscall(const char *name)
70{
71 const struct syscall_entry *entry = syscall_table;
72 for (; entry->name && entry->nr >= 0; ++entry)
73 if (!strcmp(entry->name, name))
74 return entry->nr;
75 return -1;
76}
77
78const char *lookup_syscall_name(int nr)
79{
80 const struct syscall_entry *entry = syscall_table;
81 for (; entry->name && entry->nr >= 0; ++entry)
82 if (entry->nr == nr)
83 return entry->name;
84 return NULL;
85}
86
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -040087long int parse_single_constant(char *constant_str, char **endptr)
88{
89 const struct constant_entry *entry = constant_table;
90 for (; entry->name; ++entry) {
91 if (!strcmp(entry->name, constant_str)) {
92 if (endptr)
93 *endptr = constant_str + strlen(constant_str);
94
95 return entry->value;
96 }
97 }
98
99 return strtol(constant_str, endptr, 0);
100}
101
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700102long int parse_constant(char *constant_str, char **endptr)
103{
Luis Hector Chavez21224552015-06-27 18:10:39 +0000104 long int value = 0;
105 char *group, *lastpos = constant_str;
106 char *original_constant_str = constant_str;
107
108 /*
109 * Try to parse constants separated by pipes. Note that since
110 * |constant_str| is an atom, there can be no spaces between the
111 * constant and the pipe. Constants can be either a named constant
112 * defined in libconstants.gen.c or a number parsed with strtol.
113 *
114 * If there is an error parsing any of the constants, the whole process
115 * fails.
116 */
117 while ((group = tokenize(&constant_str, "|")) != NULL) {
118 char *end = group;
119 value |= parse_single_constant(group, &end);
120 if (end == group) {
121 lastpos = original_constant_str;
122 value = 0;
123 break;
124 }
125 lastpos = end;
126 }
127 if (endptr)
128 *endptr = lastpos;
129 return value;
130}
131
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -0700132char *strip(char *s)
133{
134 char *end;
135 while (*s && isblank(*s))
136 s++;
137 end = s + strlen(s) - 1;
138 while (end >= s && *end && (isblank(*end) || *end == '\n'))
139 end--;
140 *(end + 1) = '\0';
141 return s;
142}
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800143
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700144char *tokenize(char **stringp, const char *delim)
145{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800146 char *ret = NULL;
147
148 /* If the string is NULL or empty, there are no tokens to be found. */
149 if (stringp == NULL || *stringp == NULL || **stringp == '\0')
150 return NULL;
151
152 /*
153 * If the delimiter is NULL or empty,
154 * the full string makes up the only token.
155 */
156 if (delim == NULL || *delim == '\0') {
157 ret = *stringp;
158 *stringp = NULL;
159 return ret;
160 }
161
162 char *found;
163 while (**stringp != '\0') {
164 found = strstr(*stringp, delim);
165
166 if (!found) {
167 /*
168 * The delimiter was not found, so the full string
169 * makes up the only token, and we're done.
170 */
171 ret = *stringp;
172 *stringp = NULL;
173 break;
174 }
175
176 if (found != *stringp) {
177 /* There's a non-empty token before the delimiter. */
178 *found = '\0';
179 ret = *stringp;
180 *stringp = found + strlen(delim);
181 break;
182 }
183
184 /*
185 * The delimiter was found at the start of the string,
186 * skip it and keep looking for a non-empty token.
187 */
188 *stringp += strlen(delim);
189 }
190
191 return ret;
192}
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400193
194int kernel_lessthan_3_8()
195{
196 int major, minor;
197 struct utsname uts;
198 return (uname(&uts) != -1 &&
199 sscanf(uts.release, "%d.%d", &major, &minor) == 2 &&
200 ((major < 3) || ((major == 3) && (minor < 8))));
201}
202
203char *path_join(const char *external_path, const char *internal_path)
204{
205 char *path;
206 size_t pathlen;
207
208 /* One extra char for '/' and one for '\0', hence + 2. */
209 pathlen = strlen(external_path) + strlen(internal_path) + 2;
210 path = malloc(pathlen);
211 snprintf(path, pathlen, "%s/%s", external_path, internal_path);
212
213 return path;
214}
215
Jorge Lucangeli Obesf205fff2016-08-06 09:06:21 -0400216int write_proc_file(pid_t pid, const char *content, const char *basename)
217{
218 int fd, ret;
219 size_t sz, len;
220 ssize_t written;
221 char filename[32];
222
223 sz = sizeof(filename);
224 ret = snprintf(filename, sz, "/proc/%d/%s", pid, basename);
225 if (ret < 0 || (size_t)ret >= sz) {
226 warn("failed to generate %s filename", basename);
227 return -1;
228 }
229
230 fd = open(filename, O_WRONLY | O_CLOEXEC);
231 if (fd < 0) {
232 pwarn("failed to open '%s'", filename);
233 return -errno;
234 }
235
236 len = strlen(content);
237 written = write(fd, content, len);
238 if (written < 0) {
239 pwarn("failed to write '%s'", filename);
240 return -1;
241 }
242
243 if ((size_t)written < len) {
244 warn("failed to write %zu bytes to '%s'", len, filename);
245 return -1;
246 }
247 close(fd);
248 return 0;
249}
250
251int write_pid_to_path(pid_t pid, const char *path)
252{
253 FILE *fp = fopen(path, "w");
254
255 if (!fp) {
256 pwarn("failed to open '%s'", path);
257 return -errno;
258 }
259 if (fprintf(fp, "%d\n", (int)pid) < 0) {
260 /* fprintf(3) does not set errno on failure. */
261 warn("fprintf(%s) failed", path);
262 return -1;
263 }
264 if (fclose(fp)) {
265 pwarn("fclose(%s) failed", path);
266 return -errno;
267 }
268
269 return 0;
270}
271
Jorge Lucangeli Obes7b2e29c2016-08-04 12:21:03 -0400272void *consumebytes(size_t length, char **buf, size_t *buflength)
273{
274 char *p = *buf;
275 if (length > *buflength)
276 return NULL;
277 *buf += length;
278 *buflength -= length;
279 return p;
280}
281
282char *consumestr(char **buf, size_t *buflength)
283{
284 size_t len = strnlen(*buf, *buflength);
285 if (len == *buflength)
286 /* There's no null-terminator. */
287 return NULL;
288 return consumebytes(len + 1, buf, buflength);
289}