Update version to 2.0-pre17
[lttng-tools.git] / lttng / commands / destroy.c
CommitLineData
f3ed775e
DG
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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
f3ed775e
DG
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
6e2d116c
DG
28#include "../cmd.h"
29#include "../conf.h"
30#include "../utils.h"
f3ed775e
DG
31
32static char *opt_session_name;
33
34enum {
35 OPT_HELP = 1,
36};
37
38static struct poptOption long_options[] = {
39 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
40 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
41 {0, 0, 0, 0, 0, 0, 0}
42};
43
44/*
45 * usage
46 */
47static void usage(FILE *ofp)
48{
49 fprintf(ofp, "usage: lttng destroy [options] [NAME]\n");
50 fprintf(ofp, "\n");
51 fprintf(ofp, "Where NAME is an optional session name. If not specified, lttng will\n");
52 fprintf(ofp, "get it from the configuration directory (.lttng).\n");
53 fprintf(ofp, "\n");
54 fprintf(ofp, " -h, --help Show this help\n");
55 fprintf(ofp, "\n");
56}
57
58/*
843f5df9
DG
59 * Destroy a session removing the config directory and unregistering to the
60 * session daemon.
f3ed775e
DG
61 */
62static int destroy_session()
63{
64 int ret;
65 char *session_name, *path;
66
67 if (opt_session_name == NULL) {
68 session_name = get_session_name();
69 if (session_name == NULL) {
70 ret = CMD_ERROR;
71 goto error;
72 }
73 } else {
74 session_name = opt_session_name;
75 }
76
843f5df9 77 ret = lttng_destroy_session(session_name);
f3ed775e
DG
78 if (ret < 0) {
79 goto free_name;
80 }
81
58a97671 82 path = config_get_default_path();
f3ed775e
DG
83 if (path == NULL) {
84 ret = CMD_FATAL;
85 goto free_name;
86 }
87
88 if (opt_session_name == NULL) {
89 config_destroy(path);
90 MSG("Session %s destroyed at %s", session_name, path);
91 } else {
92 MSG("Session %s destroyed", session_name);
93 }
94
f3ed775e
DG
95 ret = CMD_SUCCESS;
96
97free_name:
b73d0b29
MD
98 if (opt_session_name == NULL) {
99 free(session_name);
100 }
f3ed775e
DG
101error:
102 return ret;
103}
104
105/*
843f5df9 106 * The 'destroy <options>' first level command
f3ed775e
DG
107 */
108int cmd_destroy(int argc, const char **argv)
109{
110 int opt, ret = CMD_SUCCESS;
111 static poptContext pc;
112
113 pc = poptGetContext(NULL, argc, argv, long_options, 0);
114 poptReadDefaultConfig(pc, 0);
115
116 while ((opt = poptGetNextOpt(pc)) != -1) {
117 switch (opt) {
118 case OPT_HELP:
119 usage(stderr);
120 goto end;
121 default:
122 usage(stderr);
123 ret = CMD_UNDEFINED;
124 goto end;
125 }
126 }
127
128 opt_session_name = (char*) poptGetArg(pc);
129
130 ret = destroy_session();
131
132end:
133 return ret;
134}
This page took 0.03345 seconds and 5 git commands to generate.