54320df2a42a0988a1255eddbf19252ea8f36d7d
[lttng-tools.git] / src / bin / lttng / commands / set_session.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 static char *opt_session_name;
30
31 enum {
32 OPT_HELP = 1,
33 OPT_LIST_OPTIONS,
34 };
35
36 static struct poptOption long_options[] = {
37 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
38 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
39 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
40 {0, 0, 0, 0, 0, 0, 0}
41 };
42
43 /*
44 * usage
45 */
46 static void usage(FILE *ofp)
47 {
48 fprintf(ofp, "usage: lttng set-session NAME [OPTIONS]\n");
49 fprintf(ofp, "\n");
50 fprintf(ofp, "Options:\n");
51 fprintf(ofp, " -h, --help Show this help\n");
52 fprintf(ofp, " --list-options Simple listing of options\n");
53 fprintf(ofp, "\n");
54 }
55
56 /*
57 * set_session
58 */
59 static int set_session(void)
60 {
61 int ret = CMD_SUCCESS;
62
63 if (opt_session_name && strlen(opt_session_name) > NAME_MAX) {
64 ERR("Session name too long. Length must be lower or equal to %d",
65 NAME_MAX);
66 ret = CMD_ERROR;
67 goto error;
68 }
69
70 ret = config_init(opt_session_name);
71 if (ret < 0) {
72 ERR("Unable to set session name");
73 ret = CMD_ERROR;
74 goto error;
75 }
76
77 MSG("Session set to %s", opt_session_name);
78 ret = CMD_SUCCESS;
79
80 error:
81 return ret;
82 }
83
84 /*
85 * cmd_set_session
86 */
87 int cmd_set_session(int argc, const char **argv)
88 {
89 int opt, ret = CMD_SUCCESS;
90 static poptContext pc;
91
92 pc = poptGetContext(NULL, argc, argv, long_options, 0);
93 poptReadDefaultConfig(pc, 0);
94
95 /* TODO: mi support */
96 if (lttng_opt_mi) {
97 ret = -LTTNG_ERR_MI_NOT_IMPLEMENTED;
98 ERR("mi option not supported");
99 goto end;
100 }
101
102 while ((opt = poptGetNextOpt(pc)) != -1) {
103 switch (opt) {
104 case OPT_HELP:
105 usage(stdout);
106 goto end;
107 case OPT_LIST_OPTIONS:
108 list_cmd_options(stdout, long_options);
109 goto end;
110 default:
111 usage(stderr);
112 ret = CMD_UNDEFINED;
113 goto end;
114 }
115 }
116
117 opt_session_name = (char *) poptGetArg(pc);
118 if (opt_session_name == NULL) {
119 ERR("Missing session name");
120 usage(stderr);
121 ret = CMD_ERROR;
122 goto end;
123 }
124
125 ret = set_session();
126
127 end:
128 poptFreeContext(pc);
129 return ret;
130 }
This page took 0.044869 seconds and 3 git commands to generate.