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