Debian Codegraph @

staprun/stapio.c

raw
1 /* -*- linux-c -*-
2 *
3 * stapio.c - SystemTap module io handler.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Copyright (C) 2005-2012 Red Hat, Inc.
19 *
20 */
21
22 #include "staprun.h"
23 #include <pwd.h>
24 char *__name__ = "stapio";
25
26 int main(int argc, char **argv)
27 {
28 /* Force libc to make our stderr messages atomic by enabling line
29 buffering since stderr is unbuffered by default. Without this, libc
30 is at liberty to split a single stderr message into multiple writes
31 to the fd while holding flockfile(stderr). POSIX only guarantees that
32 a single write(2) is atomic; chaining several write(2) calls together
33 won't be atomic, and we don't want libc to do that within a single
34 *fprintf(stderr) call since it'll mangle messages printed across
35 different processes (*not* threads). */
36 setlinebuf(stderr);
37
38 #if ENABLE_NLS
39 setlocale (LC_ALL, "");
40 bindtextdomain (PACKAGE, LOCALEDIR);
41 textdomain (PACKAGE);
42 #endif
43 setup_signals();
44 parse_args(argc, argv);
45
46 /* If we inherited a relay_basedir_fd, we want to keep it to ourselves -
47 i.e., FD_CLOEXEC the bad boy. */
48 if (relay_basedir_fd >= 0) {
49 int rc = set_clexec(relay_basedir_fd);
50 if (rc)
51 exit(-1);
52 }
53
54
55 if (buffer_size)
56 dbug(1, "Using a buffer of %u MB.\n", buffer_size);
57
58 if (optind < argc) {
59 parse_modpath(argv[optind++]);
60 dbug(2, "modpath=\"%s\", modname=\"%s\"\n", modpath, modname);
61 }
62
63 if (optind < argc) {
64 if (attach_mod) {
65 err(_("Cannot have module options with attach (-A).\n"));
66 usage(argv[0],1);
67 } else {
68 unsigned start_idx = 3; /* reserve three slots in modoptions[] */
69 while (optind < argc && start_idx + 1 < MAXMODOPTIONS)
70 modoptions[start_idx++] = argv[optind++];
71 modoptions[start_idx] = NULL;
72 }
73 }
74
75 if (modpath == NULL || *modpath == '\0') {
76 err(_("Need a module name or path to load.\n"));
77 usage(argv[0],1);
78 }
79
80 if (init_stapio())
81 exit(1);
82
83 if (stp_main_loop()) {
84 err(_("Couldn't enter main loop. Exiting.\n"));
85 exit(1);
86 }
87
88 return 0;
89 }