Minor fix for ERR() call in lttng
[lttng-tools.git] / lttng / lttng.c
CommitLineData
826d496d
MD
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
fac6795d
DG
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
fac6795d
DG
17 */
18
19#define _GNU_SOURCE
20#include <errno.h>
21#include <fcntl.h>
22#include <getopt.h>
23#include <grp.h>
24#include <limits.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <unistd.h>
32
33#include <lttng/liblttngctl.h>
34
35#include "lttng.h"
36#include "lttngerr.h"
37
38/* Variables */
39static char *progname;
40
41/* Prototypes */
42static int process_client_opt(void);
43static int process_opt_list_apps(void);
44
45/*
46 * start_client
47 *
48 * Process client request from the command line
49 * options. Every tracing action is done by the
50 * liblttngctl API.
51 */
52static int process_client_opt(void)
53{
54 int ret;
55
56 /* Connect to the session daemon */
57 ret = lttng_connect_sessiond();
58 if (ret < 0) {
fac6795d
DG
59 goto end;
60 }
61
62 if (opt_list_apps) {
63 ret = process_opt_list_apps();
64 if (ret < 0) {
fac6795d
DG
65 goto end;
66 }
67 }
68
69 return 0;
70
71end:
ebafd2a5 72 ERR("%s", lttng_get_readable_code(ret));
fac6795d
DG
73 return ret;
74}
75
76/*
77 * process_opt_list_apps
78 *
79 * Get the UST traceable pid list and print
80 * them to the user.
81 */
82static int process_opt_list_apps(void)
83{
84 int i, ret;
85 pid_t *pids;
86 FILE *fp;
87 char path[24]; /* Can't go bigger than /proc/65535/cmdline */
88 char cmdline[PATH_MAX];
89
90 ret = lttng_ust_list_apps(&pids);
91 if (ret < 0) {
92 goto error;
93 }
94
95 MSG("LTTng UST traceable application [name (pid)]:");
96 for (i=0; i < ret; i++) {
97 snprintf(path, sizeof(path), "/proc/%d/cmdline", pids[i]);
98 fp = fopen(path, "r");
99 if (fp == NULL) {
100 continue;
101 }
102 ret = fread(cmdline, 1, sizeof(cmdline), fp);
103 MSG("\t%s (%d)", cmdline, pids[i]);
104 fclose(fp);
105 }
106
e065084a
DG
107 /* Allocated by lttng_ust_list_apps() */
108 free(pids);
109
fac6795d
DG
110 return 0;
111
112error:
113 return ret;
114}
115
116/*
117 * check_ltt_sessiond
118 *
119 * Check if the session daemon is available using
120 * the liblttngctl API for the check.
121 */
122static int check_ltt_sessiond(void)
123{
124 int ret;
125
126 ret = lttng_check_session_daemon();
127 if (ret < 0) {
128 ERR("No session daemon found. Aborting.");
129 }
130
131 return ret;
132}
133
134
135/*
136 * clean_exit
137 */
138void clean_exit(int code)
139{
140 DBG("Clean exit");
141 exit(code);
142}
143
144/*
145 * main
146 */
147int main(int argc, char *argv[])
148{
149 int ret;
150
151 progname = argv[0] ? argv[0] : "lttng";
152
153 /* For Mathieu Desnoyers aka Dr Tracing */
154 if (strncmp(progname, "drtrace", 7) == 0) {
155 MSG("%c[%d;%dmWelcome back Dr Tracing!%c[%dm\n\n", 27,1,33,27,0);
156 }
157
158 ret = parse_args(argc, (const char **) argv);
159 if (ret < 0) {
160 return EXIT_FAILURE;
161 }
162
163 if (opt_tracing_group != NULL) {
164 DBG("Set tracing group to '%s'", opt_tracing_group);
165 lttng_set_tracing_group(opt_tracing_group);
166 }
167
168 /* If ask for kernel tracing, need root perms */
169 if (opt_trace_kernel) {
170 DBG("Kernel tracing activated");
171 if (getuid() != 0) {
172 ERR("%s must be setuid root", progname);
173 return -EPERM;
174 }
175 }
176
177 /* Check if the lttng session daemon is running.
178 * If no, a daemon will be spawned.
179 */
180 if (check_ltt_sessiond() < 0) {
181 return EXIT_FAILURE;
182 }
183
184 ret = process_client_opt();
185 if (ret < 0) {
186 return ret;
187 }
188
189 return 0;
190}
This page took 0.028695 seconds and 4 git commands to generate.