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
|
/**
* Copyright © 2015 Mattias Andrée <maandree@member.fsf.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "daemon.h"
/**
* Quote a string, in shell (Bash-only if necessary) compatible
* format, if necessary. Here, just adding quotes around all not
* do. The string must be single line, and there must not be
* any invisible characters; it should be possible to copy
* a string from the terminal by marking it, hence all of this
* ugliness.
*
* @param str The string.
* @return Return a safe representation of the string,
* `NULL` on error.
*/
static char *
quote(const char *str)
{
#define UNSAFE(c) strchr(" \"$()[]{};|&^#!?*~`<>", c)
size_t in = 0; /* < ' ' */
size_t sn = 0; /* in UNSAFE */
size_t bn = 0; /* = '\\' */
size_t qn = 0; /* = '\'' */
size_t rn = 0; /* other */
size_t n, i = 0;
const unsigned char *s;
char *rc;
for (s = (const unsigned char *)str; *s; s++) {
if (*s < ' ') in++;
else if (UNSAFE(*s)) sn++;
else if (*s == '\\') bn++;
else if (*s == '\'') qn++;
else rn++;
}
switch (in ? 2 : (sn + bn + qn) ? 1 : 0) {
case 0:
return strdup(str);
case 1:
n = rn + sn + bn + 4 * qn + 4 * in + 2;
t (!(rc = malloc((n + 1) * sizeof(char))));
rc[i++] = '\'';
for (s = (const unsigned char *)str; *s; s++) {
rc[i++] = (char)*s;
if (*s == '\'')
rc[i++] = '\\', rc[i++] = '\'', rc[i++] = '\'';
}
rc[i++] = '\'';
rc[i] = '\0';
return rc;
case 2:
n = 4 * in + rn + sn + 2 * bn + 2 * qn + 3;
t (!(rc = malloc((n + 1) * sizeof(char))));
rc[i++] = '$';
rc[i++] = '\'';
for (s = (const unsigned char *)str; *s; s++) {
if (*s < ' ') {
rc[i++] = '\\';
rc[i++] = 'x';
rc[i++] = "0123456789ABCDEF"[(*s >> 4) & 15];
rc[i++] = "0123456789ABCDEF"[(*s >> 0) & 15];
}
else if (*s == '\\') rc[i++] = '\\', rc[i++] = (char)*s;
else if (*s == '\'') rc[i++] = '\\', rc[i++] = (char)*s;
else rc[i++] = (char)*s;
}
rc[i++] = '\'';
rc[i] = '\0';
return rc;
}
fail:
return NULL;
}
/**
* Create a textual representation of the a duration.
*
* @param buffer Output buffer, with a size of at least
* 10 `char`:s plus enough to encode a `time_t`.
* @param s The duration in seconds.
*/
static void
strduration(char *buffer, time_t s)
{
char *buf = buffer;
int seconds, minutes, hours;
seconds = s % 60, s /= 60;
minutes = s % 60, s /= 60;
hours = s % 24, s /= 24;
if (s) {
buf += sprintf(buf, "%llid", (long long int)s);
buf += sprintf(buf, "%02i:", hours);
buf += sprintf(buf, "%02i", minutes);
} else if (hours) {
buf += sprintf(buf, "%i:", hours);
buf += sprintf(buf, "%02i", minutes);
} else if (minutes) {
buf += sprintf(buf, "%i", minutes);
}
sprintf(buf, "%02i", seconds);
}
/**
* Dump a job to the socket.
*
* @param job The job.
* @return 0 on success, -1 on error.
*/
static int
send_job_human(struct job *job)
{
struct tm *tm;
struct timespec rem;
const char *clk;
char rem_s[3 * sizeof(time_t) + sizeof("d00:00:00")];
char *qstr = NULL;
char line[sizeof("job: %zu clock: unrecognised argc: %i remaining: , argv[0]: ")
+ 3 * sizeof(size_t) + 3 * sizeof(int) + sizeof(rem_s) + 9];
char timestr_a[sizeof("0000-00-00 00:00:00") + 3 * sizeof(time_t)];
char timestr_b[10];
char **args = NULL;
char **arg;
char **argv = NULL;
char **envp = NULL;
size_t argsn;
int rc = 0;
int saved_errno;
/* Get remaining time. */
if (clock_gettime(job->clk, &rem))
return errno == EINVAL ? 0 : -1;
rem.tv_sec -= job->ts.tv_sec;
rem.tv_nsec -= job->ts.tv_nsec;
if (rem.tv_nsec < 0) {
rem.tv_sec -= 1;
rem.tv_nsec += 1000000000L;
}
if (rem.tv_sec < 0)
/* This job will be removed momentarily, do not list it. (To simply things.) */
return 0;
/* Get clock name. */
switch (job->clk) {
case CLOCK_REALTIME: clk = "walltime"; break;
case CLOCK_BOOTTIME: clk = "boottime"; break;
default: clk = "unrecognised"; break;
}
/* Get textual representation of the remaining time. (Seconds only.) */
strduration(rem_s, rem.tv_sec);
/* Get textual representation of the expiration time. */
switch (job->clk) {
case CLOCK_REALTIME:
t (!(tm = localtime(&(job->ts.tv_sec))));
strftime(timestr_a, sizeof(timestr_a), "%Y-%m-%d %H:%M:%S", tm);
break;
default:
strduration(timestr_a, job->ts.tv_sec);
break;
}
sprintf(timestr_b, "%09li", job->ts.tv_nsec);
/* Get arguments. */
t (!(args = restore_array(job->payload, job->n, &argsn)));
t (!(argv = sublist(args, (size_t)(job->argc))));
t (!(envp = sublist(args + job->argc, argsn - (size_t)(job->argc))));
/* Send message. */
t (!(qstr = quote(args[0])));
sprintf(line, "job: %zu clock: %s argc: %i remaining: %s.%09li, argv[0]: ",
job->no, clk, job->argc, rem_s, rem.tv_nsec);
t (send_string(SOCK_FILENO, STDOUT_FILENO,
line, qstr, "\n",
" time: ", timestr_a, ".", timestr_b, "\n",
" argv:",
NULL));
for (arg = argv; *arg; arg++) {
free(qstr);
t (!(qstr = quote(*arg)));
t (send_string(SOCK_FILENO, STDOUT_FILENO, " ", qstr, NULL));
}
free(qstr), qstr = NULL;
t (send_string(SOCK_FILENO, STDOUT_FILENO, "\n envp:", NULL));
for (arg = envp; *arg; arg++) {
t (!(qstr = quote(*arg)));
t (send_string(SOCK_FILENO, STDOUT_FILENO, " ", qstr, NULL));
free(qstr);
}
qstr = NULL;
t (send_string(SOCK_FILENO, STDOUT_FILENO, "\n\n", NULL));
done:
saved_errno = errno;
free(qstr);
free(args);
free(argv);
free(envp);
errno = saved_errno;
return rc;
fail:
rc = -1;
goto done;
}
/**
* Subroutine to the sat daemon: list jobs.
*
* @param argc Should be 3.
* @param argv The name of the process, the pathname of the socket,
* and the pathname to the state file.
* @return 0 The process was successful.
* @return 1 The process failed queuing the job.
*/
int
main(int argc, char *argv[])
{
size_t n = 0;
char *message = NULL;
struct job **jobs;
struct job **job;
DAEMON_PROLOGUE;
/* Receive and validate message. */
t (readall(SOCK_FILENO, &message, &n) || n);
shutdown(SOCK_FILENO, SHUT_RD);
/* Perform action. */
t (!(jobs = get_jobs()));
for (job = jobs; *job; job++)
t (send_job_human(*job));
DAEMON_CLEANUP_START;
for (job = jobs; *job; job++)
free(*job);
free(jobs);
free(message);
DAEMON_CLEANUP_END;
}
|