Mi: mi backend + mi for command version
[lttng-tools.git] / src / bin / lttng / commands / set_session.c
CommitLineData
3087ef18
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
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.
3087ef18
DG
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 *
d14d33bf
AM
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.
3087ef18
DG
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
c399183f 27#include "../command.h"
3087ef18
DG
28
29static char *opt_session_name;
30
31enum {
32 OPT_HELP = 1,
679b4943 33 OPT_LIST_OPTIONS,
3087ef18
DG
34};
35
36static struct poptOption long_options[] = {
37 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
38 {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0},
679b4943 39 {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL},
3087ef18
DG
40 {0, 0, 0, 0, 0, 0, 0}
41};
42
43/*
44 * usage
45 */
46static void usage(FILE *ofp)
47{
7c96a096 48 fprintf(ofp, "usage: lttng set-session NAME [OPTIONS]\n");
3087ef18
DG
49 fprintf(ofp, "\n");
50 fprintf(ofp, "Options:\n");
51 fprintf(ofp, " -h, --help Show this help\n");
679b4943 52 fprintf(ofp, " --list-options Simple listing of options\n");
3087ef18
DG
53 fprintf(ofp, "\n");
54}
55
56/*
57 * set_session
58 */
59static int set_session(void)
60{
61 int ret = CMD_SUCCESS;
3087ef18 62
487b253b
DG
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
fffd0547 70 ret = config_init(opt_session_name);
3087ef18 71 if (ret < 0) {
fffd0547 72 ERR("Unable to set session name");
3087ef18
DG
73 ret = CMD_ERROR;
74 goto error;
75 }
76
77 MSG("Session set to %s", opt_session_name);
78 ret = CMD_SUCCESS;
79
80error:
81 return ret;
82}
83
84/*
85 * cmd_set_session
86 */
87int 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
c7e35b03
JR
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
3087ef18
DG
102 while ((opt = poptGetNextOpt(pc)) != -1) {
103 switch (opt) {
104 case OPT_HELP:
ca1c3607 105 usage(stdout);
3087ef18 106 goto end;
679b4943
SM
107 case OPT_LIST_OPTIONS:
108 list_cmd_options(stdout, long_options);
679b4943 109 goto end;
3087ef18
DG
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);
ca1c3607 121 ret = CMD_ERROR;
3087ef18
DG
122 goto end;
123 }
124
125 ret = set_session();
126
127end:
ca1c3607 128 poptFreeContext(pc);
3087ef18
DG
129 return ret;
130}
This page took 0.049429 seconds and 5 git commands to generate.