forked from subogero/omxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
171 lines (163 loc) · 3.6 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
/* (c) SZABO Gergely <[email protected]>, license GNU GPL v2 */
#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 "omxd.h"
static char *is_url(char *file);
static mode_t get_ftype(char *file);
static int cmd_foreach_in(char *cmd);
static int writecmd(char *cmd);
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(OMX_CMDS LIST_CMDS, *cmd) == NULL)
return 11;
if (file == NULL)
return writecmd(cmd);
char line[LINE_LENGTH];
line[0] = *cmd;
line[1] = ' ';
line[2] = 0;
/* 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:
return writecmd(line);
default:
printfd(2, "Wrong file type %d: %s\n", type, line + 2);
return 12;
}
}
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;
}
static 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) {
if (i < 2)
goto free_entry;
char line[LINE_LENGTH];
strcpy(line, cmd);
char *entry = entries[i]->d_name;
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 writecmd(char *cmd)
{
strcat(cmd, "\n");
const char *const filters[] = {
"jpg\n","JPG\n","m3u\n","txt\n","nfo\n","sfv\n","log\n",NULL
};
int i;
for (i = 0; filters[i] != NULL; ++i) {
if (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);
/*
* Give up the CPU to the omxd daemon to read the above line from FIFO.
* The clean solution to this race condition would be to teach the
* daemon to read multiple lines from the FIFO after a single open().
*/
usleep(10000);
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);
}