blob: 3d5a3c274f0b22dc5a3f55493ace94782d043575 [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" };
Mike Frysingerfccb4c92013-10-19 02:42:07 -040032#elif defined(__powerpc__) || defined(__ia64__) || defined(__hppa__) || \
33 defined(__sparc__)
34const 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
71char *tokenize(char **stringp, const char *delim) {
72 char *ret = NULL;
73
74 /* If the string is NULL or empty, there are no tokens to be found. */
75 if (stringp == NULL || *stringp == NULL || **stringp == '\0')
76 return NULL;
77
78 /*
79 * If the delimiter is NULL or empty,
80 * the full string makes up the only token.
81 */
82 if (delim == NULL || *delim == '\0') {
83 ret = *stringp;
84 *stringp = NULL;
85 return ret;
86 }
87
88 char *found;
89 while (**stringp != '\0') {
90 found = strstr(*stringp, delim);
91
92 if (!found) {
93 /*
94 * The delimiter was not found, so the full string
95 * makes up the only token, and we're done.
96 */
97 ret = *stringp;
98 *stringp = NULL;
99 break;
100 }
101
102 if (found != *stringp) {
103 /* There's a non-empty token before the delimiter. */
104 *found = '\0';
105 ret = *stringp;
106 *stringp = found + strlen(delim);
107 break;
108 }
109
110 /*
111 * The delimiter was found at the start of the string,
112 * skip it and keep looking for a non-empty token.
113 */
114 *stringp += strlen(delim);
115 }
116
117 return ret;
118}