f7665623aac09e88291ad260d3bd2ef6d47ccdb5
1 /* Copyright (C) 2018 Thomas Balzer */
3 /* This file is part of tomd. */
5 /* tomd is free software: you can redistribute it and/or modify */
6 /* it under the terms of the GNU General Public License as published by */
7 /* the Free Software Foundation, either version 3 of the License, or */
8 /* (at your option) any later version. */
10 /* tomd is distributed in the hope that it will be useful, */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
13 /* GNU General Public License for more details. */
15 /* You should have received a copy of the GNU General Public License */
16 /* along with tomd. If not, see <http://www.gnu.org/licenses/>. */
21 #include <sys/socket.h>
24 #include "../../include/macros.h"
28 static void header(void)
30 tomc_p("Tom's Client, Copyright (C) 2018 Thomas Balzer");
31 tomc_p("GPL v3 or later license.");
34 static void init(void)
36 /* init socket connection */
46 struct sockaddr_un addr
;
47 addr
.sun_family
= AF_LOCAL
;
48 sprintf(addr
.sun_path
, "/run/user/1000/tomd/socket");
50 if(connect(sfd
, (struct sockaddr
*) &addr
, SUN_LEN(&addr
)) != 0){
56 static char *default_name
= "no_name";
58 char status
, stop
, kill
, start
;
62 static void extract_options(int argc
, char **argv
)
64 /* -status <name> Get status - if we thought we ran it, current real status */
65 /* -stop <name> Run provided command to gently stop */
66 /* -kill <name> Aggressive killing of process (signal 15) */
67 /* -start <name> Run name */
70 tomc_p("no args given.");
80 #define X(op) {if(strcmp("-" #op, arg) == 0){options.op = 1; continue;}}
86 /* assume last non option was the name */
90 if(options
.name
== NULL
){
91 options
.name
= default_name
;
95 static void print_options(void)
98 tomc_p(" name: %s", options
.name
);
99 tomc_p(" kill: %d", options
.kill
);
100 tomc_p("status: %d", options
.status
);
101 tomc_p(" stop: %d", options
.stop
);
102 tomc_p(" start: %d", options
.start
);
106 static char write_buf
[100];
107 static char socket_buf
[100];
109 #define SOCK_WRITE(X) { \
110 strcpy(write_buf, X); \
111 int write_len = strlen(write_buf); \
113 write(sfd, write_buf, write_len); \
114 tomc_p("wrote '%s' (%d)", write_buf, write_len); \
115 if(wrote != write_len){ \
116 perror("[tomc] write size mismatch"); \
117 exit(EXIT_FAILURE); \
121 #define SOCK_READ { \
122 int size = read(sfd, socket_buf, 100); \
124 tomc_p("didn't actually get anything."); \
125 socket_buf[0] = '\0'; \
127 socket_buf[size] = '\0'; \
129 #define SOCK_READ_X(X){ \
131 if(strcmp(socket_buf, X) != 0){ \
132 tomc_p("protocol error. instead of ACK we got '%s'", socket_buf); \
133 exit(EXIT_FAILURE); \
135 tomc_p("got an ACK"); \
137 #define SOCK_ACK {SOCK_READ_X("ACK")};
138 #define SOCK_REQUEST(X) { \
141 SOCK_WRITE(options.name); \
144 tomc_p("tomd reports '%s'", socket_buf); \
147 static void write_client(void)
149 SOCK_WRITE("client:tomc");
153 static void write_requests(void)
157 /* go through options to figure out what we want to do */
159 SOCK_REQUEST("kill");
161 if(options
.start
== 1 ||
163 tomc_p("can only kill and status at once.");
168 tomc_p("can't start and stop at once.");
170 SOCK_REQUEST("start");
173 SOCK_REQUEST("stop");
178 SOCK_REQUEST("status");
182 int main(int argc
, char **argv
)
185 extract_options(argc
, argv
);