5b69cb5d9b45e54f7f7877f859bde66eb5febd54
[lttng-tools.git] / src / bin / lttng / commands / destroy.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _GNU_SOURCE
19 #include <popt.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26
27 #include "../command.h"
28
29 #include <common/sessiond-comm/sessiond-comm.h>
30
31 static int opt_destroy_all;
32
33 enum {
34 OPT_HELP = 1,
35 OPT_LIST_OPTIONS,
36 };
37
38 static struct poptOption long_options[] = {
39 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
40 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
41 {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0},
42 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
43 {0, 0, 0, 0, 0, 0, 0}
44 };
45
46 /*
47 * usage
48 */
49 static void usage(FILE *ofp)
50 {
51 fprintf(ofp, "usage: lttng destroy [options] [NAME]\n");
52 fprintf(ofp, "\n");
53 fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n");
54 fprintf(ofp, "get it from the configuration directory (.lttng).\n");
55 fprintf(ofp, "\n");
56 fprintf(ofp, " -h, --help Show this help\n");
57 fprintf(ofp, " -a, --all Destroy all sessions\n");
58 fprintf(ofp, " --list-options Simple listing of options\n");
59 fprintf(ofp, "\n");
60 }
61
62 /*
63 * destroy_session
64 *
65 * Unregister the provided session to the session daemon. On success, removes
66 * the default configuration.
67 */
68 static int destroy_session(const char *session_name)
69 {
70 int ret;
71
72 ret = lttng_destroy_session(session_name);
73 if (ret < 0) {
74 switch (-ret) {
75 case LTTCOMM_SESS_NOT_FOUND:
76 WARN("Session name %s not found", session_name);
77 break;
78 default:
79 break;
80 }
81 goto error;
82 }
83
84 MSG("Session %s destroyed", session_name);
85 config_destroy_default();
86 ret = CMD_SUCCESS;
87 error:
88 return ret;
89 }
90
91 /*
92 * destroy_all_sessions
93 *
94 * Call destroy_sessions for each registered sessions
95 */
96 static int destroy_all_sessions()
97 {
98 int count, i, ret = CMD_SUCCESS;
99 struct lttng_session *sessions;
100
101 count = lttng_list_sessions(&sessions);
102 if (count == 0) {
103 MSG("No session found, nothing to do.");
104 }
105 for (i = 0; i < count; i++) {
106 ret = destroy_session(sessions[i].name);
107 if (ret < 0) {
108 goto error;
109 }
110 }
111 error:
112 return ret;
113 }
114
115 /*
116 * get_default_session_name
117 *
118 * Returns the default sessions name, if any
119 */
120 static int get_default_session_name(char **name)
121 {
122 char *session_name = get_session_name();
123 if (session_name == NULL) {
124 return CMD_ERROR;
125 }
126 *name = session_name;
127 return CMD_SUCCESS;
128 }
129
130 /*
131 * The 'destroy <options>' first level command
132 */
133 int cmd_destroy(int argc, const char **argv)
134 {
135 int opt;
136 int ret = CMD_SUCCESS;
137 static poptContext pc;
138 char *session_name = NULL;
139
140 pc = poptGetContext(NULL, argc, argv, long_options, 0);
141 poptReadDefaultConfig(pc, 0);
142
143 while ((opt = poptGetNextOpt(pc)) != -1) {
144 switch (opt) {
145 case OPT_HELP:
146 usage(stdout);
147 break;
148 case OPT_LIST_OPTIONS:
149 list_cmd_options(stdout, long_options);
150 break;
151 default:
152 usage(stderr);
153 ret = CMD_UNDEFINED;
154 break;
155 }
156 goto end;
157 }
158
159 session_name = (char *) poptGetArg(pc);
160
161 /*
162 * ignore session name in case all
163 * sessions are to be destroyed
164 */
165 if (opt_destroy_all) {
166 ret = destroy_all_sessions();
167 goto end;
168 }
169 if (session_name == NULL) {
170 ret = get_default_session_name(&session_name);
171 if (ret < 0 || session_name == NULL) {
172 goto end;
173 }
174 }
175 ret = destroy_session(session_name);
176
177 end:
178 poptFreeContext(pc);
179 if (session_name != NULL) {
180 free(session_name);
181 }
182 return ret;
183 }
This page took 0.047968 seconds and 3 git commands to generate.