2 * Copyright (c) 2015-2016 Red Hat, Inc.
6 * Author: Jan Friesse (jfriesse@redhat.com)
8 * This software licensed under BSD license, the text of which follows:
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * - Neither the name of the Red Hat, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
42 #include "qdevice-config.h"
45 #include "dynar-str.h"
47 #include "unix-socket.h"
49 #define IPC_READ_BUF_SIZE 512
51 enum qdevice_tool_operation
{
52 QDEVICE_TOOL_OPERATION_NONE
,
53 QDEVICE_TOOL_OPERATION_SHUTDOWN
,
54 QDEVICE_TOOL_OPERATION_STATUS
,
57 enum qdevice_tool_exit_code
{
58 QDEVICE_TOOL_EXIT_CODE_NO_ERROR
= 0,
59 QDEVICE_TOOL_EXIT_CODE_USAGE
= 1,
60 QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR
= 2,
61 QDEVICE_TOOL_EXIT_CODE_SOCKET_CONNECT
= 3,
62 QDEVICE_TOOL_EXIT_CODE_QDEVICE_RETURNED_ERROR
= 4,
69 printf("usage: %s [-Hhsv] [-p qdevice_ipc_socket_path]\n",
70 QDEVICE_TOOL_PROGRAM_NAME
);
74 cli_parse(int argc
, char * const argv
[], enum qdevice_tool_operation
*operation
,
75 int *verbose
, char **socket_path
)
79 *operation
= QDEVICE_TOOL_OPERATION_NONE
;
81 *socket_path
= strdup(QDEVICE_DEFAULT_LOCAL_SOCKET_FILE
);
83 if (*socket_path
== NULL
) {
84 errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR
,
85 "Can't alloc memory for socket path string");
88 while ((ch
= getopt(argc
, argv
, "Hhsvp:")) != -1) {
91 *operation
= QDEVICE_TOOL_OPERATION_SHUTDOWN
;
94 *operation
= QDEVICE_TOOL_OPERATION_STATUS
;
101 *socket_path
= strdup(optarg
);
102 if (*socket_path
== NULL
) {
103 errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR
,
104 "Can't alloc memory for socket path string");
110 exit(QDEVICE_TOOL_EXIT_CODE_USAGE
);
115 if (*operation
== QDEVICE_TOOL_OPERATION_NONE
) {
117 exit(QDEVICE_TOOL_EXIT_CODE_USAGE
);
122 store_command(struct dynar
*str
, enum qdevice_tool_operation operation
, int verbose
)
124 const char *nline
= "\n\0";
125 const int nline_len
= 2;
128 case QDEVICE_TOOL_OPERATION_NONE
:
129 errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR
, "Unhandled operation none");
131 case QDEVICE_TOOL_OPERATION_SHUTDOWN
:
132 if (dynar_str_cat(str
, "shutdown ") != 0) {
136 case QDEVICE_TOOL_OPERATION_STATUS
:
137 if (dynar_str_cat(str
, "status ") != 0) {
144 if (dynar_str_cat(str
, "verbose ") != 0) {
149 if (dynar_cat(str
, nline
, nline_len
) != 0) {
157 * -1 - Internal error (can't alloc memory)
159 * 1 - IPC returned error
160 * 2 - Unknown status line
163 read_ipc_reply(FILE *f
)
165 struct dynar read_str
;
169 static const char *ok_str
= "OK";
170 static const char *err_str
= "Error";
174 dynar_init(&read_str
, IPC_READ_BUF_SIZE
);
180 while ((ch
= fgetc(f
)) != EOF
) {
182 putc(ch
, (err_set
? stderr
: stdout
));
185 } else if (ch
== '\n') {
189 if (dynar_cat(&read_str
, &c
, sizeof(c
)) != 0) {
194 if (strcasecmp(dynar_data(&read_str
), ok_str
) == 0) {
195 } else if (strcasecmp(dynar_data(&read_str
), err_str
) == 0) {
198 fprintf(stderr
, "Error: ");
205 if (dynar_cat(&read_str
, &c
, sizeof(c
)) != 0) {
214 dynar_destroy(&read_str
);
220 main(int argc
, char * const argv
[])
222 enum qdevice_tool_operation operation
;
227 struct dynar send_str
;
231 exit_code
= QDEVICE_TOOL_EXIT_CODE_NO_ERROR
;
233 cli_parse(argc
, argv
, &operation
, &verbose
, &socket_path
);
235 dynar_init(&send_str
, QDEVICE_DEFAULT_IPC_MAX_RECEIVE_SIZE
);
237 sock_fd
= unix_socket_client_create(socket_path
, 0);
239 err(QDEVICE_TOOL_EXIT_CODE_SOCKET_CONNECT
,
240 "Can't connect to QDevice socket (is QDevice running?)");
243 sock
= fdopen(sock_fd
, "w+t");
245 err(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR
, "Can't open QDevice socket fd");
248 if (store_command(&send_str
, operation
, verbose
) != 0) {
249 errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR
, "Can't store command");
252 res
= fprintf(sock
, "%s", dynar_data(&send_str
));
253 if (res
< 0 || (size_t)res
!= strlen(dynar_data(&send_str
)) ||
255 errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR
, "Can't send command");
258 res
= read_ipc_reply(sock
);
261 errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR
, "Internal error during IPC status line read");
266 exit_code
= QDEVICE_TOOL_EXIT_CODE_QDEVICE_RETURNED_ERROR
;
269 errx(QDEVICE_TOOL_EXIT_CODE_SOCKET_CONNECT
, "Unknown status line returned by IPC server");
273 if (fclose(sock
) != 0) {
274 warn("Can't close QDevice socket");
278 dynar_destroy(&send_str
);