Commit | Line | Data |
---|---|---|
eb9cb8b7 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> |
eb9cb8b7 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
eb9cb8b7 | 5 | * |
eb9cb8b7 DG |
6 | */ |
7 | ||
6c1c0768 | 8 | #define _LGPL_SOURCE |
eb9cb8b7 DG |
9 | #include <popt.h> |
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include <sys/stat.h> | |
14 | #include <sys/types.h> | |
15 | #include <unistd.h> | |
16 | ||
c7e35b03 JR |
17 | #include <common/mi-lttng.h> |
18 | ||
c399183f | 19 | #include "../command.h" |
979d476d | 20 | #include "version.h" |
eb9cb8b7 | 21 | |
4fc83d94 PP |
22 | #ifdef LTTNG_EMBED_HELP |
23 | static const char help_msg[] = | |
24 | #include <lttng-version.1.h> | |
25 | ; | |
26 | #endif | |
27 | ||
eb9cb8b7 DG |
28 | enum { |
29 | OPT_HELP = 1, | |
679b4943 | 30 | OPT_LIST_OPTIONS, |
eb9cb8b7 DG |
31 | }; |
32 | ||
c7e35b03 JR |
33 | static const char *lttng_license = "lttng is free software and under the GPL license and part LGPL"; |
34 | ||
eb9cb8b7 DG |
35 | static struct poptOption long_options[] = { |
36 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ | |
37 | {"help", 'h', POPT_ARG_NONE, 0, OPT_HELP, 0, 0}, | |
679b4943 | 38 | {"list-options", 0, POPT_ARG_NONE, NULL, OPT_LIST_OPTIONS, NULL, NULL}, |
eb9cb8b7 DG |
39 | {0, 0, 0, 0, 0, 0, 0} |
40 | }; | |
41 | ||
c7e35b03 JR |
42 | /* |
43 | * create_version | |
44 | */ | |
45 | static void create_version(struct mi_lttng_version *version) | |
46 | { | |
47 | strncpy(version->version, VERSION, NAME_MAX); | |
48 | version->version_major = VERSION_MAJOR; | |
49 | version->version_minor = VERSION_MINOR; | |
50 | version->version_patchlevel = VERSION_PATCHLEVEL; | |
50bba0f3 | 51 | strncpy(version->version_commit, GIT_VERSION, NAME_MAX); |
c7e35b03 JR |
52 | strncpy(version->version_name, VERSION_NAME, NAME_MAX); |
53 | strncpy(version->package_url, PACKAGE_URL, NAME_MAX); | |
54 | } | |
55 | ||
56 | /* | |
57 | * Print the machine interface output of this command. | |
58 | */ | |
a3ecaea8 | 59 | static int print_mi(void) |
c7e35b03 JR |
60 | { |
61 | int ret = CMD_SUCCESS; | |
62 | struct mi_writer *writer = NULL; | |
63 | struct mi_lttng_version version; | |
64 | ||
65 | create_version(&version); | |
66 | ||
67 | writer = mi_lttng_writer_create(fileno(stdout), lttng_opt_mi); | |
68 | if (!writer) { | |
69 | ret = -LTTNG_ERR_NOMEM; | |
70 | goto end; | |
71 | } | |
72 | ||
73 | /* Open the command element */ | |
74 | ret = mi_lttng_writer_command_open(writer, | |
75 | mi_lttng_element_command_version); | |
76 | if (ret) { | |
77 | ret = CMD_ERROR; | |
78 | goto error; | |
79 | } | |
80 | ||
81 | /* Beginning of output */ | |
82 | ret = mi_lttng_writer_open_element(writer, | |
83 | mi_lttng_element_command_output); | |
84 | if (ret) { | |
85 | ret = CMD_ERROR; | |
86 | goto error; | |
87 | } | |
88 | ||
89 | /* Print the machine interface of version */ | |
90 | ret = mi_lttng_version(writer, &version, | |
91 | VERSION_DESCRIPTION, lttng_license); | |
92 | if (ret) { | |
93 | ret = CMD_ERROR; | |
94 | goto error; | |
95 | } | |
96 | ||
97 | /* Close the output element */ | |
98 | ret = mi_lttng_writer_close_element(writer); | |
99 | if (ret) { | |
100 | ret = CMD_ERROR; | |
101 | goto error; | |
102 | } | |
103 | ||
104 | /* Close the command */ | |
105 | ret = mi_lttng_writer_command_close(writer); | |
106 | if (ret) { | |
107 | ret = CMD_ERROR; | |
108 | } | |
109 | ||
110 | error: | |
111 | /* Cleanup */ | |
112 | if (writer && mi_lttng_writer_destroy(writer)) { | |
113 | /* Preserve original error code */ | |
114 | ret = ret ? ret : -LTTNG_ERR_MI_IO_FAIL; | |
115 | } | |
116 | ||
117 | end: | |
118 | return ret; | |
119 | } | |
120 | ||
eb9cb8b7 DG |
121 | /* |
122 | * cmd_version | |
123 | */ | |
124 | int cmd_version(int argc, const char **argv) | |
125 | { | |
126 | int opt, ret = CMD_SUCCESS; | |
127 | static poptContext pc; | |
128 | ||
129 | pc = poptGetContext(NULL, argc, argv, long_options, 0); | |
130 | poptReadDefaultConfig(pc, 0); | |
131 | ||
132 | while ((opt = poptGetNextOpt(pc)) != -1) { | |
133 | switch (opt) { | |
134 | case OPT_HELP: | |
4ba92f18 | 135 | SHOW_HELP(); |
eb9cb8b7 | 136 | goto end; |
679b4943 SM |
137 | case OPT_LIST_OPTIONS: |
138 | list_cmd_options(stdout, long_options); | |
679b4943 | 139 | goto end; |
eb9cb8b7 | 140 | default: |
eb9cb8b7 DG |
141 | ret = CMD_UNDEFINED; |
142 | goto end; | |
143 | } | |
144 | } | |
145 | ||
c7e35b03 JR |
146 | if (lttng_opt_mi) { |
147 | ret = print_mi(); | |
148 | } else { | |
4c6ac053 MD |
149 | MSG("lttng version " VERSION " - " VERSION_NAME "%s", |
150 | GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION); | |
c7e35b03 | 151 | MSG("\n" VERSION_DESCRIPTION "\n"); |
a18d9544 | 152 | MSG("Web site: https://lttng.org"); |
c7e35b03 | 153 | MSG("\n%s", lttng_license); |
a3bc3918 JR |
154 | if (EXTRA_VERSION_NAME[0] != '\0') { |
155 | MSG("\nExtra version name: " EXTRA_VERSION_NAME); | |
156 | } | |
157 | if (EXTRA_VERSION_DESCRIPTION[0] != '\0') { | |
158 | MSG("\nExtra version description:\n\t" EXTRA_VERSION_DESCRIPTION); | |
159 | } | |
7f5ed73a JR |
160 | if (EXTRA_VERSION_PATCHES[0] != '\0') { |
161 | MSG("\nExtra version patches:\n\t" EXTRA_VERSION_PATCHES); | |
162 | } | |
c7e35b03 | 163 | } |
eb9cb8b7 DG |
164 | |
165 | end: | |
ca1c3607 | 166 | poptFreeContext(pc); |
eb9cb8b7 DG |
167 | return ret; |
168 | } |