Change lttng API to use a lttng_handle
[lttng-tools.git] / 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
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <popt.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #include "../cmd.h"
29 #include "../conf.h"
30 #include "../utils.h"
31
32 static char *opt_session_name;
33 static struct lttng_handle *handle;
34
35 enum {
36 OPT_HELP = 1,
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 {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, "\n");
57 }
58
59 /*
60 * destroy_session
61 *
62 * Destroy a session removing the config directory and unregistering to the
63 * session daemon.
64 */
65 static int destroy_session()
66 {
67 int ret;
68 char *session_name, *path;
69
70 if (opt_session_name == NULL) {
71 session_name = get_session_name();
72 if (session_name == NULL) {
73 ret = CMD_ERROR;
74 goto error;
75 }
76 } else {
77 session_name = opt_session_name;
78 }
79
80 handle = lttng_create_handle(session_name, NULL);
81 if (handle == NULL) {
82 ret = -1;
83 goto error;
84 }
85
86 ret = lttng_destroy_session(handle);
87 if (ret < 0) {
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 lttng_destroy_handle(handle);
112
113 return ret;
114 }
115
116 /*
117 * cmd_destroy
118 *
119 * The 'destroy <options>' first level command
120 */
121 int cmd_destroy(int argc, const char **argv)
122 {
123 int opt, ret = CMD_SUCCESS;
124 static poptContext pc;
125
126 pc = poptGetContext(NULL, argc, argv, long_options, 0);
127 poptReadDefaultConfig(pc, 0);
128
129 while ((opt = poptGetNextOpt(pc)) != -1) {
130 switch (opt) {
131 case OPT_HELP:
132 usage(stderr);
133 goto end;
134 default:
135 usage(stderr);
136 ret = CMD_UNDEFINED;
137 goto end;
138 }
139 }
140
141 opt_session_name = (char*) poptGetArg(pc);
142
143 ret = destroy_session();
144
145 end:
146 return ret;
147 }
This page took 0.033053 seconds and 5 git commands to generate.