-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathswitch_core.c
3605 lines (3037 loc) · 108 KB
/
switch_core.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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2005-2014, Anthony Minessale II <[email protected]>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is
* Anthony Minessale II <[email protected]>
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Anthony Minessale II <[email protected]>
* Michael Jerris <[email protected]>
* Paul D. Tinsley <pdt at jackhammer.org>
* Marcel Barbulescu <[email protected]>
* Joseph Sullivan <[email protected]>
* Seven Du <[email protected]>
* Andrey Volk <[email protected]>
*
* switch_core.c -- Main Core Library
*
*/
#include <switch.h>
#include <switch_ssl.h>
#include <switch_stun.h>
#include <switch_nat.h>
#include "private/switch_apr_pvt.h"
#include "private/switch_core_pvt.h"
#include <switch_curl.h>
#include <switch_msrp.h>
#ifndef WIN32
#include <switch_private.h>
#ifdef HAVE_SETRLIMIT
#include <sys/resource.h>
#endif
#endif
#include <errno.h>
#include <sqlite3.h>
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>
#endif
#ifdef SOLARIS_PRIVILEGES
#include <priv.h>
#endif
#ifdef __linux__
#include <sys/wait.h>
#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* Required for POSIX_SPAWN_USEVFORK */
#endif
#include <spawn.h>
#include <poll.h>
#endif
#ifdef WIN32
#define popen _popen
#define pclose _pclose
#endif
#ifdef HAVE_SYSTEMD
#include <systemd/sd-daemon.h>
#endif
SWITCH_DECLARE_DATA switch_directories SWITCH_GLOBAL_dirs = { 0 };
SWITCH_DECLARE_DATA switch_filenames SWITCH_GLOBAL_filenames = { 0 };
/* The main runtime obj we keep this hidden for ourselves */
struct switch_runtime runtime = { 0 };
static void switch_load_core_config(const char *file);
static void send_heartbeat(void)
{
switch_event_t *event;
switch_core_time_duration_t duration;
switch_core_measure_time(switch_core_uptime(), &duration);
if (switch_event_create(&event, SWITCH_EVENT_HEARTBEAT) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Ready");
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Up-Time",
"%u year%s, "
"%u day%s, "
"%u hour%s, "
"%u minute%s, "
"%u second%s, "
"%u millisecond%s, "
"%u microsecond%s",
duration.yr, duration.yr == 1 ? "" : "s",
duration.day, duration.day == 1 ? "" : "s",
duration.hr, duration.hr == 1 ? "" : "s",
duration.min, duration.min == 1 ? "" : "s",
duration.sec, duration.sec == 1 ? "" : "s",
duration.ms, duration.ms == 1 ? "" : "s", duration.mms, duration.mms == 1 ? "" : "s");
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "FreeSWITCH-Version", "%s", switch_version_full());
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Uptime-msec", "%"SWITCH_TIME_T_FMT, switch_core_uptime() / 1000);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Count", "%u", switch_core_session_count());
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Max-Sessions", "%u", switch_core_session_limit(0));
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Per-Sec", "%u", runtime.sps);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Per-Sec-Last", "%u", runtime.sps_last);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Per-Sec-Max", "%u", runtime.sps_peak);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Per-Sec-FiveMin", "%u", runtime.sps_peak_fivemin);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Since-Startup", "%" SWITCH_SIZE_T_FMT, switch_core_session_id() - 1);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Peak-Max", "%u", runtime.sessions_peak);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Session-Peak-FiveMin", "%u", runtime.sessions_peak_fivemin);
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Idle-CPU", "%f", switch_core_idle_cpu());
switch_event_fire(&event);
}
}
static char main_ip4[256] = "";
static char main_ip6[256] = "";
static void check_ip(void)
{
char guess_ip4[256] = "";
char guess_ip6[256] = "";
char old_ip4[256] = "";
char old_ip6[256] = "";
int ok4 = 1, ok6 = 1;
int mask = 0;
switch_status_t check6, check4;
switch_event_t *event;
char *hostname = switch_core_get_variable("hostname");
gethostname(runtime.hostname, sizeof(runtime.hostname));
if (zstr(hostname)) {
switch_core_set_variable("hostname", runtime.hostname);
} else if (strcmp(hostname, runtime.hostname)) {
if (switch_event_create(&event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "condition", "hostname-change");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "old-hostname", hostname);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "new-hostname", runtime.hostname);
switch_event_fire(&event);
}
switch_core_set_variable("hostname", runtime.hostname);
}
check4 = switch_find_local_ip(guess_ip4, sizeof(guess_ip4), &mask, AF_INET);
check6 = switch_find_local_ip(guess_ip6, sizeof(guess_ip6), NULL, AF_INET6);
if (check6 != SWITCH_STATUS_SUCCESS && (zstr(main_ip6) || !strcasecmp(main_ip6, "::1"))) {
check6 = SWITCH_STATUS_SUCCESS;
}
if (check4 != SWITCH_STATUS_SUCCESS) {
ok4 = 2;
} else if (!*main_ip4) {
switch_set_string(main_ip4, guess_ip4);
} else {
if (!(ok4 = !strcmp(main_ip4, guess_ip4))) {
struct in_addr in;
in.s_addr = mask;
switch_set_string(old_ip4, main_ip4);
switch_set_string(main_ip4, guess_ip4);
switch_core_set_variable("local_ip_v4", guess_ip4);
switch_core_set_variable("local_mask_v4", inet_ntoa(in));
}
}
if (check6 != SWITCH_STATUS_SUCCESS) {
ok6 = 2;
} else if (!*main_ip6) {
switch_set_string(main_ip6, guess_ip6);
} else {
if (!(ok6 = !strcmp(main_ip6, guess_ip6))) {
switch_set_string(old_ip6, main_ip6);
switch_set_string(main_ip6, guess_ip6);
switch_core_set_variable("local_ip_v6", guess_ip6);
}
}
if (!ok4 || !ok6) {
if (switch_event_create(&event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "condition", "network-address-change");
if (!ok4) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-previous-v4", old_ip4);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-change-v4", main_ip4);
}
if (!ok6) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-previous-v6", old_ip6);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-change-v6", main_ip6);
}
switch_event_fire(&event);
}
}
if (ok4 == 2 || ok6 == 2) {
if (switch_event_create(&event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header(event, SWITCH_STACK_BOTTOM, "condition", "network-outage");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-status-v4", ok4 == 2 ? "disconnected" : "active");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-v4", main_ip4);
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-status-v6", ok6 == 2 ? "disconnected" : "active");
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "network-address-v6", main_ip6);
switch_event_fire(&event);
}
}
}
SWITCH_STANDARD_SCHED_FUNC(heartbeat_callback)
{
send_heartbeat();
/* reschedule this task */
task->runtime = switch_epoch_time_now(NULL) + runtime.event_heartbeat_interval;
}
SWITCH_STANDARD_SCHED_FUNC(check_ip_callback)
{
check_ip();
/* reschedule this task */
task->runtime = switch_epoch_time_now(NULL) + 60;
}
SWITCH_DECLARE(switch_status_t) switch_core_set_console(const char *console)
{
if ((runtime.console = fopen(console, "a")) == 0) {
fprintf(stderr, "Cannot open output file %s.\n", console);
return SWITCH_STATUS_FALSE;
}
return SWITCH_STATUS_SUCCESS;
}
SWITCH_DECLARE(FILE *) switch_core_get_console(void)
{
return runtime.console;
}
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
SWITCH_DECLARE(void) switch_core_screen_size(int *x, int *y)
{
#ifdef WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi;
int ret;
if ((ret = GetConsoleScreenBufferInfo(GetStdHandle( STD_OUTPUT_HANDLE ), &csbi))) {
if (x) *x = csbi.dwSize.X;
if (y) *y = csbi.dwSize.Y;
}
#elif defined(TIOCGWINSZ)
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
if (x) *x = w.ws_col;
if (y) *y = w.ws_row;
#else
if (x) *x = 80;
if (y) *y = 24;
#endif
}
SWITCH_DECLARE(FILE *) switch_core_data_channel(switch_text_channel_t channel)
{
return runtime.console;
}
SWITCH_DECLARE(void) switch_core_remove_state_handler(const switch_state_handler_table_t *state_handler)
{
int index, tmp_index = 0;
const switch_state_handler_table_t *tmp[SWITCH_MAX_STATE_HANDLERS + 1] = { 0 };
switch_mutex_lock(runtime.global_mutex);
for (index = 0; index < runtime.state_handler_index; index++) {
const switch_state_handler_table_t *cur = runtime.state_handlers[index];
runtime.state_handlers[index] = NULL;
if (cur == state_handler) {
continue;
}
tmp[tmp_index++] = cur;
}
runtime.state_handler_index = 0;
for (index = 0; index < tmp_index; index++) {
runtime.state_handlers[runtime.state_handler_index++] = tmp[index];
}
switch_mutex_unlock(runtime.global_mutex);
}
SWITCH_DECLARE(int) switch_core_add_state_handler(const switch_state_handler_table_t *state_handler)
{
int index;
switch_mutex_lock(runtime.global_mutex);
index = runtime.state_handler_index;
if (index > (SWITCH_MAX_STATE_HANDLERS - 1)) {
index = -1;
} else {
runtime.state_handlers[index] = state_handler;
runtime.state_handler_index++;
}
switch_mutex_unlock(runtime.global_mutex);
return index;
}
SWITCH_DECLARE(const switch_state_handler_table_t *) switch_core_get_state_handler(int index)
{
if (index >= SWITCH_MAX_STATE_HANDLERS || index > runtime.state_handler_index) {
return NULL;
}
return runtime.state_handlers[index];
}
SWITCH_DECLARE(void) switch_core_dump_variables(switch_stream_handle_t *stream)
{
switch_event_header_t *hi;
switch_mutex_lock(runtime.global_mutex);
for (hi = runtime.global_vars->headers; hi; hi = hi->next) {
stream->write_function(stream, "%s=%s\n", hi->name, hi->value);
}
switch_mutex_unlock(runtime.global_mutex);
}
SWITCH_DECLARE(const char *) switch_core_get_hostname(void)
{
return runtime.hostname;
}
SWITCH_DECLARE(const char *) switch_core_get_switchname(void)
{
if (!zstr(runtime.switchname)) return runtime.switchname;
return runtime.hostname;
}
SWITCH_DECLARE(char *) switch_core_get_domain(switch_bool_t dup)
{
char *domain;
const char *var;
switch_thread_rwlock_rdlock(runtime.global_var_rwlock);
if (!(var = switch_core_get_variable("domain"))) {
var = "freeswitch.local";
}
if (dup) {
domain = strdup(var);
} else {
domain = (char *) var;
}
switch_thread_rwlock_unlock(runtime.global_var_rwlock);
return domain;
}
SWITCH_DECLARE(switch_status_t) switch_core_get_variables(switch_event_t **event)
{
switch_status_t status;
switch_thread_rwlock_rdlock(runtime.global_var_rwlock);
status = switch_event_dup(event, runtime.global_vars);
switch_thread_rwlock_unlock(runtime.global_var_rwlock);
return status;
}
SWITCH_DECLARE(char *) switch_core_get_variable(const char *varname)
{
char *val;
switch_thread_rwlock_rdlock(runtime.global_var_rwlock);
val = (char *) switch_event_get_header(runtime.global_vars, varname);
switch_thread_rwlock_unlock(runtime.global_var_rwlock);
return val;
}
SWITCH_DECLARE(char *) switch_core_get_variable_dup(const char *varname)
{
char *val = NULL, *v;
if (varname) {
switch_thread_rwlock_rdlock(runtime.global_var_rwlock);
if ((v = (char *) switch_event_get_header(runtime.global_vars, varname))) {
val = strdup(v);
}
switch_thread_rwlock_unlock(runtime.global_var_rwlock);
}
return val;
}
SWITCH_DECLARE(char *) switch_core_get_variable_pdup(const char *varname, switch_memory_pool_t *pool)
{
char *val = NULL, *v;
if (varname) {
switch_thread_rwlock_rdlock(runtime.global_var_rwlock);
if ((v = (char *) switch_event_get_header(runtime.global_vars, varname))) {
val = switch_core_strdup(pool, v);
}
switch_thread_rwlock_unlock(runtime.global_var_rwlock);
}
return val;
}
static void switch_core_unset_variables(void)
{
switch_thread_rwlock_wrlock(runtime.global_var_rwlock);
switch_event_destroy(&runtime.global_vars);
switch_event_create_plain(&runtime.global_vars, SWITCH_EVENT_CHANNEL_DATA);
switch_thread_rwlock_unlock(runtime.global_var_rwlock);
}
SWITCH_DECLARE(void) switch_core_set_variable(const char *varname, const char *value)
{
char *val;
if (varname) {
switch_thread_rwlock_wrlock(runtime.global_var_rwlock);
val = (char *) switch_event_get_header(runtime.global_vars, varname);
if (val) {
switch_event_del_header(runtime.global_vars, varname);
}
if (value) {
char *v = strdup(value);
switch_string_var_check(v, SWITCH_TRUE);
switch_event_add_header_string(runtime.global_vars, SWITCH_STACK_BOTTOM, varname, v);
free(v);
} else {
switch_event_del_header(runtime.global_vars, varname);
}
switch_thread_rwlock_unlock(runtime.global_var_rwlock);
}
}
SWITCH_DECLARE(switch_bool_t) switch_core_set_var_conditional(const char *varname, const char *value, const char *val2)
{
char *val;
if (varname) {
switch_thread_rwlock_wrlock(runtime.global_var_rwlock);
val = (char *) switch_event_get_header(runtime.global_vars, varname);
if (val) {
if (!val2 || strcmp(val, val2) != 0) {
switch_thread_rwlock_unlock(runtime.global_var_rwlock);
return SWITCH_FALSE;
}
switch_event_del_header(runtime.global_vars, varname);
} else if (!zstr(val2)) {
switch_thread_rwlock_unlock(runtime.global_var_rwlock);
return SWITCH_FALSE;
}
if (value) {
char *v = strdup(value);
switch_string_var_check(v, SWITCH_TRUE);
switch_event_add_header_string_nodup(runtime.global_vars, SWITCH_STACK_BOTTOM, varname, v);
} else {
switch_event_del_header(runtime.global_vars, varname);
}
switch_thread_rwlock_unlock(runtime.global_var_rwlock);
}
return SWITCH_TRUE;
}
SWITCH_DECLARE(char *) switch_core_get_uuid(void)
{
return runtime.uuid_str;
}
static void *SWITCH_THREAD_FUNC switch_core_service_thread(switch_thread_t *thread, void *obj)
{
switch_core_session_t *session = obj;
switch_channel_t *channel;
switch_frame_t *read_frame = NULL;
// switch_assert(thread != NULL);
// switch_assert(session != NULL);
if (switch_core_session_read_lock(session) != SWITCH_STATUS_SUCCESS) {
return NULL;
}
switch_mutex_lock(session->frame_read_mutex);
channel = switch_core_session_get_channel(session);
switch_channel_set_flag(channel, CF_SERVICE);
while (switch_channel_test_flag(channel, CF_SERVICE)) {
if (switch_channel_test_flag(channel, CF_SERVICE_AUDIO)) {
switch (switch_core_session_read_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0)) {
case SWITCH_STATUS_SUCCESS:
case SWITCH_STATUS_TIMEOUT:
case SWITCH_STATUS_BREAK:
break;
default:
switch_channel_clear_flag(channel, CF_SERVICE);
break;
}
}
if (switch_channel_test_flag(channel, CF_SERVICE_VIDEO) && switch_channel_test_flag(channel, CF_VIDEO)) {
switch (switch_core_session_read_video_frame(session, &read_frame, SWITCH_IO_FLAG_NONE, 0)) {
case SWITCH_STATUS_SUCCESS:
case SWITCH_STATUS_TIMEOUT:
case SWITCH_STATUS_BREAK:
break;
default:
switch_channel_clear_flag(channel, CF_SERVICE);
break;
}
}
}
switch_mutex_unlock(session->frame_read_mutex);
switch_channel_clear_flag(channel, CF_SERVICE_AUDIO);
switch_channel_clear_flag(channel, CF_SERVICE_VIDEO);
switch_core_session_rwunlock(session);
return NULL;
}
/* Either add a timeout here or make damn sure the thread cannot get hung somehow (my preference) */
SWITCH_DECLARE(void) switch_core_thread_session_end(switch_core_session_t *session)
{
switch_channel_t *channel;
switch_assert(session);
channel = switch_core_session_get_channel(session);
switch_assert(channel);
switch_channel_clear_flag(channel, CF_SERVICE);
switch_channel_clear_flag(channel, CF_SERVICE_AUDIO);
switch_channel_clear_flag(channel, CF_SERVICE_VIDEO);
switch_core_session_kill_channel(session, SWITCH_SIG_BREAK);
}
SWITCH_DECLARE(void) switch_core_service_session_av(switch_core_session_t *session, switch_bool_t audio, switch_bool_t video)
{
switch_channel_t *channel;
switch_assert(session);
channel = switch_core_session_get_channel(session);
switch_assert(channel);
if (audio) switch_channel_set_flag(channel, CF_SERVICE_AUDIO);
if (video) switch_channel_set_flag(channel, CF_SERVICE_VIDEO);
switch_core_session_launch_thread(session, (void *(*)(switch_thread_t *,void *))switch_core_service_thread, session);
}
/* This function abstracts the thread creation for modules by allowing you to pass a function ptr and
a void object and trust that that the function will be run in a thread with arg This lets
you request and activate a thread without giving up any knowledge about what is in the thread
neither the core nor the calling module know anything about each other.
This thread is expected to never exit until the application exits so the func is responsible
to make sure that is the case.
The typical use for this is so switch_loadable_module.c can start up a thread for each module
passing the table of module methods as a session obj into the core without actually allowing
the core to have any clue and keeping switch_loadable_module.c from needing any thread code.
*/
SWITCH_DECLARE(switch_thread_t *) switch_core_launch_thread(switch_thread_start_t func, void *obj, switch_memory_pool_t *pool)
{
switch_thread_t *thread = NULL;
switch_threadattr_t *thd_attr = NULL;
switch_core_thread_session_t *ts;
int mypool;
mypool = pool ? 0 : 1;
if (!pool && switch_core_new_memory_pool(&pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not allocate memory pool\n");
return NULL;
}
switch_threadattr_create(&thd_attr, pool);
if ((ts = switch_core_alloc(pool, sizeof(*ts))) == 0) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, "Could not allocate memory\n");
} else {
if (mypool) {
ts->pool = pool;
}
ts->objs[0] = obj;
ts->objs[1] = thread;
switch_threadattr_stacksize_set(thd_attr, SWITCH_THREAD_STACKSIZE);
switch_threadattr_priority_set(thd_attr, SWITCH_PRI_REALTIME);
switch_thread_create(&thread, thd_attr, func, ts, pool);
}
return thread;
}
SWITCH_DECLARE(void) switch_core_set_globals(void)
{
#define BUFSIZE 1024
#ifdef WIN32
char lpPathBuffer[BUFSIZE];
DWORD dwBufSize = BUFSIZE;
char base_dir[1024];
char *lastbacklash;
char *tmp;
GetModuleFileName(NULL, base_dir, BUFSIZE);
lastbacklash = strrchr(base_dir, '\\');
base_dir[(lastbacklash - base_dir)] = '\0';
/* set base_dir as cwd, to be able to use relative paths in scripting languages (e.g. mod_lua) when FS is running as a service or while debugging FS using visual studio */
SetCurrentDirectory(base_dir);
tmp = switch_string_replace(base_dir, "\\", "/");
strcpy(base_dir, tmp);
free(tmp);
#else
char base_dir[1024] = SWITCH_PREFIX_DIR;
#endif
/* Order of precedence for, eg, rundir:
* -run
* -base
* --with-rundir
* --prefix
*/
if (!SWITCH_GLOBAL_dirs.mod_dir && (SWITCH_GLOBAL_dirs.mod_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.mod_dir, BUFSIZE, "%s%smod", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_MOD_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.mod_dir, BUFSIZE, "%s", SWITCH_MOD_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.mod_dir, BUFSIZE, "%s%smod", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.lib_dir && (SWITCH_GLOBAL_dirs.lib_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.lib_dir, BUFSIZE, "%s%slib", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_LIB_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.lib_dir, BUFSIZE, "%s", SWITCH_LIB_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.lib_dir, BUFSIZE, "%s%slib", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.conf_dir && (SWITCH_GLOBAL_dirs.conf_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.conf_dir, BUFSIZE, "%s%sconf", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_CONF_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.conf_dir, BUFSIZE, "%s", SWITCH_CONF_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.conf_dir, BUFSIZE, "%s%sconf", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.log_dir && (SWITCH_GLOBAL_dirs.log_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.log_dir, BUFSIZE, "%s%slog", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_LOG_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.log_dir, BUFSIZE, "%s", SWITCH_LOG_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.log_dir, BUFSIZE, "%s%slog", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.run_dir && (SWITCH_GLOBAL_dirs.run_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.run_dir, BUFSIZE, "%s%srun", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_RUN_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.run_dir, BUFSIZE, "%s", SWITCH_RUN_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.run_dir, BUFSIZE, "%s%srun", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.recordings_dir && (SWITCH_GLOBAL_dirs.recordings_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.recordings_dir, BUFSIZE, "%s%srecordings", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_RECORDINGS_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.recordings_dir, BUFSIZE, "%s", SWITCH_RECORDINGS_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.recordings_dir, BUFSIZE, "%s%srecordings", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.sounds_dir && (SWITCH_GLOBAL_dirs.sounds_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.sounds_dir, BUFSIZE, "%s%ssounds", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_SOUNDS_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.sounds_dir, BUFSIZE, "%s", SWITCH_SOUNDS_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.sounds_dir, BUFSIZE, "%s%ssounds", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.storage_dir && (SWITCH_GLOBAL_dirs.storage_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.storage_dir, BUFSIZE, "%s%sstorage", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_STORAGE_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.storage_dir, BUFSIZE, "%s", SWITCH_STORAGE_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.storage_dir, BUFSIZE, "%s%sstorage", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.cache_dir && (SWITCH_GLOBAL_dirs.cache_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.cache_dir, BUFSIZE, "%s%scache", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_CACHE_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.cache_dir, BUFSIZE, "%s", SWITCH_CACHE_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.cache_dir, BUFSIZE, "%s%scache", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.db_dir && (SWITCH_GLOBAL_dirs.db_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.db_dir, BUFSIZE, "%s%sdb", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_DB_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.db_dir, BUFSIZE, "%s", SWITCH_DB_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.db_dir, BUFSIZE, "%s%sdb", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.script_dir && (SWITCH_GLOBAL_dirs.script_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.script_dir, BUFSIZE, "%s%sscripts", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_SCRIPT_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.script_dir, BUFSIZE, "%s", SWITCH_SCRIPT_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.script_dir, BUFSIZE, "%s%sscripts", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.htdocs_dir && (SWITCH_GLOBAL_dirs.htdocs_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.htdocs_dir, BUFSIZE, "%s%shtdocs", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_HTDOCS_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.htdocs_dir, BUFSIZE, "%s", SWITCH_HTDOCS_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.htdocs_dir, BUFSIZE, "%s%shtdocs", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.grammar_dir && (SWITCH_GLOBAL_dirs.grammar_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.grammar_dir, BUFSIZE, "%s%sgrammar", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_GRAMMAR_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.grammar_dir, BUFSIZE, "%s", SWITCH_GRAMMAR_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.grammar_dir, BUFSIZE, "%s%sgrammar", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.fonts_dir && (SWITCH_GLOBAL_dirs.fonts_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.fonts_dir, BUFSIZE, "%s%sfonts", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_FONTS_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.fonts_dir, BUFSIZE, "%s", SWITCH_FONTS_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.fonts_dir, BUFSIZE, "%s%sfonts", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.images_dir && (SWITCH_GLOBAL_dirs.images_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.images_dir, BUFSIZE, "%s%simages", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_IMAGES_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.images_dir, BUFSIZE, "%s", SWITCH_IMAGES_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.images_dir, BUFSIZE, "%s%simages", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.data_dir && (SWITCH_GLOBAL_dirs.data_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.data_dir, BUFSIZE, "%s", SWITCH_GLOBAL_dirs.base_dir);
else
#ifdef SWITCH_DATA_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.data_dir, BUFSIZE, "%s", SWITCH_DATA_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.data_dir, BUFSIZE, "%s", base_dir);
#endif
}
if (!SWITCH_GLOBAL_dirs.localstate_dir && (SWITCH_GLOBAL_dirs.localstate_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.localstate_dir, BUFSIZE, "%s", SWITCH_GLOBAL_dirs.base_dir);
else
#ifdef SWITCH_LOCALSTATE_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.localstate_dir, BUFSIZE, "%s", SWITCH_LOCALSTATE_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.localstate_dir, BUFSIZE, "%s", base_dir);
#endif
}
if (!SWITCH_GLOBAL_dirs.certs_dir && (SWITCH_GLOBAL_dirs.certs_dir = (char *) malloc(BUFSIZE))) {
if (SWITCH_GLOBAL_dirs.base_dir)
switch_snprintf(SWITCH_GLOBAL_dirs.certs_dir, BUFSIZE, "%s%scert", SWITCH_GLOBAL_dirs.base_dir, SWITCH_PATH_SEPARATOR);
else
#ifdef SWITCH_CERTS_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.certs_dir, BUFSIZE, "%s", SWITCH_CERTS_DIR);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.certs_dir, BUFSIZE, "%s%scert", base_dir, SWITCH_PATH_SEPARATOR);
#endif
}
if (!SWITCH_GLOBAL_dirs.temp_dir && (SWITCH_GLOBAL_dirs.temp_dir = (char *) malloc(BUFSIZE))) {
#ifdef SWITCH_TEMP_DIR
switch_snprintf(SWITCH_GLOBAL_dirs.temp_dir, BUFSIZE, "%s", SWITCH_TEMP_DIR);
#else
#ifdef WIN32
GetTempPath(dwBufSize, lpPathBuffer);
lpPathBuffer[strlen(lpPathBuffer)-1] = 0;
tmp = switch_string_replace(lpPathBuffer, "\\", "/");
strcpy(lpPathBuffer, tmp);
free(tmp);
switch_snprintf(SWITCH_GLOBAL_dirs.temp_dir, BUFSIZE, "%s", lpPathBuffer);
#else
switch_snprintf(SWITCH_GLOBAL_dirs.temp_dir, BUFSIZE, "%s", "/tmp");
#endif
#endif
}
if (!SWITCH_GLOBAL_filenames.conf_name && (SWITCH_GLOBAL_filenames.conf_name = (char *) malloc(BUFSIZE))) {
switch_snprintf(SWITCH_GLOBAL_filenames.conf_name, BUFSIZE, "%s", "freeswitch.xml");
}
/* Do this last because it being empty is part of the above logic */
if (!SWITCH_GLOBAL_dirs.base_dir && (SWITCH_GLOBAL_dirs.base_dir = (char *) malloc(BUFSIZE))) {
switch_snprintf(SWITCH_GLOBAL_dirs.base_dir, BUFSIZE, "%s", base_dir);
}
switch_assert(SWITCH_GLOBAL_dirs.base_dir);
switch_assert(SWITCH_GLOBAL_dirs.mod_dir);
switch_assert(SWITCH_GLOBAL_dirs.lib_dir);
switch_assert(SWITCH_GLOBAL_dirs.conf_dir);
switch_assert(SWITCH_GLOBAL_dirs.log_dir);
switch_assert(SWITCH_GLOBAL_dirs.run_dir);
switch_assert(SWITCH_GLOBAL_dirs.db_dir);
switch_assert(SWITCH_GLOBAL_dirs.script_dir);
switch_assert(SWITCH_GLOBAL_dirs.htdocs_dir);
switch_assert(SWITCH_GLOBAL_dirs.grammar_dir);
switch_assert(SWITCH_GLOBAL_dirs.fonts_dir);
switch_assert(SWITCH_GLOBAL_dirs.images_dir);
switch_assert(SWITCH_GLOBAL_dirs.recordings_dir);
switch_assert(SWITCH_GLOBAL_dirs.sounds_dir);
switch_assert(SWITCH_GLOBAL_dirs.certs_dir);
switch_assert(SWITCH_GLOBAL_dirs.temp_dir);
switch_assert(SWITCH_GLOBAL_dirs.data_dir);
switch_assert(SWITCH_GLOBAL_dirs.localstate_dir);
switch_assert(SWITCH_GLOBAL_filenames.conf_name);
}
SWITCH_DECLARE(int32_t) switch_core_set_process_privileges(void)
{
#ifdef SOLARIS_PRIVILEGES
priv_set_t *basicset;
/* make the process privilege-aware */
setpflags(PRIV_AWARE, 1);
/* reset the privileges to basic */
basicset = priv_str_to_set("basic", ",", NULL);
if (setppriv(PRIV_SET, PRIV_EFFECTIVE, basicset) != 0) {
fprintf(stderr, "ERROR: Failed to acquire basic privileges (%s)\n", strerror(errno));
}
/* we need high-resolution clock, and this requires a non-basic privilege */
if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_CLOCK_HIGHRES, NULL) < 0) {
fprintf(stderr, "ERROR: Failed to acquire proc_clock_highres privilege (%s)\n", strerror(errno));
return -1;
}
/* need this for setrlimit */
if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_SYS_RESOURCE, NULL) < 0) {
fprintf(stderr, "ERROR: Failed to acquire sys_resource privilege (%s)\n", strerror(errno));
return -1;
}
/* we need to read directories belonging to other uid */
if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_FILE_DAC_SEARCH, NULL) < 0) {
fprintf(stderr, "ERROR: Failed to acquire file_dac_search privilege (%s)\n", strerror(errno));
return -1;
}
#endif
return 0;
}
SWITCH_DECLARE(int32_t) set_low_priority(void)
{
#ifdef WIN32
return SetPriorityClass(GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS);
#else
#if defined(USE_SCHED_SETSCHEDULER) && ! defined(SOLARIS_PRIVILEGES)
/*
* Try to use a normal scheduler
*/
struct sched_param sched = { 0 };
sched.sched_priority = 0;
if (sched_setscheduler(0, SCHED_OTHER, &sched) < 0) {
fprintf(stderr, "ERROR: Failed to set SCHED_OTHER scheduler (%s)\n", strerror(errno));
return -1;
}
#endif
#ifdef HAVE_SETPRIORITY
/*
* setpriority() works on FreeBSD (6.2), nice() doesn't
*/
if (setpriority(PRIO_PROCESS, getpid(), 19) < 0) {
fprintf(stderr, "ERROR: Could not set nice level\n");
return -1;
}
#else
if (nice(19) != 19) {
fprintf(stderr, "ERROR: Could not set nice level\n");
return -1;
}
#endif
return 0;
#endif
}
SWITCH_DECLARE(int32_t) set_realtime_priority(void)
{
#ifdef WIN32
return SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
#else
#ifdef USE_SCHED_SETSCHEDULER
/*
* Try to use a round-robin scheduler
* with a fallback if that does not work
*/
struct sched_param sched = { 0 };
sched.sched_priority = SWITCH_PRI_LOW;
#endif
#ifdef SOLARIS_PRIVILEGES
/* request the privileges to elevate the priority */
if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_PRIOCNTL, NULL) < 0) {
#ifdef PRIV_PROC_PRIOUP
/* fallback to PRIV_PROC_PRIOUP on SmartOS */
fprintf(stderr, "WARN: Failed to acquire proc_priocntl privilege (%s)\n", strerror(errno));
if (priv_set(PRIV_ON, PRIV_EFFECTIVE, PRIV_PROC_PRIOUP, NULL) < 0) {