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 DG |
29 | #include <common/sessiond-comm/sessiond-comm.h> |
30 | ||
b09ee5ba | 31 | static int opt_destroy_all; |
f3ed775e DG |
32 | |
33 | enum { | |
34 | OPT_HELP = 1, | |
679b4943 | 35 | OPT_LIST_OPTIONS, |
f3ed775e DG |
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}, | |
b09ee5ba | 41 | {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0}, |
679b4943 | 42 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
f3ed775e DG |
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"); | |
b09ee5ba | 57 | fprintf(ofp, " -a, --all Destroy all sessions\n"); |
27221701 | 58 | fprintf(ofp, " --list-options Simple listing of options\n"); |
f3ed775e DG |
59 | fprintf(ofp, "\n"); |
60 | } | |
61 | ||
62 | /* | |
b09ee5ba FG |
63 | * destroy_session |
64 | * | |
65 | * Unregister the provided session to the session daemon. On success, removes | |
66 | * the default configuration. | |
f3ed775e | 67 | */ |
b09ee5ba | 68 | static int destroy_session(const char *session_name) |
f3ed775e DG |
69 | { |
70 | int ret; | |
f3ed775e | 71 | |
843f5df9 | 72 | ret = lttng_destroy_session(session_name); |
f3ed775e | 73 | if (ret < 0) { |
42224349 DG |
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 | } | |
b09ee5ba | 81 | goto error; |
f3ed775e DG |
82 | } |
83 | ||
b09ee5ba FG |
84 | MSG("Session %s destroyed", session_name); |
85 | config_destroy_default(); | |
f3ed775e | 86 | ret = CMD_SUCCESS; |
b09ee5ba FG |
87 | error: |
88 | return ret; | |
89 | } | |
f3ed775e | 90 | |
b09ee5ba FG |
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 | } | |
b73d0b29 | 110 | } |
f3ed775e DG |
111 | error: |
112 | return ret; | |
113 | } | |
114 | ||
b09ee5ba FG |
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 | ||
f3ed775e | 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 | ||
b09ee5ba | 159 | session_name = (char *) poptGetArg(pc); |
f3ed775e | 160 | |
b09ee5ba FG |
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); | |
f3ed775e DG |
176 | |
177 | end: | |
ca1c3607 | 178 | poptFreeContext(pc); |
b09ee5ba FG |
179 | if (session_name != NULL) { |
180 | free(session_name); | |
181 | } | |
f3ed775e DG |
182 | return ret; |
183 | } |