blob: 3de8eb7b48dc73ba9737cf2e2d1f64392e74bd48 [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>
Luis Hector Chavez40b25742013-09-22 19:44:06 -07007#include <stdio.h>
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -07008#include <string.h>
9
10#include "util.h"
11
Luis Hector Chavez40b25742013-09-22 19:44:06 -070012#include "libconstants.h"
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070013#include "libsyscalls.h"
14
Mike Frysingerfccb4c92013-10-19 02:42:07 -040015/*
16 * These are syscalls used by the syslog() C library call. You can find them
17 * by running a simple test program. See below for x86_64 behavior:
18 * $ cat test.c
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080019 * #include <syslog.h>
Mike Frysingerfccb4c92013-10-19 02:42:07 -040020 * main() { syslog(0, "foo"); }
21 * $ gcc test.c -static
22 * $ strace ./a.out
23 * ...
24 * socket(PF_FILE, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 3 <- look for socket connection
25 * connect(...) <- important
26 * sendto(...) <- important
27 * exit_group(0) <- finish!
28 */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070029#if defined(__x86_64__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080030#if defined(__ANDROID__)
31const char *log_syscalls[] = {"socket", "connect", "fcntl", "writev"};
Alex Deymo7c6899c2016-01-27 18:24:19 -080032#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080033const char *log_syscalls[] = {"connect", "sendto"};
34#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070035#elif defined(__i386__)
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080036#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080037const char *log_syscalls[] = {"socketcall", "writev", "fcntl64",
38 "clock_gettime"};
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080039#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080040const char *log_syscalls[] = {"socketcall", "time"};
Jeff Vander Stoep1482f202016-01-07 11:21:31 -080041#endif
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070042#elif defined(__arm__)
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080043#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080044const char *log_syscalls[] = {"clock_gettime", "connect", "fcntl64", "socket",
45 "writev"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080046#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080047const char *log_syscalls[] = {"connect", "gettimeofday", "send"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080048#endif
49#elif defined(__aarch64__)
50#if defined(__ANDROID__)
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080051const char *log_syscalls[] = {"connect", "fcntl", "sendto", "socket", "writev"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080052#else
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080053const char *log_syscalls[] = {"connect", "send"};
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080054#endif
Jorge Lucangeli Obesb98ad292016-01-25 15:07:30 -080055#elif defined(__powerpc__) || defined(__ia64__) || defined(__hppa__) || \
56 defined(__sparc__) || defined(__mips__)
57const char *log_syscalls[] = {"connect", "send"};
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070058#else
59#error "Unsupported platform"
60#endif
61
62const size_t log_syscalls_len = sizeof(log_syscalls)/sizeof(log_syscalls[0]);
63
Luis Hector Chavez21224552015-06-27 18:10:39 +000064long int parse_single_constant(char *constant_str, char **endptr);
65
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070066int lookup_syscall(const char *name)
67{
68 const struct syscall_entry *entry = syscall_table;
69 for (; entry->name && entry->nr >= 0; ++entry)
70 if (!strcmp(entry->name, name))
71 return entry->nr;
72 return -1;
73}
74
75const char *lookup_syscall_name(int nr)
76{
77 const struct syscall_entry *entry = syscall_table;
78 for (; entry->name && entry->nr >= 0; ++entry)
79 if (entry->nr == nr)
80 return entry->name;
81 return NULL;
82}
83
Luis Hector Chavez40b25742013-09-22 19:44:06 -070084long int parse_constant(char *constant_str, char **endptr)
85{
Luis Hector Chavez21224552015-06-27 18:10:39 +000086 long int value = 0;
87 char *group, *lastpos = constant_str;
88 char *original_constant_str = constant_str;
89
90 /*
91 * Try to parse constants separated by pipes. Note that since
92 * |constant_str| is an atom, there can be no spaces between the
93 * constant and the pipe. Constants can be either a named constant
94 * defined in libconstants.gen.c or a number parsed with strtol.
95 *
96 * If there is an error parsing any of the constants, the whole process
97 * fails.
98 */
99 while ((group = tokenize(&constant_str, "|")) != NULL) {
100 char *end = group;
101 value |= parse_single_constant(group, &end);
102 if (end == group) {
103 lastpos = original_constant_str;
104 value = 0;
105 break;
106 }
107 lastpos = end;
108 }
109 if (endptr)
110 *endptr = lastpos;
111 return value;
112}
113
114long int parse_single_constant(char *constant_str, char **endptr)
115{
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700116 const struct constant_entry *entry = constant_table;
117 for (; entry->name; ++entry) {
118 if (!strcmp(entry->name, constant_str)) {
119 if (endptr)
120 *endptr = constant_str + strlen(constant_str);
121
122 return entry->value;
123 }
124 }
125
126 return strtol(constant_str, endptr, 0);
127}
128
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -0700129char *strip(char *s)
130{
131 char *end;
132 while (*s && isblank(*s))
133 s++;
134 end = s + strlen(s) - 1;
135 while (end >= s && *end && (isblank(*end) || *end == '\n'))
136 end--;
137 *(end + 1) = '\0';
138 return s;
139}
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800140
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700141char *tokenize(char **stringp, const char *delim)
142{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800143 char *ret = NULL;
144
145 /* If the string is NULL or empty, there are no tokens to be found. */
146 if (stringp == NULL || *stringp == NULL || **stringp == '\0')
147 return NULL;
148
149 /*
150 * If the delimiter is NULL or empty,
151 * the full string makes up the only token.
152 */
153 if (delim == NULL || *delim == '\0') {
154 ret = *stringp;
155 *stringp = NULL;
156 return ret;
157 }
158
159 char *found;
160 while (**stringp != '\0') {
161 found = strstr(*stringp, delim);
162
163 if (!found) {
164 /*
165 * The delimiter was not found, so the full string
166 * makes up the only token, and we're done.
167 */
168 ret = *stringp;
169 *stringp = NULL;
170 break;
171 }
172
173 if (found != *stringp) {
174 /* There's a non-empty token before the delimiter. */
175 *found = '\0';
176 ret = *stringp;
177 *stringp = found + strlen(delim);
178 break;
179 }
180
181 /*
182 * The delimiter was found at the start of the string,
183 * skip it and keep looking for a non-empty token.
184 */
185 *stringp += strlen(delim);
186 }
187
188 return ret;
189}