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"
25 #include "../../include/manifest.h"
29 static void header(void)
31 tomc_p("Tom's Client, Copyright (C) 2018 Thomas Balzer");
32 tomc_p("GPL v3 or later license.");
35 static void init(void)
37 /* init socket connection */
47 struct sockaddr_un addr
;
48 addr
.sun_family
= AF_LOCAL
;
49 sprintf(addr
.sun_path
, "/run/user/1000/tomd/socket");
51 if(connect(sfd
, (struct sockaddr
*) &addr
, SUN_LEN(&addr
)) != 0){
58 static char *default_name
= "no_name";
60 char status
, stop
, kill
, start
;
64 static void extract_options(int argc
, char **argv
)
66 /* -status <name> Get status - if we thought we ran it, current real status */
67 /* -stop <name> Run provided command to gently stop */
68 /* -kill <name> Aggressive killing of process (signal 15) */
69 /* -start <name> Run name */
72 tomc_p("no args given.");
82 #define X(op) {if(strcmp("-" #op, arg) == 0){options.op = 1; continue;}}
88 /* assume last non option was the name */
92 if(options
.name
== NULL
){
93 options
.name
= default_name
;
97 static void print_options(void)
100 tomc_p(" name: %s", options
.name
);
101 tomc_p(" kill: %d", options
.kill
);
102 tomc_p("status: %d", options
.status
);
103 tomc_p(" stop: %d", options
.stop
);
104 tomc_p(" start: %d", options
.start
);
108 static char write_buf
[100];
109 static char socket_buf
[100];
111 #define SOCK_WRITE(X) { \
112 strcpy(write_buf, X); \
113 int write_len = strlen(write_buf); \
115 write(sfd, write_buf, write_len); \
116 /* tomc_p("wrote '%s' (%d)", write_buf, write_len); */ \
117 if(wrote != write_len){ \
118 perror("[tomc] write size mismatch"); \
119 exit(EXIT_FAILURE); \
122 int last_read_size
= 0;
124 static void do_read(char *line
)
126 /* printf("got %s as input.\n", line); */
127 strcpy(socket_buf
, line
);
128 /* printf("socket_buf: %s\n", socket_buf); */
131 #define SOCK_READ { \
132 socket_read(do_read); \
134 /* int size = read(sfd, socket_buf, 100); \ */
135 /* if(size == 0) { \ */
136 /* tomc_p("didn't actually get anything."); \ */
137 /* socket_buf[0] = '\0'; \ */
139 /* tomc_p("__debug__:size=%d", size); \ */
141 /* socket_buf[size] = '\0'; \ */
142 /* tomc_p("__debug__: '%s'", socket_buf);\ */
143 /* last_read_size = size;\ */
145 #define SOCK_READ_X(X){ \
147 if(strcmp(socket_buf, X) != 0){ \
148 tomc_p("protocol error. instead of ACK we got '%s'", socket_buf); \
149 exit(EXIT_FAILURE); \
152 #define SOCK_ACK {SOCK_READ_X("ACK")};
153 #define SOCK_REQUEST(X) { \
156 SOCK_WRITE(options.name); \
160 static void write_client(void)
162 SOCK_WRITE("client:tomc");
166 static void write_requests(void)
170 /* go through options to figure out what we want to do */
172 SOCK_REQUEST("kill");
174 if(options
.start
== 1 ||
176 tomc_p("can only kill and status at once.");
181 tomc_p("can't start and stop at once.");
183 SOCK_REQUEST("start");
186 SOCK_REQUEST("stop");
191 SOCK_REQUEST("status");
195 /* printf("looking for that last ack.\n"); */
197 if(strcmp(socket_buf
, "ACK") == 0){
198 /* tomc_p("got the ending ack."); */
199 write(sfd
, "ACK", sizeof "ACK");
202 /* printf("in else.\n"); */
203 /* int len = strlen(socket_buf); */
204 /* int total = len; */
205 /* if(last_read_size == 0){ */
209 /* while(total != last_read_size -1){ */
210 /* tomc_p("__debug__:total=%d, size=%d", total, last_read_size); */
211 /* char *ptr = socket_buf + total; */
212 /* tomc_p("'%s'", ptr); */
213 /* total += strlen(ptr); */
219 int main(int argc
, char **argv
)
222 extract_options(argc
, argv
);
227 /* tomc_p("dying of our own accord."); */