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 | ||
fd076c09 | 31 | static char *opt_session_name; |
b09ee5ba | 32 | static int opt_destroy_all; |
f3ed775e DG |
33 | |
34 | enum { | |
35 | OPT_HELP = 1, | |
679b4943 | 36 | OPT_LIST_OPTIONS, |
f3ed775e DG |
37 | }; |
38 | ||
39 | static struct poptOption long_options[] = { | |
40 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
41 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
b09ee5ba | 42 | {"all", 'a', POPT_ARG_VAL, &opt_destroy_all, 1, 0, 0}, |
679b4943 | 43 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
f3ed775e DG |
44 | {0, 0, 0, 0, 0, 0, 0} |
45 | }; | |
46 | ||
47 | /* | |
48 | * usage | |
49 | */ | |
50 | static void usage(FILE *ofp) | |
51 | { | |
52 | fprintf(ofp, "usage: lttng destroy [options] [NAME]\n"); | |
53 | fprintf(ofp, "\n"); | |
54 | fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n"); | |
55 | fprintf(ofp, "get it from the configuration directory (.lttng).\n"); | |
56 | fprintf(ofp, "\n"); | |
57 | fprintf(ofp, " -h, --help Show this help\n"); | |
b09ee5ba | 58 | fprintf(ofp, " -a, --all Destroy all sessions\n"); |
27221701 | 59 | fprintf(ofp, " --list-options Simple listing of options\n"); |
f3ed775e DG |
60 | fprintf(ofp, "\n"); |
61 | } | |
62 | ||
63 | /* | |
b09ee5ba FG |
64 | * destroy_session |
65 | * | |
66 | * Unregister the provided session to the session daemon. On success, removes | |
67 | * the default configuration. | |
f3ed775e | 68 | */ |
b09ee5ba | 69 | static int destroy_session(const char *session_name) |
f3ed775e DG |
70 | { |
71 | int ret; | |
f3ed775e | 72 | |
843f5df9 | 73 | ret = lttng_destroy_session(session_name); |
f3ed775e | 74 | if (ret < 0) { |
42224349 DG |
75 | switch (-ret) { |
76 | case LTTCOMM_SESS_NOT_FOUND: | |
77 | WARN("Session name %s not found", session_name); | |
78 | break; | |
79 | default: | |
80 | break; | |
81 | } | |
b09ee5ba | 82 | goto error; |
f3ed775e DG |
83 | } |
84 | ||
b09ee5ba FG |
85 | MSG("Session %s destroyed", session_name); |
86 | config_destroy_default(); | |
f3ed775e | 87 | ret = CMD_SUCCESS; |
b09ee5ba FG |
88 | error: |
89 | return ret; | |
90 | } | |
f3ed775e | 91 | |
b09ee5ba FG |
92 | /* |
93 | * destroy_all_sessions | |
94 | * | |
95 | * Call destroy_sessions for each registered sessions | |
96 | */ | |
97 | static int destroy_all_sessions() | |
98 | { | |
99 | int count, i, ret = CMD_SUCCESS; | |
100 | struct lttng_session *sessions; | |
101 | ||
102 | count = lttng_list_sessions(&sessions); | |
103 | if (count == 0) { | |
104 | MSG("No session found, nothing to do."); | |
105 | } | |
106 | for (i = 0; i < count; i++) { | |
107 | ret = destroy_session(sessions[i].name); | |
108 | if (ret < 0) { | |
109 | goto error; | |
110 | } | |
b73d0b29 | 111 | } |
f3ed775e DG |
112 | error: |
113 | return ret; | |
114 | } | |
115 | ||
b09ee5ba FG |
116 | /* |
117 | * get_default_session_name | |
118 | * | |
119 | * Returns the default sessions name, if any | |
120 | */ | |
121 | static int get_default_session_name(char **name) | |
122 | { | |
123 | char *session_name = get_session_name(); | |
124 | if (session_name == NULL) { | |
125 | return CMD_ERROR; | |
126 | } | |
127 | *name = session_name; | |
128 | return CMD_SUCCESS; | |
129 | } | |
130 | ||
f3ed775e | 131 | /* |
843f5df9 | 132 | * The 'destroy <options>' first level command |
f3ed775e DG |
133 | */ |
134 | int cmd_destroy(int argc, const char **argv) | |
135 | { | |
b09ee5ba FG |
136 | int opt; |
137 | int ret = CMD_SUCCESS; | |
f3ed775e | 138 | static poptContext pc; |
b09ee5ba | 139 | char *session_name = NULL; |
f3ed775e DG |
140 | |
141 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
142 | poptReadDefaultConfig(pc, 0); | |
143 | ||
144 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
145 | switch (opt) { | |
146 | case OPT_HELP: | |
ca1c3607 | 147 | usage(stdout); |
b09ee5ba | 148 | break; |
679b4943 SM |
149 | case OPT_LIST_OPTIONS: |
150 | list_cmd_options(stdout, long_options); | |
b09ee5ba | 151 | break; |
f3ed775e DG |
152 | default: |
153 | usage(stderr); | |
154 | ret = CMD_UNDEFINED; | |
b09ee5ba | 155 | break; |
f3ed775e | 156 | } |
b09ee5ba | 157 | goto end; |
f3ed775e DG |
158 | } |
159 | ||
fd076c09 | 160 | /* Ignore session name in case all sessions are to be destroyed */ |
b09ee5ba FG |
161 | if (opt_destroy_all) { |
162 | ret = destroy_all_sessions(); | |
163 | goto end; | |
164 | } | |
fd076c09 CB |
165 | |
166 | opt_session_name = (char *) poptGetArg(pc); | |
167 | ||
168 | if (opt_session_name == NULL) { | |
169 | /* No session name specified, lookup default */ | |
170 | session_name = get_session_name(); | |
171 | if (session_name == NULL) { | |
172 | ret = CMD_ERROR; | |
b09ee5ba FG |
173 | goto end; |
174 | } | |
fd076c09 CB |
175 | } else { |
176 | session_name = opt_session_name; | |
b09ee5ba | 177 | } |
fd076c09 | 178 | |
b09ee5ba | 179 | ret = destroy_session(session_name); |
f3ed775e DG |
180 | |
181 | end: | |
fd076c09 | 182 | if (opt_session_name == NULL) { |
b09ee5ba FG |
183 | free(session_name); |
184 | } | |
fd076c09 CB |
185 | |
186 | poptFreeContext(pc); | |
f3ed775e DG |
187 | return ret; |
188 | } |