blob: 550ed78ecfac26fa3d5e2a20cc643f8def6126dc [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>
7#include <string.h>
8
9#include "util.h"
10
11#include "libsyscalls.h"
12
Mike Frysingerfccb4c92013-10-19 02:42:07 -040013/*
14 * These are syscalls used by the syslog() C library call. You can find them
15 * by running a simple test program. See below for x86_64 behavior:
16 * $ cat test.c
17 * main() { syslog(0, "foo"); }
18 * $ gcc test.c -static
19 * $ strace ./a.out
20 * ...
21 * socket(PF_FILE, SOCK_DGRAM|SOCK_CLOEXEC, 0) = 3 <- look for socket connection
22 * connect(...) <- important
23 * sendto(...) <- important
24 * exit_group(0) <- finish!
25 */
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070026#if defined(__x86_64__)
27const char *log_syscalls[] = { "connect", "sendto" };
28#elif defined(__i386__)
29const char *log_syscalls[] = { "socketcall", "time" };
30#elif defined(__arm__)
31const char *log_syscalls[] = { "connect", "gettimeofday", "send" };
Jorge Lucangeli Obesa21c8fc2015-07-15 16:22:34 -070032#elif defined(__aarch64__) || defined(__powerpc__) || defined(__ia64__) || \
33 defined(__hppa__) || defined(__sparc__) || defined(__mips__)
Mike Frysingerfccb4c92013-10-19 02:42:07 -040034const char *log_syscalls[] = { "connect", "send" };
Jorge Lucangeli Obesbda833c2012-07-31 16:25:56 -070035#else
36#error "Unsupported platform"
37#endif
38
39const size_t log_syscalls_len = sizeof(log_syscalls)/sizeof(log_syscalls[0]);
40
Jorge Lucangeli Obesa6b034d2012-08-07 15:29:20 -070041int lookup_syscall(const char *name)
42{
43 const struct syscall_entry *entry = syscall_table;
44 for (; entry->name && entry->nr >= 0; ++entry)
45 if (!strcmp(entry->name, name))
46 return entry->nr;
47 return -1;
48}
49
50const char *lookup_syscall_name(int nr)
51{
52 const struct syscall_entry *entry = syscall_table;
53 for (; entry->name && entry->nr >= 0; ++entry)
54 if (entry->nr == nr)
55 return entry->name;
56 return NULL;
57}
58
59char *strip(char *s)
60{
61 char *end;
62 while (*s && isblank(*s))
63 s++;
64 end = s + strlen(s) - 1;
65 while (end >= s && *end && (isblank(*end) || *end == '\n'))
66 end--;
67 *(end + 1) = '\0';
68 return s;
69}
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -080070
Jorge Lucangeli Obesc8b21e12014-06-13 14:26:16 -070071char *tokenize(char **stringp, const char *delim)
72{
Jorge Lucangeli Obes66cfc142012-11-30 15:42:52 -080073 char *ret = NULL;
74
75 /* If the string is NULL or empty, there are no tokens to be found. */
76 if (stringp == NULL || *stringp == NULL || **stringp == '\0')
77 return NULL;
78
79 /*
80 * If the delimiter is NULL or empty,
81 * the full string makes up the only token.
82 */
83 if (delim == NULL || *delim == '\0') {
84 ret = *stringp;
85 *stringp = NULL;
86 return ret;
87 }
88
89 char *found;
90 while (**stringp != '\0') {
91 found = strstr(*stringp, delim);
92
93 if (!found) {
94 /*
95 * The delimiter was not found, so the full string
96 * makes up the only token, and we're done.
97 */
98 ret = *stringp;
99 *stringp = NULL;
100 break;
101 }
102
103 if (found != *stringp) {
104 /* There's a non-empty token before the delimiter. */
105 *found = '\0';
106 ret = *stringp;
107 *stringp = found + strlen(delim);
108 break;
109 }
110
111 /*
112 * The delimiter was found at the start of the string,
113 * skip it and keep looking for a non-empty token.
114 */
115 *stringp += strlen(delim);
116 }
117
118 return ret;
119}