This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
process.c
658 lines (555 loc) · 17.1 KB
/
process.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/**
* @file
*
* @brief Source for process plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include "process.h"
#include <kdberrors.h>
#include <kdbhelper.h>
#include <kdbinvoke.h>
#include <kdbprivate.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
typedef struct
{
pid_t childPid;
FILE * toChild;
FILE * fromChild;
Key * childContractKey;
KeySet * childContract;
ElektraInvokeHandle * dump;
struct
{
bool open;
bool get;
bool set;
bool close;
} hasOp;
} IoData;
#define MSG_HANDSHAKE_HEADER_V1 "ELEKTRA_PROCESS INIT v1\n"
#define MSG_HANDSHAKE_ACK_V1 "ELEKTRA_PROCESS ACK v1\n"
#define MSG_TERMINATION "ELEKTRA_PROCESS TERMINATE\n"
static char ** readArgs (KeySet * config, const char * arg0);
static char ** readEnv (KeySet * config);
static void freeConfigArray (char ** array);
static int executeOperation (IoData * data, const char * op, KeySet * ks, bool readKs, Key * parentKey);
static KeySet * readKeySet (IoData * data, Key * errorKey);
static void deleteData (IoData * data, Key * errorKey);
int elektraProcessOpen (Plugin * handle, Key * errorKey)
{
KeySet * config = elektraPluginGetConfig (handle);
if (ksLookupByName (config, "system:/module", 0) != NULL)
{
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
Key * appKey = ksLookupByName (config, "/executable", 0);
const char * appPath = appKey == NULL ? NULL : keyString (appKey);
if (appKey == NULL || appPath == NULL)
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (errorKey, "The /executable config key is missing");
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (appPath[0] != '/')
{
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (errorKey, "The value of the /executable config key is not an absolute path: '%s'",
appPath);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
char ** args = readArgs (config, appPath);
char ** env = readEnv (config);
Key * appConfigRoot = keyNew ("/config", KEY_END);
KeySet * appConfig = ksCut (config, appConfigRoot);
keyDel (appConfigRoot);
ksAppend (config, appConfig);
pid_t pid;
int parentToChild[2], childToParent[2];
if (pipe (parentToChild) != 0)
{
ELEKTRA_SET_RESOURCE_ERRORF (errorKey, "Could not execute app (Couldn't open pipe p2c). Reason: %s", strerror (errno));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (pipe (childToParent) != 0)
{
ELEKTRA_SET_RESOURCE_ERRORF (errorKey, "Could not execute app (Couldn't open pipe c2p). Reason: %s", strerror (errno));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
pid = fork ();
if (pid == -1)
{
ELEKTRA_SET_RESOURCE_ERRORF (errorKey, "Could not execute app '%s' (Fork failed). Reason: %s", appPath, strerror (errno));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (pid == 0)
{
// child
if (dup2 (parentToChild[0], STDIN_FILENO) == -1)
{
exit (EXIT_FAILURE);
}
close (parentToChild[0]);
close (parentToChild[1]);
if (dup2 (childToParent[1], STDOUT_FILENO) == -1)
{
exit (EXIT_FAILURE);
}
close (childToParent[0]);
close (childToParent[1]);
execve (appPath, args, env);
perror (appPath);
exit (EXIT_FAILURE);
}
// parent
freeConfigArray (args);
freeConfigArray (env);
close (parentToChild[0]);
close (childToParent[1]);
if (write (parentToChild[1], MSG_HANDSHAKE_HEADER_V1, sizeof (MSG_HANDSHAKE_HEADER_V1) - 1) != sizeof (MSG_HANDSHAKE_HEADER_V1) - 1)
{
ELEKTRA_SET_RESOURCE_ERRORF (errorKey, "Could not execute app (handshake write failed). Reason: %s", strerror (errno));
close (parentToChild[1]);
close (childToParent[0]);
kill (pid, SIGTERM);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
char handshakeAck[sizeof (MSG_HANDSHAKE_ACK_V1)];
ssize_t readBytes = read (childToParent[0], handshakeAck, sizeof (MSG_HANDSHAKE_ACK_V1) - 1);
if (readBytes != sizeof (MSG_HANDSHAKE_ACK_V1) - 1)
{
ELEKTRA_SET_RESOURCE_ERRORF (errorKey, "Could not execute app (handshake read failed). Reason: %s", strerror (errno));
close (parentToChild[1]);
close (childToParent[0]);
kill (pid, SIGTERM);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
handshakeAck[sizeof (MSG_HANDSHAKE_ACK_V1) - 1] = '\0';
if (strcmp (handshakeAck, MSG_HANDSHAKE_ACK_V1) != 0)
{
ELEKTRA_SET_RESOURCE_ERROR (errorKey, "Could not execute app (handshake ack failed). Reason: broken ack");
close (parentToChild[1]);
close (childToParent[0]);
kill (pid, SIGTERM);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
FILE * toChild = fdopen (parentToChild[1], "wb");
FILE * fromChild = fdopen (childToParent[0], "rb");
IoData * data = elektraCalloc (sizeof (IoData));
data->childPid = pid;
data->toChild = toChild;
data->fromChild = fromChild;
data->dump = elektraInvokeOpen ("dump", NULL, errorKey);
if (data->dump == NULL)
{
ELEKTRA_SET_RESOURCE_ERROR (errorKey, "Could not execute app (dump init failed).");
deleteData (data, errorKey);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
char * childName = NULL;
size_t n = 0;
readBytes = getline (&childName, &n, fromChild);
if (readBytes < 0)
{
ELEKTRA_SET_RESOURCE_ERRORF (errorKey, "Could not execute app (name read failed). Reason: %s", strerror (errno));
deleteData (data, errorKey);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
childName[readBytes - 1] = '\0';
KeySet * contract = readKeySet (data, errorKey);
if (contract == NULL)
{
ELEKTRA_SET_RESOURCE_ERROR (errorKey, "Could not execute app (contract read failed)");
deleteData (data, errorKey);
free (childName);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
Key * openKey = ksLookupByName (contract, "system:/elektra/modules/process/exports/has/open", KDB_O_POP);
if (openKey != NULL && strcmp (keyString (openKey), "1") == 0)
{
data->hasOp.open = true;
ksAppendKey (contract, keyNew ("system:/elektra/modules/process/exports/open", KEY_FUNC, elektraProcessOpen, KEY_END));
}
else
{
data->hasOp.open = false;
}
keyDel (openKey);
Key * getKey = ksLookupByName (contract, "system:/elektra/modules/process/exports/has/get", KDB_O_POP);
if (getKey != NULL && strcmp (keyString (getKey), "1") == 0)
{
data->hasOp.get = true;
ksAppendKey (contract, keyNew ("system:/elektra/modules/process/exports/get", KEY_FUNC, elektraProcessGet, KEY_END));
}
else
{
data->hasOp.get = false;
}
keyDel (getKey);
Key * setKey = ksLookupByName (contract, "system:/elektra/modules/process/exports/has/set", KDB_O_POP);
if (setKey != NULL && strcmp (keyString (setKey), "1") == 0)
{
data->hasOp.set = true;
ksAppendKey (contract, keyNew ("system:/elektra/modules/process/exports/set", KEY_FUNC, elektraProcessSet, KEY_END));
}
else
{
data->hasOp.set = false;
}
keyDel (setKey);
Key * closeKey = ksLookupByName (contract, "system:/elektra/modules/process/exports/has/close", KDB_O_POP);
if (closeKey != NULL && strcmp (keyString (closeKey), "1") == 0)
{
data->hasOp.close = true;
ksAppendKey (contract, keyNew ("system:/elektra/modules/process/exports/close", KEY_FUNC, elektraProcessClose, KEY_END));
}
else
{
data->hasOp.close = false;
}
keyDel (closeKey);
Key * oldContractRoot = keyNew ("system:/elektra/modules/process", KEY_END);
Key * newContractRoot = keyNew ("system:/elektra/modules", KEY_END);
keyAddBaseName (newContractRoot, childName);
free (childName);
data->childContractKey = newContractRoot;
data->childContract = contract;
ksRename (data->childContract, oldContractRoot, newContractRoot);
keyDel (oldContractRoot);
elektraPluginSetData (handle, data);
int result;
if (data->hasOp.open)
{
result = executeOperation (data, "open", appConfig, false, errorKey);
}
else
{
result = ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
ksDel (appConfig);
return result;
}
int elektraProcessClose (Plugin * handle, Key * errorKey)
{
IoData * data = elektraPluginGetData (handle);
if (data == NULL)
{
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
Key * eKey = errorKey == NULL ? keyNew ("/", KEY_END) : errorKey;
int result;
if (data->hasOp.close)
{
result = executeOperation (data, "close", NULL, false, eKey);
}
else
{
result = ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
bool error = false;
if (data->toChild != NULL && fputs (MSG_TERMINATION, data->toChild) == EOF)
{
ELEKTRA_SET_RESOURCE_ERRORF (eKey, "Could not terminate app (write failed). Reason: %s", strerror (errno));
error = true;
}
fflush (data->toChild);
pid_t childPid = data->childPid;
elektraPluginSetData (handle, NULL);
deleteData (data, eKey);
if (childPid != 0)
{
kill (childPid, SIGTERM);
int status;
do
{
pid_t w = waitpid (childPid, &status, 0);
if (w == -1)
{
ELEKTRA_SET_RESOURCE_ERRORF (eKey, "Could not terminate app (waitpid). Reason: %s", strerror (errno));
}
} while (!WIFEXITED (status) && !WIFSIGNALED (status));
}
if (errorKey == NULL)
{
keyDel (eKey);
}
return error ? ELEKTRA_PLUGIN_STATUS_ERROR : result;
}
int elektraProcessGet (Plugin * handle, KeySet * returned, Key * parentKey)
{
if (!elektraStrCmp (keyName (parentKey), "system:/elektra/modules/process"))
{
KeySet * contract =
ksNew (30, keyNew ("system:/elektra/modules/process", KEY_VALUE, "process plugin waits for your orders", KEY_END),
keyNew ("system:/elektra/modules/process/exports", KEY_END),
keyNew ("system:/elektra/modules/process/exports/open", KEY_FUNC, elektraProcessOpen, KEY_END),
keyNew ("system:/elektra/modules/process/exports/close", KEY_FUNC, elektraProcessClose, KEY_END),
keyNew ("system:/elektra/modules/process/exports/get", KEY_FUNC, elektraProcessGet, KEY_END),
keyNew ("system:/elektra/modules/process/exports/set", KEY_FUNC, elektraProcessSet, KEY_END),
#include ELEKTRA_README
keyNew ("system:/elektra/modules/process/infos/version", KEY_VALUE, PLUGINVERSION, KEY_END), KS_END);
ksAppend (returned, contract);
ksDel (contract);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
IoData * data = elektraPluginGetData (handle);
if (!elektraStrCmp (keyName (parentKey), keyName (data->childContractKey)))
{
ksAppend (returned, data->childContract);
return ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
if (data->hasOp.get)
{
return executeOperation (data, "get", returned, true, parentKey);
}
else
{
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
}
}
int elektraProcessSet (Plugin * handle, KeySet * returned, Key * parentKey)
{
IoData * data = elektraPluginGetData (handle);
if (data->hasOp.set)
{
return executeOperation (data, "set", returned, true, parentKey);
}
else
{
return ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
}
}
Plugin * ELEKTRA_PLUGIN_EXPORT
{
// clang-format off
return elektraPluginExport ("process",
ELEKTRA_PLUGIN_OPEN, &elektraProcessOpen,
ELEKTRA_PLUGIN_CLOSE, &elektraProcessClose,
ELEKTRA_PLUGIN_GET, &elektraProcessGet,
ELEKTRA_PLUGIN_SET, &elektraProcessSet,
ELEKTRA_PLUGIN_END);
// clang-format on
}
// private functions
static char ** readArgs (KeySet * config, const char * arg0)
{
Key * root = ksLookupByName (config, "/args", 0);
const char * lastIndex = root == NULL ? NULL : keyString (root);
if (lastIndex == NULL || strlen (lastIndex) == 0)
{
char ** array = elektraMalloc (sizeof (char *) * 2);
array[0] = strdup (arg0);
array[1] = NULL;
return array;
}
char index[ELEKTRA_MAX_ARRAY_SIZE] = "";
kdb_long_long_t count = 0;
while (strcmp (index, lastIndex) <= 0)
{
count++;
elektraWriteArrayNumber (index, count);
}
char ** array = elektraMalloc (sizeof (char *) * (count + 2));
array[0] = strdup (arg0);
array[count + 1] = NULL;
Key * lookup = keyDup (root, KEY_CP_NAME);
keySetNamespace (lookup, KEY_NS_CASCADING);
keyAddBaseName (lookup, "");
for (kdb_long_long_t i = 0; i < count; i++)
{
elektraWriteArrayNumber (index, i);
keySetBaseName (lookup, index);
Key * key = ksLookup (config, lookup, 0);
array[i + 1] = strdup (keyString (key));
}
keyDel (lookup);
return array;
}
static char ** readEnv (KeySet * config)
{
char index[ELEKTRA_MAX_ARRAY_SIZE];
Key * envRoot = ksLookupByName (config, "/env", 0);
const char * envLastIndex = envRoot == NULL ? NULL : keyString (envRoot);
kdb_long_long_t envCount = 0;
if (envLastIndex != NULL && strlen (envLastIndex) > 0)
{
index[0] = '\0';
while (strcmp (index, envLastIndex) <= 0)
{
envCount++;
elektraWriteArrayNumber (index, envCount);
}
}
Key * copyRoot = ksLookupByName (config, "/copyenv", 0);
const char * copyLastIndex = copyRoot == NULL ? NULL : keyString (copyRoot);
kdb_long_long_t copyCount = 0;
if (copyLastIndex != NULL && strlen (copyLastIndex) > 0)
{
index[0] = '\0';
while (strcmp (index, copyLastIndex) <= 0)
{
copyCount++;
elektraWriteArrayNumber (index, copyCount);
}
}
if (copyCount == 0 && envCount == 0)
{
return elektraCalloc (sizeof (char *));
}
char ** array = elektraMalloc (sizeof (char *) * (envCount + copyCount + 1));
size_t arrayPos = 0;
Key * lookup = keyDup (envRoot, KEY_CP_NAME);
keySetNamespace (lookup, KEY_NS_CASCADING);
keyAddBaseName (lookup, "");
for (kdb_long_long_t i = 0; i < envCount; i++)
{
elektraWriteArrayNumber (index, i);
keySetBaseName (lookup, index);
Key * key = ksLookup (config, lookup, 0);
array[arrayPos++] = strdup (keyString (key));
}
keyCopy (lookup, copyRoot, KEY_CP_NAME);
keySetNamespace (lookup, KEY_NS_CASCADING);
keyAddBaseName (lookup, "");
for (kdb_long_long_t i = 0; i < copyCount; i++)
{
elektraWriteArrayNumber (index, i);
keySetBaseName (lookup, index);
Key * key = ksLookup (config, lookup, 0);
const char * envValue = getenv (keyString (key));
if (envValue != NULL)
{
char * envVar = elektraFormat ("%s=%s", keyString (key), envValue);
array[arrayPos++] = strdup (envVar);
elektraFree (envVar);
}
}
array[arrayPos] = NULL;
keyDel (lookup);
return array;
}
static void freeConfigArray (char ** array)
{
for (size_t i = 0; array[i] != NULL; i++)
{
// comes from strdup so we use free directly
free (array[i]);
}
elektraFree (array);
}
static void deleteData (IoData * data, Key * errorKey)
{
// TODO: errors
data->childPid = 0;
elektraInvokeClose (data->dump, errorKey);
ksDel (data->childContract);
keyDel (data->childContractKey);
fclose (data->fromChild);
fclose (data->toChild);
elektraFree (data);
}
static int executeOperation (IoData * data, const char * op, KeySet * ks, bool readKs, Key * parentKey)
{
typedef int (*fserialize_t) (KeySet *, FILE *, Key *);
fserialize_t fserialize = *(fserialize_t *) elektraInvokeGetFunction (data->dump, "fserialize");
if (fserialize == NULL)
{
ELEKTRA_SET_INTERFACE_ERRORF (parentKey, "Could not execute '%s' (write failed). Reason: fserialize missing", op);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
if (fprintf (data->toChild, "%s\n", op) < 0)
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Could not execute operation '%s' (write failed). Reason: %s", op,
strerror (errno));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
KeySet * parentKs = ksNew (1, keyDup (parentKey, KEY_CP_ALL), KS_END);
if (fserialize (parentKs, data->toChild, parentKey) < 0)
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
ksDel (parentKs);
if (ks != NULL)
{
if (fserialize (ks, data->toChild, parentKey) < 0)
{
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
}
fflush (data->toChild);
char * result = NULL;
size_t n = 0;
ssize_t readBytes = getline (&result, &n, data->fromChild);
if (readBytes < 0)
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Could not execute operation '%s' (read failed). Reason: %s", op, strerror (errno));
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
result[readBytes - 1] = '\0';
KeySet * newParentKs = readKeySet (data, parentKey);
if (newParentKs == NULL || ksGetSize (newParentKs) != 1)
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Could not execute operation '%s'. Reason: funserialize failed", op);
free (result);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
// Note: value might be read-only, so copy value and meta separately to ensure we copy as much as possible
keyCopy (parentKey, ksAtCursor (newParentKs, 0), KEY_CP_VALUE);
keyCopy (parentKey, ksAtCursor (newParentKs, 0), KEY_CP_META);
ksDel (newParentKs);
if (ks != NULL && readKs)
{
KeySet * returned = readKeySet (data, parentKey);
if (returned == NULL)
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Could not execute operation '%s'. Reason: funserialize failed", op);
free (result);
return ELEKTRA_PLUGIN_STATUS_ERROR;
}
ksClear (ks);
ksAppend (ks, returned);
ksDel (returned);
}
int rc;
if (strcmp (result, "success") == 0)
{
rc = ELEKTRA_PLUGIN_STATUS_SUCCESS;
}
else if (strcmp (result, "noupdate") == 0)
{
rc = ELEKTRA_PLUGIN_STATUS_NO_UPDATE;
}
else if (strcmp (result, "error") == 0)
{
if (keyGetMeta (parentKey, "error") == NULL)
{
ELEKTRA_SET_INTERFACE_ERROR (parentKey, "Process returned error result without setting meta:/error on parent key.");
}
rc = ELEKTRA_PLUGIN_STATUS_ERROR;
}
else
{
ELEKTRA_SET_RESOURCE_ERRORF (parentKey, "Could not execute app (read failed). Reason: unknown result '%s'", result);
rc = ELEKTRA_PLUGIN_STATUS_ERROR;
}
free (result);
return rc;
}
static KeySet * readKeySet (IoData * data, Key * errorKey)
{
typedef int (*funserialize_t) (KeySet *, FILE *, Key *);
funserialize_t funserialize = *(funserialize_t *) elektraInvokeGetFunction (data->dump, "funserialize");
if (funserialize == NULL)
{
return NULL;
}
KeySet * ks = ksNew (0, KS_END);
if (funserialize (ks, data->fromChild, errorKey) < 0)
{
return NULL;
}
return ks;
}