blob: 6b16a23d3eaaab96fc5822b8e53bc10df19511d8 [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__)
33const char *log_syscalls[] = { "connect", "gettimeofday", "send" };
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070034#elif defined(__aarch64__) || defined(__powerpc__) || defined(__ia64__) || \
35 defined(__hppa__) || defined(__sparc__) || defined(__mips__)
Mike Frysingerfccb4c92013-10-19 02:42:07 -040036const char *log_syscalls[] = { "connect", "send" };
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070037#else
38#error "Unsupported platform"
39#endif
40
41const size_t log_syscalls_len = sizeof(log_syscalls)/sizeof(log_syscalls[0]);
42
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070043int lookup_syscall(const char *name)
44{
45 const struct syscall_entry *entry = syscall_table;
46 for (; entry->name && entry->nr >= 0; ++entry)
47 if (!strcmp(entry->name, name))
48 return entry->nr;
49 return -1;
50}
51
52const char *lookup_syscall_name(int nr)
53{
54 const struct syscall_entry *entry = syscall_table;
55 for (; entry->name && entry->nr >= 0; ++entry)
56 if (entry->nr == nr)
57 return entry->name;
58 return NULL;
59}
60
Luis Hector Chavez40b25742013-09-22 19:44:06 -070061long int parse_constant(char *constant_str, char **endptr)
62{
63 const struct constant_entry *entry = constant_table;
64 for (; entry->name; ++entry) {
65 if (!strcmp(entry->name, constant_str)) {
66 if (endptr)
67 *endptr = constant_str + strlen(constant_str);
68
69 return entry->value;
70 }
71 }
72
73 return strtol(constant_str, endptr, 0);
74}
75
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070076char *strip(char *s)
77{
78 char *end;
79 while (*s && isblank(*s))
80 s++;
81 end = s + strlen(s) - 1;
82 while (end >= s && *end && (isblank(*end) || *end == '\n'))
83 end--;
84 *(end + 1) = '\0';
85 return s;
86}
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -080087
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070088char *tokenize(char **stringp, const char *delim)
89{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -080090 char *ret = NULL;
91
92 /* If the string is NULL or empty, there are no tokens to be found. */
93 if (stringp == NULL || *stringp == NULL || **stringp == '\0')
94 return NULL;
95
96 /*
97 * If the delimiter is NULL or empty,
98 * the full string makes up the only token.
99 */
100 if (delim == NULL || *delim == '\0') {
101 ret = *stringp;
102 *stringp = NULL;
103 return ret;
104 }
105
106 char *found;
107 while (**stringp != '\0') {
108 found = strstr(*stringp, delim);
109
110 if (!found) {
111 /*
112 * The delimiter was not found, so the full string
113 * makes up the only token, and we're done.
114 */
115 ret = *stringp;
116 *stringp = NULL;
117 break;
118 }
119
120 if (found != *stringp) {
121 /* There's a non-empty token before the delimiter. */
122 *found = '\0';
123 ret = *stringp;
124 *stringp = found + strlen(delim);
125 break;
126 }
127
128 /*
129 * The delimiter was found at the start of the string,
130 * skip it and keep looking for a non-empty token.
131 */
132 *stringp += strlen(delim);
133 }
134
135 return ret;
136}