Fix: destroy session removes the default config file
[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 #include <common/utils.h>
31
32 static char *opt_session_name;
33 static int opt_destroy_all;
34
35 enum {
36 OPT_HELP = 1,
37 OPT_LIST_OPTIONS,
38 };
39
40 static struct poptOption long_options[] = {
41 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
42 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
43 {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0},
44 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
45 {0, 0, 0, 0, 0, 0, 0}
46 };
47
48 /*
49 * usage
50 */
51 static void usage(FILE *ofp)
52 {
53 fprintf(ofp, "usage: lttng destroy [NAME] [OPTIONS]\n");
54 fprintf(ofp, "\n");
55 fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n");
56 fprintf(ofp, "get it from the configuration directory (.lttng).\n");
57 fprintf(ofp, "\n");
58 fprintf(ofp, "Options:\n");
59 fprintf(ofp, " -h, --help Show this help\n");
60 fprintf(ofp, " -a, --all Destroy all sessions\n");
61 fprintf(ofp, " --list-options Simple listing of options\n");
62 fprintf(ofp, "\n");
63 }
64
65 /*
66 * destroy_session
67 *
68 * Unregister the provided session to the session daemon. On success, removes
69 * the default configuration.
70 */
71 static int destroy_session(const char *session_name)
72 {
73 int ret;
74 char *default_session_name = NULL;
75
76 ret = lttng_destroy_session(session_name);
77 if (ret < 0) {
78 switch (-ret) {
79 case LTTNG_ERR_SESS_NOT_FOUND:
80 WARN("Session name %s not found", session_name);
81 break;
82 default:
83 ERR("%s", lttng_strerror(ret));
84 break;
85 }
86 goto error;
87 }
88
89 MSG("Session %s destroyed", session_name);
90
91 default_session_name = get_session_name_quiet();
92 if (default_session_name &&
93 !strncmp(session_name, session_name, NAME_MAX)) {
94 config_destroy_default();
95 }
96 ret = CMD_SUCCESS;
97 error:
98 free(default_session_name);
99 return ret;
100 }
101
102 /*
103 * destroy_all_sessions
104 *
105 * Call destroy_sessions for each registered sessions
106 */
107 static int destroy_all_sessions()
108 {
109 int count, i, ret = CMD_SUCCESS;
110 struct lttng_session *sessions;
111
112 count = lttng_list_sessions(&sessions);
113 if (count == 0) {
114 MSG("No session found, nothing to do.");
115 } else if (count < 0) {
116 ERR("%s", lttng_strerror(ret));
117 goto error;
118 }
119
120 for (i = 0; i < count; i++) {
121 ret = destroy_session(sessions[i].name);
122 if (ret < 0) {
123 goto error;
124 }
125 }
126 error:
127 return ret;
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 /* Ignore session name in case all sessions are to be destroyed */
160 if (opt_destroy_all) {
161 ret = destroy_all_sessions();
162 goto end;
163 }
164
165 opt_session_name = (char *) poptGetArg(pc);
166
167 if (opt_session_name == NULL) {
168 /* No session name specified, lookup default */
169 session_name = get_session_name();
170 if (session_name == NULL) {
171 ret = CMD_ERROR;
172 goto end;
173 }
174 } else {
175 session_name = opt_session_name;
176 }
177
178 ret = destroy_session(session_name);
179
180 end:
181 if (opt_session_name == NULL) {
182 free(session_name);
183 }
184
185 poptFreeContext(pc);
186 return ret;
187 }
This page took 0.0338850000000001 seconds and 4 git commands to generate.