-
Notifications
You must be signed in to change notification settings - Fork 189
/
installer.c
858 lines (741 loc) · 23 KB
/
installer.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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/*
SysmonForLinux
Copyright (c) Microsoft Corporation
All rights reserved.
MIT License
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.
*/
//====================================================================
//
// installer.c
//
// Functions that install Sysmon For Linux
//
//====================================================================
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <signal.h>
#include <libsysinternalsEBPF.h>
#include "sysmonevents.h"
#include "installer.h"
#include "linuxHelpers.h"
#include "sysmon_defs.h"
extern char _binary_sysmonLogView_start[];
extern char _binary_sysmonLogView_end[];
extern char _binary_sysmon_d_start[];
extern char _binary_sysmon_d_end[];
extern char _binary_sysmon_service_start[];
extern char _binary_sysmon_service_end[];
extern char _binary_sysmonEBPFkern4_15_o_start[];
extern char _binary_sysmonEBPFkern4_15_o_end[];
extern char _binary_sysmonEBPFkern4_16_o_start[];
extern char _binary_sysmonEBPFkern4_16_o_end[];
extern char _binary_sysmonEBPFkern4_17_5_1_o_start[];
extern char _binary_sysmonEBPFkern4_17_5_1_o_end[];
extern char _binary_sysmonEBPFkern5_2_o_start[];
extern char _binary_sysmonEBPFkern5_2_o_end[];
extern char _binary_sysmonEBPFkern5_3_5_5_o_start[];
extern char _binary_sysmonEBPFkern5_3_5_5_o_end[];
extern char _binary_sysmonEBPFkern5_6__o_start[];
extern char _binary_sysmonEBPFkern5_6__o_end[];
extern char _binary_sysmonEBPFkern4_15_core_o_start[];
extern char _binary_sysmonEBPFkern4_15_core_o_end[];
extern char _binary_sysmonEBPFkern4_16_core_o_start[];
extern char _binary_sysmonEBPFkern4_16_core_o_end[];
extern char _binary_sysmonEBPFkern4_17_5_1_core_o_start[];
extern char _binary_sysmonEBPFkern4_17_5_1_core_o_end[];
extern char _binary_sysmonEBPFkern5_2_core_o_start[];
extern char _binary_sysmonEBPFkern5_2_core_o_end[];
extern char _binary_sysmonEBPFkern5_3_5_5_core_o_start[];
extern char _binary_sysmonEBPFkern5_3_5_5_core_o_end[];
extern char _binary_sysmonEBPFkern5_6__core_o_start[];
extern char _binary_sysmonEBPFkern5_6__core_o_end[];
mode_t dirMode = S_IRWXU;
mode_t fileMode = S_IRUSR | S_IWUSR;
mode_t exeFileMode = S_IRWXU;
mode_t systemdFileMode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
mode_t serviceFileMode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
//--------------------------------------------------------------------
//
// installFiles
//
// Sysmon is linked with embedded versions of a number of key resource
// files. This function writes those files to the approprate places.
// The purpose is to make Sysmon a standalone installer so that it can
// be copied to another host and installed there with minimal extra
// dependencies.
//
// Also installs a systemd or initd start up scripts and sets it to
// start up by default.
//
// Returns true on success, otherwise false.
//
//--------------------------------------------------------------------
bool installFiles(bool force)
{
int fd;
void *addr = NULL;
struct stat st;
const char exePath[] = PROC_EXE_PATH;
umask(0022);
if (!createDir(SYSMON_INSTALL_DIR, dirMode)) {
fprintf(stderr, "Cannot create sysmon directory. Make sure you are root or sudo.\n");
return false;
}
fd = open(exePath, O_RDONLY);
if (fd < 0)
return false;
if (fstat(fd, &st) < 0) {
close(fd);
return false;
}
addr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
close(fd);
return false;
}
if (!dropFile(SYSMON_INSTALL_DIR "/" SYSMON_BINARY,
addr,
addr + st.st_size,
force,
exeFileMode)) {
munmap(addr, st.st_size);
close(fd);
return false;
}
munmap(addr, st.st_size);
close(fd);
if (!dropFile(SYSMON_INSTALL_DIR "/" KERN_4_15_OBJ,
_binary_sysmonEBPFkern4_15_o_start,
_binary_sysmonEBPFkern4_15_o_end,
force,
fileMode))
return false;
if (!dropFile(SYSMON_INSTALL_DIR "/" KERN_4_16_OBJ,
_binary_sysmonEBPFkern4_16_o_start,
_binary_sysmonEBPFkern4_16_o_end,
force,
fileMode))
return false;
if (!dropFile(SYSMON_INSTALL_DIR "/" KERN_4_17_5_1_OBJ,
_binary_sysmonEBPFkern4_17_5_1_o_start,
_binary_sysmonEBPFkern4_17_5_1_o_end,
force,
fileMode))
return false;
if (!dropFile(SYSMON_INSTALL_DIR "/" KERN_5_2_OBJ,
_binary_sysmonEBPFkern5_2_o_start,
_binary_sysmonEBPFkern5_2_o_end,
force,
fileMode))
return false;
if (!dropFile(SYSMON_INSTALL_DIR "/" KERN_5_3_5_5_OBJ,
_binary_sysmonEBPFkern5_3_5_5_o_start,
_binary_sysmonEBPFkern5_3_5_5_o_end,
force,
fileMode))
return false;
if (!dropFile(SYSMON_INSTALL_DIR "/" KERN_5_6__OBJ,
_binary_sysmonEBPFkern5_6__o_start,
_binary_sysmonEBPFkern5_6__o_end,
force,
fileMode))
return false;
if (!dropFile(SYSMON_INSTALL_DIR "/" KERN_4_15_CORE_OBJ,
_binary_sysmonEBPFkern4_15_core_o_start,
_binary_sysmonEBPFkern4_15_core_o_end,
force,
fileMode))
return false;
if (!dropFile(SYSMON_INSTALL_DIR "/" KERN_4_16_CORE_OBJ,
_binary_sysmonEBPFkern4_16_core_o_start,
_binary_sysmonEBPFkern4_16_core_o_end,
force,
fileMode))
return false;
if (!dropFile(SYSMON_INSTALL_DIR "/" KERN_4_17_5_1_CORE_OBJ,
_binary_sysmonEBPFkern4_17_5_1_core_o_start,
_binary_sysmonEBPFkern4_17_5_1_core_o_end,
force,
fileMode))
return false;
if (!dropFile(SYSMON_INSTALL_DIR "/" KERN_5_2_CORE_OBJ,
_binary_sysmonEBPFkern5_2_core_o_start,
_binary_sysmonEBPFkern5_2_core_o_end,
force,
fileMode))
return false;
if (!dropFile(SYSMON_INSTALL_DIR "/" KERN_5_3_5_5_CORE_OBJ,
_binary_sysmonEBPFkern5_3_5_5_core_o_start,
_binary_sysmonEBPFkern5_3_5_5_core_o_end,
force,
fileMode))
return false;
if (!dropFile(SYSMON_INSTALL_DIR "/" KERN_5_6__CORE_OBJ,
_binary_sysmonEBPFkern5_6__core_o_start,
_binary_sysmonEBPFkern5_6__core_o_end,
force,
fileMode))
return false;
if (!dropFile(SYSMON_INSTALL_DIR "/" SYSMONLOGVIEW_BINARY,
_binary_sysmonLogView_start,
_binary_sysmonLogView_end,
force,
exeFileMode))
return false;
if (dirExists(SYSTEMD_DIR)) {
//
// systemd managed system
//
if (!dropFile(SYSTEMD_DIR "/" SYSTEMD_SERVICE,
_binary_sysmon_service_start,
_binary_sysmon_service_end,
force,
systemdFileMode))
return false;
if(system(SYSTEMD_RELOAD_CMD) == -1) {
return false;
}
if(system(SYSTEMD_ENABLE_CMD " " SYSTEMD_SERVICE) == -1) {
return false;
}
} else if (dirExists(INITD_DIR)) {
//
// init.d / rc script managed system
//
if (!dropFile(INITD_DIR "/" INITD_SERVICE,
_binary_sysmon_d_start,
_binary_sysmon_d_end,
force,
serviceFileMode))
return false;
//
// create symbolic links for the different run states
// Sysmon not running in 0 (shutdown), 1-2 (single-user)
// and 6 (reboot); but will run in 3-4 (multi-user) and 5 (desktop),
// aligned with usual Linux conventions.
//
setRunState(0, false);
setRunState(1, false);
setRunState(2, false);
setRunState(3, true);
setRunState(4, true);
setRunState(5, true);
setRunState(6, false);
}
return true;
}
//--------------------------------------------------------------------
//
// uninstall
//
// Disables the systemd or initd service.
//
//--------------------------------------------------------------------
void uninstall()
{
if (dirExists(SYSTEMD_DIR)) {
if (system(SYSTEMD_DISABLE_CMD " " SYSTEMD_SERVICE) == -1) {
return;
}
} else if (dirExists(INITD_DIR)) {
setRunState(0, false);
setRunState(1, false);
setRunState(2, false);
setRunState(3, false);
setRunState(4, false);
setRunState(5, false);
setRunState(6, false);
}
}
//--------------------------------------------------------------------
//
// copyConfigFile
//
// Copies the supplied config file to the Sysmon install directory.
//
// Returns true on success, otherwise false.
//
//--------------------------------------------------------------------
bool copyConfigFile(const char *configFile)
{
if (configFile == NULL) {
fprintf(stderr, "copyConfigFile invalid params\n");
return false;
}
int fd;
void *addr = NULL;
struct stat st;
bool ret = true;
char realConfigPath[PATH_MAX];
char realTargetPath[PATH_MAX];
if (!fileExists(configFile))
return false;
if (realpath(configFile, realConfigPath) == NULL)
return false;
snprintf(realTargetPath, sizeof(realTargetPath), "%s", SYSMON_CONFIG_FILE);
if (strcmp(realConfigPath, realTargetPath) == 0)
return true;
fd = open(configFile, O_RDONLY);
if (fd < 0)
return false;
if (fstat(fd, &st) < 0) {
close(fd);
return false;
}
addr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (addr == MAP_FAILED) {
close(fd);
return false;
}
if (!dropFile(realTargetPath,
addr,
addr + st.st_size,
true,
fileMode)) {
ret = false;
}
munmap(addr, st.st_size);
close(fd);
return ret;
}
//--------------------------------------------------------------------
//
// createEmptyConfigFile
//
// For situations where Sysmon was configured without a configuration
// file, this function creates an empty one to represent that fact
// (and because the systemd and initd start up scripts expect a config
// file).
//
// Returns true on success, otherwise false.
//
//--------------------------------------------------------------------
bool createEmptyConfigFile()
{
bool ret = false;
int fd = 0;
char empty[] = "<Sysmon schemaversion=\"4.22\">\n<EventFiltering>\n</EventFiltering>\n</Sysmon>\n";
fd = creat(SYSMON_CONFIG_FILE, fileMode);
if (fd < 0)
return false;
if (write(fd, empty, strlen(empty) + 1) != -1) {
ret = true;
}
close(fd);
return ret;
}
//--------------------------------------------------------------------
//
// writeArgv
//
// Writes the argc and argv of the commandline to files in the Sysmon
// install directory for later use.
//
// Returns true on success, otherwise false.
//
//--------------------------------------------------------------------
bool writeArgv(int argc, char *argv[])
{
if (argv == NULL) {
fprintf(stderr, "writeArgv invalid params\n");
return false;
}
int fd;
unlink(SYSMON_ARGC_FILE);
fd = creat(SYSMON_ARGC_FILE, fileMode);
if (fd < 0)
return false;
if (write(fd, &argc, sizeof(argc)) == -1) {
close(fd);
return false;
}
close(fd);
unlink(SYSMON_ARGV_FILE);
fd = creat(SYSMON_ARGV_FILE, fileMode);
if (fd < 0)
return false;
for (int i=0; i<argc; i++) {
if (write(fd, argv[i], strlen(argv[i]) + 1) == -1) {
close(fd);
return false;
}
}
close(fd);
return true;
}
//--------------------------------------------------------------------
//
// readArgv
//
// Reads the stored argc and argv. Switches the config file in the
// command line (if it exists) with the path to the one in the Sysmon
// install directory, and returns the one in the command line as
// configFile.
//
// Returns true on success, otherwise false.
//
//--------------------------------------------------------------------
bool readArgv(int *argc, char ***argv, char **configFile)
{
if (argc == NULL || argv == NULL || configFile == NULL) {
fprintf(stderr, "readArgv invalid params\n");
return false;
}
int fd;
struct stat st;
char *data;
unsigned int ptrTableSize = 0;
char *specialConfigFile = NULL;
bool sawConfigFileSwitch = false;
*configFile = NULL;
fd = open(SYSMON_ARGC_FILE, O_RDONLY);
if (fd < 0)
return false;
if (read(fd, argc, sizeof(*argc)) == -1) {
close(fd);
return false;
}
close(fd);
ptrTableSize = sizeof(char *) * (*argc+1);
fd = open(SYSMON_ARGV_FILE, O_RDONLY);
if (fd < 0)
return false;
if (fstat(fd, &st) < 0) {
close(fd);
return false;
}
// allocate space for the string pointers followed by the strings, plus
// space for special config file
*argv = (char **)malloc(ptrTableSize + st.st_size + strlen(SYSMON_CONFIG_FILE) + 1);
if (*argv == NULL) {
close(fd);
return false;
}
data = (char *)(*argv) + ptrTableSize;
// read actual strings
if (read(fd, data, st.st_size) == -1) {
close(fd);
return false;
}
close(fd);
// write special config file to end
specialConfigFile = data + st.st_size;
strcpy(specialConfigFile, SYSMON_CONFIG_FILE);
// make pointers
for (int i=0; i<*argc; i++) {
// if previous arg was '-i' or '-c' and this arg doesn't start with '-'
// then point to the special config file instead and return the actual
// config file in configFile
if (sawConfigFileSwitch && data[0] != '-') {
(*argv)[i] = specialConfigFile;
*configFile = data;
} else {
(*argv)[i] = data;
}
data += strlen(data) + 1;
if (strcasecmp((*argv)[i], "-i") == 0 || strcasecmp((*argv)[i], "-c") == 0) {
sawConfigFileSwitch = true;
} else {
sawConfigFileSwitch = false;
}
}
// add the null pointer to the end
(*argv)[*argc] = NULL;
return true;
}
//--------------------------------------------------------------------
//
// GetCommandLine
//
// Obtains the command line from the files in the Sysmon install dir
// and returns a pointer to a newly malloced buffer containing it.
//
// Returns a new pointer on success, otherwise NULL.
//
//--------------------------------------------------------------------
char *GetCommandLine()
{
static char *cmdline = NULL;
int argc = 0;
char **argv = NULL;
char *configFile = NULL;
unsigned int i;
unsigned int totalSize = 0;
char *ptr = NULL;
if (!readArgv(&argc, &argv, &configFile)) {
return cmdline;
}
if (argc <= 0) {
free(argv);
return cmdline;
}
for (i=0; i<argc; i++) {
totalSize += strlen(argv[i]) + 1;
}
if (totalSize == 0) {
free(argv);
return cmdline;
}
if (cmdline != NULL) {
free(cmdline);
}
cmdline = (char *)malloc(totalSize);
if (cmdline == NULL) {
free(argv);
return NULL;
}
ptr = cmdline;
for (i=0; i<argc; i++) {
strcpy(ptr, argv[i]);
ptr += strlen(argv[i]);
*ptr = ' ';
ptr++;
}
// change last space to a null
ptr--;
*ptr = 0x00;
free(argv);
return cmdline;
}
//--------------------------------------------------------------------
//
// writeFieldSizes
//
// Writes the FieldSizes argument in the configuration file to a file
// in the Sysmon install directory for later use.
//
// Returns true on success, otherwise false.
//
//--------------------------------------------------------------------
bool writeFieldSizes(char *fieldSizesStr)
{
bool ret = false;
unlink(SYSMON_FIELDSIZES_FILE);
if (fieldSizesStr == NULL) {
// if string is NULL, just erase the file
return true;
}
int fd;
fd = creat(SYSMON_FIELDSIZES_FILE, fileMode);
if (fd < 0)
return false;
if (write(fd, fieldSizesStr, strlen(fieldSizesStr) + 1) != -1) {
ret = true;
}
close(fd);
return ret;
}
//--------------------------------------------------------------------
//
// readFieldSizes
//
// Reads the stored FieldSizes argument.
//
// Returns allocated buffer containing FieldSizes on success, NULL
// otherwise.
//
//--------------------------------------------------------------------
char *readFieldSizes()
{
int fd;
struct stat st;
char *data;
fd = open(SYSMON_FIELDSIZES_FILE, O_RDONLY);
if (fd < 0)
return NULL;
if (fstat(fd, &st) < 0) {
close(fd);
return NULL;
}
// allocate space for the string
data = (char *)malloc(st.st_size);
if (data == NULL) {
close(fd);
return NULL;
}
if (read(fd, data, st.st_size) == -1) {
close(fd);
free(data);
return NULL;
}
close(fd);
return data;
}
//--------------------------------------------------------------------
//
// setRunState
//
// Sets the initd runState (0-6) to running/not-running according to
// the running bool.
//
// Returns true on success, otherwise false.
//
//--------------------------------------------------------------------
bool setRunState(unsigned int runState, bool running)
{
char startLink[PATH_MAX];
char killLink[PATH_MAX];
if (runState > 6)
return false;
snprintf(startLink, sizeof(startLink), INITD_DIR_FMT "/" INITD_START_ID INITD_SERVICE, runState);
snprintf(killLink, sizeof(killLink), INITD_DIR_FMT "/" INITD_KILL_ID INITD_SERVICE, runState);
if (running) {
unlink(killLink);
if (symlink(INITD_DIR "/" INITD_SERVICE, startLink) < 0) {
return false;
}
} else {
unlink(startLink);
if (symlink(INITD_DIR "/" INITD_SERVICE, killLink) < 0) {
return false;
}
}
return true;
}
//--------------------------------------------------------------------
//
// stopSysmonService
//
// Stops the systemd or initd Sysmon service.
//
// Returns true on success, otherwise false.
//
//--------------------------------------------------------------------
bool stopSysmonService()
{
if (fileExists(SYSTEMD_DIR "/" SYSTEMD_SERVICE)) {
if (system(SYSTEMD_STOP_CMD " " SYSTEMD_SERVICE) == -1) {
return false;
}
return true;
} else if (fileExists(INITD_DIR "/" INITD_SERVICE)) {
if (system(INITD_DIR "/" INITD_SERVICE " stop") == -1) {
return false;
}
return true;
}
return false;
}
//--------------------------------------------------------------------
//
// startSysmonService
//
// Starts the systemd or initd Sysmon service. If the running Sysmon
// was already started as a service, it does nothing and returns true.
//
// Returns true on success, otherwise false.
//
//--------------------------------------------------------------------
bool startSysmonService()
{
if (OPT_SET(Service))
return true;
if (fileExists(SYSTEMD_DIR "/" SYSTEMD_SERVICE)) {
execl("/bin/sh", "sh", "-c", SYSTEMD_START_CMD " " SYSTEMD_SERVICE, NULL);
} else if (fileExists(INITD_DIR "/" INITD_SERVICE)) {
execl("/bin/sh", "sh", "-c", INITD_DIR "/" INITD_SERVICE " start", NULL);
}
return false;
}
//--------------------------------------------------------------------
//
// sysmonSearch
//
// Searches for other Sysmon processes and optionally kills them if
// the supplied signal is >=0.
//
// For signal < 0 (e.g. search), returns true if Sysmon is found
// running.
// For signal >= 0 (e.g. kill), returns true if at least one Sysmon
// was found running and was sent the signal.
//
//--------------------------------------------------------------------
bool sysmonSearch(int signal)
{
pid_t pid = getpid();
char exe[PATH_MAX];
char path[2 * PATH_MAX]; // artifically long to
// handle edge-cases
const char *name;
DIR *d;
struct dirent *dir;
int path_len;
bool killed = false;
d = opendir("/proc");
if (d) {
while ((dir = readdir(d)) != NULL) {
if (!StrIsNum(dir->d_name) || pid == atoi(dir->d_name))
continue;
snprintf(exe, PATH_MAX, PROC_EXE_PATH_FMT, atoi(dir->d_name));
path_len = readlink(exe, path, PATH_MAX);
if (path_len <= 0)
continue;
path[path_len] = 0x00;
name = strrchr(path, '/');
if (name == NULL)
continue;
if (strcmp(name+1, SYSMON_BINARY) != 0 && strcmp(name+1, SYSMON_BINARY " (deleted)") != 0)
continue;
if (signal >= 0) {
kill(atoi(dir->d_name), signal);
killed = true;
} else {
return true;
}
}
closedir(d);
}
if (signal >= 0 && killed)
return true;
return false;
}
//--------------------------------------------------------------------
//
// killOtherSysmon
//
// Sends a SIGTERM to all other Sysmon processes. If force is true,
// also sends SIGKILL to any remaining Sysmon processes.
//
//--------------------------------------------------------------------
void killOtherSysmon(bool force)
{
sysmonSearch(SIGTERM);
if (force) {
sysmonSearch(SIGKILL);
}
}
//--------------------------------------------------------------------
//
// sysmonIsRunning
//
// Checks if Sysmon is running and returns true if at least one is,
// and false otherwise.
//
//--------------------------------------------------------------------
bool sysmonIsRunning()
{
return sysmonSearch(-1);
}
//--------------------------------------------------------------------
//
// signalConfigChange
//
// Sends a SIGHUP to the running Sysmon process to indicate that it
// should read the new config stored in the install directory.
//
//--------------------------------------------------------------------
void signalConfigChange()
{
sysmonSearch(SIGHUP);
}