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