-
Notifications
You must be signed in to change notification settings - Fork 1
/
cottage.c
219 lines (185 loc) · 4.78 KB
/
cottage.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include <ctype.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#define DEF_SOCK_PATH "/tmp/howm"
#define ENV_SOCK_VAR "HOWM_SOCK"
#define BUF_SIZE 1024
#define VERSION "0.3"
/* The errors (or lack of) that could be sent back by howm. */
enum ipc_errs { IPC_ERR_NONE, IPC_ERR_SYNTAX, IPC_ERR_ALLOC, IPC_ERR_NO_FUNC,
IPC_ERR_TOO_MANY_ARGS, IPC_ERR_TOO_FEW_ARGS, IPC_ERR_ARG_NOT_INT,
IPC_ERR_ARG_NOT_BOOL, IPC_ERR_ARG_TOO_LARGE, IPC_ERR_ARG_TOO_SMALL,
IPC_ERR_UNKNOWN_TYPE, IPC_ERR_NO_CONFIG };
enum msg_type { MSG_FUNCTION = 1, MSG_CONFIG, MSG_OP_HELPER };
static void usage(void);
static int send_command(int argc, char *argv[], int type);
static int send_op(char *argv[]);
static int setup_socket(struct sockaddr_un addr);
static void handle_error(int err);
/* Send a command to howm and wait for its reply. */
int main(int argc, char *argv[])
{
int ret, ch, type = 0;
if (argc < 2)
usage();
while ((ch = getopt(argc, argv, "vcfo")) != -1 && !type) {
switch (ch) {
case 'c':
type = MSG_CONFIG;
break;
case 'f':
type = MSG_FUNCTION;
break;
case 'o':
type = MSG_OP_HELPER;
break;
case 'v':
printf("%s\n", VERSION);
exit(EXIT_SUCCESS);
default:
usage();
}
}
if (!type || (type == MSG_OP_HELPER && argc < 5))
usage();
argc -= 2;
argv += 2;
ret = type == MSG_OP_HELPER ? send_op(argv)
: send_command(argc, argv, type);
if (ret == -1)
perror("Failed to send data");
if (ret != IPC_ERR_NONE)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
static int send_command(int argc, char *argv[], int type)
{
struct sockaddr_un addr;
int sock;
int n, len = 0, off = 0, ret, recvd;
char data[BUF_SIZE];
sock = setup_socket(addr);
n = snprintf(data, sizeof(data) - (2 * sizeof(char)), "%c%c", type, 0);
len += n;
off += n;
for (; argc > 0 && sizeof(data) - off > 0; off += n, argc--, argv++) {
n = snprintf(data + off, sizeof(data) - off, "%s%c", *argv, 0);
len += n;
}
ret = write(sock, data, len);
if (ret == -1)
perror("Failed to send command");
recvd = read(sock, &ret, sizeof(ret));
if (recvd == -1) {
perror("Failed to receive response");
ret = -1;
}
if (ret != IPC_ERR_NONE)
handle_error(ret);
if (close(sock) == -1) {
perror("Failed to close socket.\n");
exit(EXIT_FAILURE);
}
return ret;
}
/**
* Howm expects a connection, a single command and then for the
* socket to be closed. send_command opens and closes a socket,
* so howm behaves as if cottage were to be invoked 3 times.
*/
static int send_op(char *argv[])
{
int ret = 0;
char *op[] = { *argv++ };
char *count[] = { "count", *argv++ };
char *motion[] = { "motion", *argv };
/* Send op_* command */
ret = send_command(1, op, MSG_FUNCTION);
if (ret != IPC_ERR_NONE)
return ret;
/* Send count x command */
ret = send_command(2, count, MSG_FUNCTION);
if (ret != IPC_ERR_NONE)
return ret;
/* Send motion x command */
ret = send_command(2, motion, MSG_FUNCTION);
if (ret != IPC_ERR_NONE)
return ret;
return IPC_ERR_NONE;
}
static void handle_error(int ret)
{
switch (ret) {
case IPC_ERR_SYNTAX:
fprintf(stderr, "Invalid syntax.\n");
break;
case IPC_ERR_ALLOC:
fprintf(stderr, "Couldn't allocate memory to store args.\n");
break;
case IPC_ERR_NO_FUNC:
fprintf(stderr, "No such function.\n");
break;
case IPC_ERR_TOO_MANY_ARGS:
fprintf(stderr, "Too many args.\n");
break;
case IPC_ERR_TOO_FEW_ARGS:
fprintf(stderr, "Too few args.\n");
break;
case IPC_ERR_ARG_NOT_INT:
fprintf(stderr, "Argument wasn't an int\n");
break;
case IPC_ERR_ARG_NOT_BOOL:
fprintf(stderr, "Argument wasn't a bool\n");
break;
case IPC_ERR_ARG_TOO_LARGE:
fprintf(stderr, "Argument was too large\n");
break;
case IPC_ERR_ARG_TOO_SMALL:
fprintf(stderr, "Argument was too small\n");
break;
case IPC_ERR_UNKNOWN_TYPE:
fprintf(stderr, "Unknown type of message\n");
break;
case IPC_ERR_NO_CONFIG:
fprintf(stderr, "Unknown config option\n");
break;
default:
fprintf(stderr, "Unknown error code\n");
break;
}
}
static int setup_socket(struct sockaddr_un addr)
{
char *sp;
char sock_path[256];
int sock;
sp = getenv(ENV_SOCK_VAR);
if (sp != NULL)
snprintf(sock_path, sizeof(sock_path), "%s", sp);
else
snprintf(sock_path, sizeof(sock_path), "%s", DEF_SOCK_PATH);
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
snprintf(addr.sun_path, sizeof(addr.sun_path), "%s", sock_path);
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1) {
perror("Socket error");
exit(EXIT_FAILURE);
}
if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) == -1) {
perror("Connection error");
exit(EXIT_FAILURE);
}
return sock;
}
static void usage(void)
{
fprintf(stderr, "usage: cottage -f|-c|-o <args>\n");
exit(EXIT_FAILURE);
}