Commit | Line | Data |
---|---|---|
f3ed775e DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
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. | |
f3ed775e DG |
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 | * | |
d14d33bf AM |
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. | |
f3ed775e DG |
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 | ||
c399183f | 27 | #include "../command.h" |
f3ed775e | 28 | |
42224349 | 29 | #include <common/sessiond-comm/sessiond-comm.h> |
bdae7d71 | 30 | #include <common/utils.h> |
42224349 | 31 | |
fd076c09 | 32 | static char *opt_session_name; |
b09ee5ba | 33 | static int opt_destroy_all; |
f3ed775e DG |
34 | |
35 | enum { | |
36 | OPT_HELP = 1, | |
679b4943 | 37 | OPT_LIST_OPTIONS, |
f3ed775e DG |
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}, | |
b09ee5ba | 43 | {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0}, |
679b4943 | 44 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
f3ed775e DG |
45 | {0, 0, 0, 0, 0, 0, 0} |
46 | }; | |
47 | ||
48 | /* | |
49 | * usage | |
50 | */ | |
51 | static void usage(FILE *ofp) | |
52 | { | |
32a6298d | 53 | fprintf(ofp, "usage: lttng destroy [NAME] [OPTIONS]\n"); |
f3ed775e DG |
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"); | |
32a6298d | 58 | fprintf(ofp, "Options:\n"); |
f3ed775e | 59 | fprintf(ofp, " -h, --help Show this help\n"); |
b09ee5ba | 60 | fprintf(ofp, " -a, --all Destroy all sessions\n"); |
27221701 | 61 | fprintf(ofp, " --list-options Simple listing of options\n"); |
f3ed775e DG |
62 | fprintf(ofp, "\n"); |
63 | } | |
64 | ||
65 | /* | |
b09ee5ba FG |
66 | * destroy_session |
67 | * | |
68 | * Unregister the provided session to the session daemon. On success, removes | |
69 | * the default configuration. | |
f3ed775e | 70 | */ |
b09ee5ba | 71 | static int destroy_session(const char *session_name) |
f3ed775e DG |
72 | { |
73 | int ret; | |
bdae7d71 | 74 | char *default_session_name = NULL; |
f3ed775e | 75 | |
843f5df9 | 76 | ret = lttng_destroy_session(session_name); |
f3ed775e | 77 | if (ret < 0) { |
42224349 | 78 | switch (-ret) { |
f73fabfd | 79 | case LTTNG_ERR_SESS_NOT_FOUND: |
42224349 DG |
80 | WARN("Session name %s not found", session_name); |
81 | break; | |
82 | default: | |
60e835ca | 83 | ERR("%s", lttng_strerror(ret)); |
42224349 DG |
84 | break; |
85 | } | |
b09ee5ba | 86 | goto error; |
f3ed775e DG |
87 | } |
88 | ||
b09ee5ba | 89 | MSG("Session %s destroyed", session_name); |
bdae7d71 PPM |
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 | } | |
f3ed775e | 96 | ret = CMD_SUCCESS; |
b09ee5ba | 97 | error: |
bdae7d71 | 98 | free(default_session_name); |
b09ee5ba FG |
99 | return ret; |
100 | } | |
f3ed775e | 101 | |
b09ee5ba FG |
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."); | |
60e835ca DG |
115 | } else if (count < 0) { |
116 | ERR("%s", lttng_strerror(ret)); | |
117 | goto error; | |
b09ee5ba | 118 | } |
60e835ca | 119 | |
b09ee5ba FG |
120 | for (i = 0; i < count; i++) { |
121 | ret = destroy_session(sessions[i].name); | |
122 | if (ret < 0) { | |
123 | goto error; | |
124 | } | |
b73d0b29 | 125 | } |
f3ed775e DG |
126 | error: |
127 | return ret; | |
128 | } | |
129 | ||
130 | /* | |
843f5df9 | 131 | * The 'destroy <options>' first level command |
f3ed775e DG |
132 | */ |
133 | int cmd_destroy(int argc, const char **argv) | |
134 | { | |
b09ee5ba FG |
135 | int opt; |
136 | int ret = CMD_SUCCESS; | |
f3ed775e | 137 | static poptContext pc; |
b09ee5ba | 138 | char *session_name = NULL; |
f3ed775e DG |
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: | |
ca1c3607 | 146 | usage(stdout); |
b09ee5ba | 147 | break; |
679b4943 SM |
148 | case OPT_LIST_OPTIONS: |
149 | list_cmd_options(stdout, long_options); | |
b09ee5ba | 150 | break; |
f3ed775e DG |
151 | default: |
152 | usage(stderr); | |
153 | ret = CMD_UNDEFINED; | |
b09ee5ba | 154 | break; |
f3ed775e | 155 | } |
b09ee5ba | 156 | goto end; |
f3ed775e DG |
157 | } |
158 | ||
fd076c09 | 159 | /* Ignore session name in case all sessions are to be destroyed */ |
b09ee5ba FG |
160 | if (opt_destroy_all) { |
161 | ret = destroy_all_sessions(); | |
162 | goto end; | |
163 | } | |
fd076c09 CB |
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; | |
b09ee5ba FG |
172 | goto end; |
173 | } | |
fd076c09 CB |
174 | } else { |
175 | session_name = opt_session_name; | |
b09ee5ba | 176 | } |
fd076c09 | 177 | |
b09ee5ba | 178 | ret = destroy_session(session_name); |
f3ed775e DG |
179 | |
180 | end: | |
fd076c09 | 181 | if (opt_session_name == NULL) { |
b09ee5ba FG |
182 | free(session_name); |
183 | } | |
fd076c09 CB |
184 | |
185 | poptFreeContext(pc); | |
f3ed775e DG |
186 | return ret; |
187 | } |