blob: 3028c9847bff5d46f3da661ec8ff49d3a7474d7a [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
19 * main() { syslog(0, "foo"); }
20 * $ gcc test.c -static
21 * $ strace ./a.out
22 * ...
23 * socket(PF_FILE, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 3 <- look for socket connection
24 * connect(...) <- important
25 * sendto(...) <- important
26 * exit_group(0) <- finish!
27 */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070028#if defined(__x86_64__)
29const char *log_syscalls[] = { "connect", "sendto" };
30#elif defined(__i386__)
31const char *log_syscalls[] = { "socketcall", "time" };
32#elif defined(__arm__)
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080033#if defined(__ANDROID__)
34const char *log_syscalls[] = { "clock_gettime", "connect", "fcntl64", "socket", "writev" };
35#else
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070036const char *log_syscalls[] = { "connect", "gettimeofday", "send" };
Jeff Vander Stoepd38f3992015-12-02 10:54:51 -080037#endif
38#elif defined(__aarch64__)
39#if defined(__ANDROID__)
40const char *log_syscalls[] = { "connect", "fcntl", "sendto", "socket", "writev" };
41#else
42const char *log_syscalls[] = { "connect", "send" };
43#endif
44#elif defined(__powerpc__) || defined(__ia64__) || defined(__hppa__) \
45 || defined(__sparc__) || defined(__mips__)
Mike Frysingerfccb4c92013-10-19 02:42:07 -040046const char *log_syscalls[] = { "connect", "send" };
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070047#else
48#error "Unsupported platform"
49#endif
50
51const size_t log_syscalls_len = sizeof(log_syscalls)/sizeof(log_syscalls[0]);
52
Luis Hector Chavez21224552015-06-27 18:10:39 +000053long int parse_single_constant(char *constant_str, char **endptr);
54
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070055int lookup_syscall(const char *name)
56{
57 const struct syscall_entry *entry = syscall_table;
58 for (; entry->name && entry->nr >= 0; ++entry)
59 if (!strcmp(entry->name, name))
60 return entry->nr;
61 return -1;
62}
63
64const char *lookup_syscall_name(int nr)
65{
66 const struct syscall_entry *entry = syscall_table;
67 for (; entry->name && entry->nr >= 0; ++entry)
68 if (entry->nr == nr)
69 return entry->name;
70 return NULL;
71}
72
Luis Hector Chavez40b25742013-09-22 19:44:06 -070073long int parse_constant(char *constant_str, char **endptr)
74{
Luis Hector Chavez21224552015-06-27 18:10:39 +000075 long int value = 0;
76 char *group, *lastpos = constant_str;
77 char *original_constant_str = constant_str;
78
79 /*
80 * Try to parse constants separated by pipes. Note that since
81 * |constant_str| is an atom, there can be no spaces between the
82 * constant and the pipe. Constants can be either a named constant
83 * defined in libconstants.gen.c or a number parsed with strtol.
84 *
85 * If there is an error parsing any of the constants, the whole process
86 * fails.
87 */
88 while ((group = tokenize(&constant_str, "|")) != NULL) {
89 char *end = group;
90 value |= parse_single_constant(group, &end);
91 if (end == group) {
92 lastpos = original_constant_str;
93 value = 0;
94 break;
95 }
96 lastpos = end;
97 }
98 if (endptr)
99 *endptr = lastpos;
100 return value;
101}
102
103long int parse_single_constant(char *constant_str, char **endptr)
104{
Luis Hector Chavez40b25742013-09-22 19:44:06 -0700105 const struct constant_entry *entry = constant_table;
106 for (; entry->name; ++entry) {
107 if (!strcmp(entry->name, constant_str)) {
108 if (endptr)
109 *endptr = constant_str + strlen(constant_str);
110
111 return entry->value;
112 }
113 }
114
115 return strtol(constant_str, endptr, 0);
116}
117
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -0700118char *strip(char *s)
119{
120 char *end;
121 while (*s && isblank(*s))
122 s++;
123 end = s + strlen(s) - 1;
124 while (end >= s && *end && (isblank(*end) || *end == '\n'))
125 end--;
126 *(end + 1) = '\0';
127 return s;
128}
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800129
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -0700130char *tokenize(char **stringp, const char *delim)
131{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -0800132 char *ret = NULL;
133
134 /* If the string is NULL or empty, there are no tokens to be found. */
135 if (stringp == NULL || *stringp == NULL || **stringp == '\0')
136 return NULL;
137
138 /*
139 * If the delimiter is NULL or empty,
140 * the full string makes up the only token.
141 */
142 if (delim == NULL || *delim == '\0') {
143 ret = *stringp;
144 *stringp = NULL;
145 return ret;
146 }
147
148 char *found;
149 while (**stringp != '\0') {
150 found = strstr(*stringp, delim);
151
152 if (!found) {
153 /*
154 * The delimiter was not found, so the full string
155 * makes up the only token, and we're done.
156 */
157 ret = *stringp;
158 *stringp = NULL;
159 break;
160 }
161
162 if (found != *stringp) {
163 /* There's a non-empty token before the delimiter. */
164 *found = '\0';
165 ret = *stringp;
166 *stringp = found + strlen(delim);
167 break;
168 }
169
170 /*
171 * The delimiter was found at the start of the string,
172 * skip it and keep looking for a non-empty token.
173 */
174 *stringp += strlen(delim);
175 }
176
177 return ret;
178}