-
Notifications
You must be signed in to change notification settings - Fork 19
/
client.c
319 lines (306 loc) · 7 KB
/
client.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* (c) SZABO Gergely <[email protected]>, license GNU GPL v2 */
#define _FILE_OFFSET_BITS 64
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <signal.h>
#include <stdio.h>
#include "omxd.h"
static int client_cmd(char *cmd, char *file);
static void print_list(char *playing);
static char *is_url(char *file);
static int cmd_foreach_in(char *cmd);
static int writecmd(char *cmd);
static int writeopts(char *cmd, int argc, char *argv[]);
char file_opening[32];
static int open_tout(char *file);
static void open_tout_handler(int signal);
int client(int argc, char *argv[])
{
char *cmd = argv[1];
char *file = argc >= 3 ? argv[2] : NULL;
signal(SIGALRM, open_tout_handler);
/* Check command */
if (strchr(CLIENT_CMDS, *cmd) != NULL)
return client_cmd(cmd, file);
if (strchr(OMX_CMDS LIST_CMDS STOP_CMDS OPT_CMDS, *cmd) == NULL)
return 11;
/* Pass omxplayer options */
if (strchr(OPT_CMDS, *cmd)) {
return writeopts(cmd, argc, argv);
}
if (file == NULL)
return writecmd(cmd);
char line[LINE_LENGTH];
line[0] = *cmd;
line[1] = ' ';
line[2] = 0;
/* Direct jump/delete */
if (strchr("gx", *cmd)) {
strcat(line, file);
return writecmd(line);
}
/* URL? */
char *url = is_url(file);
if (url != NULL) {
strcat(line, url);
return writecmd(line);
}
/* Relative path? */
if (file[0] != '/') {
getcwd(line + 2, LINE_LENGTH);
strcat(line, "/");
}
strcat(line, file);
mode_t type = get_ftype(line + 2);
switch (type) {
case S_IFDIR:
return cmd_foreach_in(line);
case S_IFREG:
case S_IFIFO:
return writecmd(line);
default:
printfd(2, "Wrong file type %d: %s\n", type, line + 2);
return 12;
}
}
static int client_cmd(char *cmd, char *file)
{
if (*cmd != 'S')
return 15;
char st[LINE_LENGTH] = { 0, };
char playing[LINE_LENGTH] = { 0, };
int t_play, t_len;
int pid = 0;
int res = parse_status(st, playing, &t_play, &t_len, &pid);
if (res)
return res;
/* Print */
printfd(1, "%s %d/%d %s\n", st, t_play, t_len, playing);
if (file != NULL && strncmp(file, "all", 4) == 0)
print_list(playing);
return 0;
}
int parse_status(char *st, char *playing, int *t_play, int *t_len, int *pid)
{
/* Open status log file */
FILE *logfile = fopen("omxstat", "r");
if (logfile == NULL) {
logfile = fopen("/var/log/omxstat", "r");
if (logfile == NULL)
return 15;
}
/* Vars for line and fields */
char line[LINE_LENGTH];
int t_last;
int t = time(NULL);
if (fgets(line, LINE_LENGTH, logfile) == NULL) {
fclose(logfile);
return 15;
}
/* Parse line: timestamp state dt omxplayer.logfile file */
sscand(strtok(line, " \n"), &t_last);
char *tok = strtok(NULL, " \n");
if (tok != NULL)
strcpy(st, tok);
sscand(strtok(NULL, " \n"), t_play);
char *omxp_log = strtok(NULL, " \n");
if (omxp_log != NULL)
sscand(omxp_log + 23, pid); /* Keep PID from logfile name */
tok = strtok(NULL, "\n");
if (tok != NULL)
strcpy(playing, tok);
/* Special case: Stopped */
if (strncmp(st, "Stopped", 8) == 0) {
*t_play = 0;
*t_len = 0;
playing[0] = 0; /* Empty string */
} else {
/* Extract track length from omxplayer log file */
*t_len = player_length(omxp_log);
/* Add dt since last command to t_play if not paused */
if (strncmp(st, "Paused", 8) != 0)
*t_play += t - t_last;
}
fclose(logfile);
return 0;
}
/* Get track length from omxplayer logfile */
int player_length(char *omxp_log)
{
if (omxp_log == NULL)
return 0;
FILE *log = fopen(omxp_log, "r");
if (log == NULL)
return 0;
int t = 0;
char line[LINE_LENGTH];
while (fgets(line, LINE_LENGTH, log)) {
if (strncmp(strtok(line, " "), "Duration:", 9) != 0)
continue;
int unit;
char *hours = strtok(NULL, ":");
if (strlen(hours) != 2 ||
!strchr("0123456789", hours[0]) ||
!strchr("0123456789", hours[1])) {
t = -1;
break;
}
sscand(hours, &unit);
t += 3600 * unit;
sscand(strtok(NULL, ":"), &unit);
t += 60 * unit;
sscand(strtok(NULL, "., "), &unit);
t += unit;
break;
}
fclose(log);
return t;
}
static void print_list(char *playing)
{
FILE *play = fopen(LIST_FILE, "r");
if (play == NULL) {
int I_root = 1;
play = fopen(LIST_FILE, "r");
if (play == NULL)
return;
}
char line[LINE_LENGTH];
while (fgets(line, LINE_LENGTH, play)) {
if (playing != NULL && *playing != 0 &&
strstr(line, playing) == line)
printf("> ");
printf(line);
}
fclose(play);
}
/* Other helpers */
static char *is_url(char *file)
{
char *url_sep = strstr(file, "://");
if (url_sep == NULL)
return NULL;
if (strncmp(file, "rtmpt", 5) == 0) {
memcpy(file + 1, file, 4);
return file + 1;
}
return file;
}
mode_t get_ftype(char *file)
{
struct stat filestat;
if (stat(file, &filestat) == -1) {
printfd(2, "Could not stat %s\n", file);
return 0;
}
return filestat.st_mode & S_IFMT;
}
static int cmd_foreach_in(char *cmd)
{
if (strchr("IHJ", *cmd)) {
writestr(2, "Temporary injection (IHJ) for files only\n");
return 13;
}
if (cmd[strlen(cmd) - 1] != '/')
strcat(cmd, "/");
struct dirent **entries;
int n = scandir(cmd + 2, &entries, NULL, alphasort);
if (n < 0) {
printfd(2, "Unable to scan dir %s\n", cmd + 2);
return 14;
}
int i;
for (i = 0; i < n; ++i) {
char *entry = entries[i]->d_name;
if (*entry == '.')
goto free_entry;
char line[LINE_LENGTH];
strcpy(line, cmd);
strcat(line, entry);
switch (get_ftype(line + 2)) {
case S_IFDIR:
cmd_foreach_in(line);
break;
case S_IFREG:
writecmd(line);
if (*cmd == 'i')
*cmd = 'a';
break;
default:
break;
}
free_entry:
free(entries[i]);
}
free(entries);
return 0;
}
static int writeopts(char *cmd, int argc, char *argv[])
{
char cmds[LINE_LENGTH];
*cmds = 0;
int i;
/* cmd O alone: send to FIFO straight away */
if (argc <= 2) {
strcat(cmds, cmd);
strcat(cmds, "\n");
}
/* cmd O plus options: send O opt LF for each option */
for (i = 2; i < argc; ++i) {
strcat(cmds, cmd);
strcat(cmds, " ");
strcat(cmds, argv[i]);
strcat(cmds, "\n");
}
strcat(cmds, ".");
return writecmd(cmds);
}
static int writecmd(char *cmd)
{
if (cmd[strlen(cmd)-1] != '\n')
strcat(cmd, "\n");
const char *const filters[] = {
"jpg\n","JPG\n","jpeg\n","JPEG\n","m3u\n","txt\n","nfo\n","sfv\n","log\n",NULL
};
int i;
for (i = 0; filters[i] != NULL; ++i) {
if (*cmd != 'O' && strstr(cmd, filters[i]) != NULL)
return 0;
}
int cmdfd = open_tout("omxctl");
if (cmdfd < 0) {
cmdfd = open_tout("/var/run/omxctl");
if (cmdfd < 0) {
writestr(2, "Can't open omxctl or /var/run/omxctl\n");
return 10;
}
}
writestr(2, cmd);
writestr(cmdfd, cmd);
close(cmdfd);
return 0;
}
static int open_tout(char *file)
{
strcpy(file_opening, file);
alarm(1);
int fd = open(file, O_WRONLY);
alarm(0);
file_opening[0] = 0;
return fd;
}
static void open_tout_handler(int signal)
{
printfd(2,
"Opening FIFO '%s' timed out.\n"
"The omxd daemon is dead or listening on another one.\n"
"An unprivileged omxd listens on omxctl in its current dir.\n"
"One started as root listens on '/var/run/omxctl'.\n",
file_opening);
_exit(10);
}