Fix: lttng UI exit value and error message
[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
33 enum {
34 OPT_HELP = 1,
35 OPT_LIST_OPTIONS,
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},
41 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
42 {0, 0, 0, 0, 0, 0, 0}
43 };
44
45 /*
46 * usage
47 */
48 static void usage(FILE *ofp)
49 {
50 fprintf(ofp, "usage: lttng destroy [options] [NAME]\n");
51 fprintf(ofp, "\n");
52 fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n");
53 fprintf(ofp, "get it from the configuration directory (.lttng).\n");
54 fprintf(ofp, "\n");
55 fprintf(ofp, " -h, --help Show this help\n");
56 fprintf(ofp, " --list-options Simple listing of options\n");
57 fprintf(ofp, "\n");
58 }
59
60 /*
61 * Destroy a session removing the config directory and unregistering to the
62 * session daemon.
63 */
64 static int destroy_session()
65 {
66 int ret;
67 char *session_name, *path;
68
69 if (opt_session_name == NULL) {
70 session_name = get_session_name();
71 if (session_name == NULL) {
72 ret = CMD_ERROR;
73 goto error;
74 }
75 } else {
76 session_name = opt_session_name;
77 }
78
79 ret = lttng_destroy_session(session_name);
80 if (ret < 0) {
81 switch (-ret) {
82 case LTTCOMM_SESS_NOT_FOUND:
83 WARN("Session name %s not found", session_name);
84 break;
85 default:
86 break;
87 }
88 goto free_name;
89 }
90
91 path = config_get_default_path();
92 if (path == NULL) {
93 ret = CMD_FATAL;
94 goto free_name;
95 }
96
97 if (opt_session_name == NULL) {
98 config_destroy(path);
99 MSG("Session %s destroyed at %s", session_name, path);
100 } else {
101 MSG("Session %s destroyed", session_name);
102 }
103
104 ret = CMD_SUCCESS;
105
106 free_name:
107 if (opt_session_name == NULL) {
108 free(session_name);
109 }
110 error:
111 return ret;
112 }
113
114 /*
115 * The 'destroy <options>' first level command
116 */
117 int cmd_destroy(int argc, const char **argv)
118 {
119 int opt, ret = CMD_SUCCESS;
120 static poptContext pc;
121
122 pc = poptGetContext(NULL, argc, argv, long_options, 0);
123 poptReadDefaultConfig(pc, 0);
124
125 while ((opt = poptGetNextOpt(pc)) != -1) {
126 switch (opt) {
127 case OPT_HELP:
128 usage(stdout);
129 goto end;
130 case OPT_LIST_OPTIONS:
131 list_cmd_options(stdout, long_options);
132 goto end;
133 default:
134 usage(stderr);
135 ret = CMD_UNDEFINED;
136 goto end;
137 }
138 }
139
140 opt_session_name = (char*) poptGetArg(pc);
141
142 ret = destroy_session();
143
144 end:
145 poptFreeContext(pc);
146 return ret;
147 }
This page took 0.031735 seconds and 4 git commands to generate.