Fix error handling
[lttng-tools.git] / lttng / commands / create.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 <time.h>
27#include <unistd.h>
28
6e2d116c
DG
29#include "../cmd.h"
30#include "../conf.h"
31#include "../utils.h"
f3ed775e
DG
32
33static char *opt_output_path;
34static char *opt_session_name;
35
36enum {
37 OPT_HELP = 1,
38};
39
40static struct poptOption long_options[] = {
41 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
42 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
43 {"output", 'o', POPT_ARG_STRING, &opt_output_path, 0, 0, 0},
44 {0, 0, 0, 0, 0, 0, 0}
45};
46
47/*
48 * usage
49 */
50static void usage(FILE *ofp)
51{
52 fprintf(ofp, "usage: lttng create [options] [NAME]\n");
53 fprintf(ofp, "\n");
54 fprintf(ofp, " -h, --help Show this help\n");
58a97671 55 fprintf(ofp, " -o, --output PATH Specify output path for traces\n");
f3ed775e
DG
56 fprintf(ofp, "\n");
57}
58
59/*
60 * create_session
61 *
62 * Create a tracing session. If no name specified, a default name will be
63 * generated.
64 */
65static int create_session()
66{
31e43e7f 67 int ret, have_name = 0;
f3ed775e 68 char name[NAME_MAX];
58a97671 69 char *session_name, *traces_path = NULL, *alloc_path = NULL;
f3ed775e
DG
70 time_t rawtime;
71 struct tm *timeinfo;
72
73 /* Auto session name creation */
74 if (opt_session_name == NULL) {
75 time(&rawtime);
76 timeinfo = localtime(&rawtime);
77 strftime(name, sizeof(name), "auto-%Y%m%d-%H%M%S", timeinfo);
78 session_name = name;
79 DBG("Auto session name set to %s", session_name);
80 } else {
81 session_name = opt_session_name;
31e43e7f 82 have_name = 1;
f3ed775e
DG
83 }
84
85 /* Auto output path */
86 if (opt_output_path == NULL) {
58a97671 87 alloc_path = strdup(config_get_default_path());
f3ed775e 88 if (alloc_path == NULL) {
b082db07
DG
89 ERR("Home path not found.\n \
90 Please specify an output path using -o, --output PATH");
f3ed775e
DG
91 ret = CMD_FATAL;
92 goto error;
93 }
f3ed775e 94
58a97671
DG
95 ret = asprintf(&traces_path, "%s/" LTTNG_DEFAULT_TRACE_DIR_NAME, alloc_path);
96 if (ret < 0) {
97 perror("asprintf trace dir name");
98 goto error;
99 }
100 } else {
101 traces_path = opt_output_path;
f3ed775e
DG
102 }
103
58a97671 104 ret = lttng_create_session(session_name, traces_path);
f3ed775e
DG
105 if (ret < 0) {
106 goto error;
107 }
108
58a97671
DG
109 /* Init lttng session config */
110 ret = config_init(session_name);
f3ed775e 111 if (ret < 0) {
e0ac0dd5
DG
112 if (ret == -1) {
113 ret = CMD_ERROR;
114 }
f3ed775e
DG
115 goto error;
116 }
117
118 MSG("Session %s created.", session_name);
31e43e7f
DG
119 if (have_name) {
120 MSG("Traces will be written in %s/%s-<date>-<time>" , traces_path, session_name);
121 } else {
122 MSG("Traces will be written in %s/%s", traces_path, session_name);
123 }
f3ed775e
DG
124
125 ret = CMD_SUCCESS;
126
127error:
128 if (alloc_path) {
129 free(alloc_path);
130 }
131
58a97671
DG
132 if (traces_path) {
133 free(traces_path);
f3ed775e
DG
134 }
135 return ret;
136}
137
138/*
139 * cmd_list
140 *
141 * The 'list <options>' first level command
142 */
143int cmd_create(int argc, const char **argv)
144{
145 int opt, ret = CMD_SUCCESS;
146 static poptContext pc;
147
148 pc = poptGetContext(NULL, argc, argv, long_options, 0);
149 poptReadDefaultConfig(pc, 0);
150
151 while ((opt = poptGetNextOpt(pc)) != -1) {
152 switch (opt) {
153 case OPT_HELP:
154 usage(stderr);
155 goto end;
156 default:
157 usage(stderr);
158 ret = CMD_UNDEFINED;
159 goto end;
160 }
161 }
162
163 opt_session_name = (char*) poptGetArg(pc);
164
165 ret = create_session();
166
167end:
168 return ret;
169}
This page took 0.029171 seconds and 4 git commands to generate.