/
os_unix.c
8461 lines (7693 loc) · 197 KB
/
os_unix.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
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
* OS/2 port by Paul Slootman
* VMS merge by Zoltan Arpadffy
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* os_unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
* Also for OS/2, using the excellent EMX package!!!
* Also for Atari MiNT.
*
* A lot of this file was originally written by Juergen Weigert and later
* changed beyond recognition.
*/
#include "vim.h"
#ifdef FEAT_MZSCHEME
# include "if_mzsch.h"
#endif
#include "os_unixx.h" // unix includes for os_unix.c only
#ifdef USE_XSMP
# include <X11/SM/SMlib.h>
#endif
#ifdef HAVE_SELINUX
# include <selinux/selinux.h>
static int selinux_enabled = -1;
#endif
#ifdef HAVE_SMACK
# include <attr/xattr.h>
# include <linux/xattr.h>
# ifndef SMACK_LABEL_LEN
# define SMACK_LABEL_LEN 1024
# endif
#endif
#ifdef __CYGWIN__
# include <cygwin/version.h>
# include <sys/cygwin.h> // for cygwin_conv_to_posix_path() and/or
// for cygwin_conv_path()
# ifdef FEAT_CYGWIN_WIN32_CLIPBOARD
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include "winclip.pro"
# endif
#endif
#ifdef FEAT_MOUSE_GPM
# include <gpm.h>
# ifdef DYNAMIC_GPM
# define Gpm_Open (*dll_Gpm_Open)
# define Gpm_Close (*dll_Gpm_Close)
# define Gpm_GetEvent (*dll_Gpm_GetEvent)
# define gpm_flag (dll_gpm_flag != NULL ? *dll_gpm_flag : 0)
# define gpm_fd (dll_gpm_fd != NULL ? *dll_gpm_fd : -1)
static int (*dll_Gpm_Open) (Gpm_Connect *, int);
static int (*dll_Gpm_Close) (void);
static int (*dll_Gpm_GetEvent) (Gpm_Event *);
static int *dll_gpm_flag;
static int *dll_gpm_fd;
static void *libgpm_hinst;
# endif
// <linux/keyboard.h> contains defines conflicting with "keymap.h",
// I just copied relevant defines here. A cleaner solution would be to put gpm
// code into separate file and include there linux/keyboard.h
// #include <linux/keyboard.h>
# define KG_SHIFT 0
# define KG_CTRL 2
# define KG_ALT 3
# define KG_ALTGR 1
# define KG_SHIFTL 4
# define KG_SHIFTR 5
# define KG_CTRLL 6
# define KG_CTRLR 7
# define KG_CAPSSHIFT 8
static void gpm_close(void);
static int gpm_open(void);
static int mch_gpm_process(void);
#endif
#ifdef FEAT_SYSMOUSE
# include <sys/consio.h>
# include <sys/fbio.h>
static int sysmouse_open(void);
static void sysmouse_close(void);
static void sig_sysmouse SIGPROTOARG;
#endif
/*
* end of autoconf section. To be extended...
*/
// Are the following #ifdefs still required? And why? Is that for X11?
#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
# ifdef SIGWINCH
# undef SIGWINCH
# endif
# ifdef TIOCGWINSZ
# undef TIOCGWINSZ
# endif
#endif
#if defined(SIGWINDOW) && !defined(SIGWINCH) // hpux 9.01 has it
# define SIGWINCH SIGWINDOW
#endif
#ifdef FEAT_X11
# include <X11/Xlib.h>
# include <X11/Xutil.h>
# include <X11/Xatom.h>
# ifdef FEAT_XCLIPBOARD
# include <X11/Intrinsic.h>
# include <X11/Shell.h>
# include <X11/StringDefs.h>
static Widget xterm_Shell = (Widget)0;
static void clip_update(void);
static void xterm_update(void);
# endif
Window x11_window = 0;
Display *x11_display = NULL;
#endif
static int ignore_sigtstp = FALSE;
static int get_x11_title(int);
static char_u *oldtitle = NULL;
static volatile sig_atomic_t oldtitle_outdated = FALSE;
static int unix_did_set_title = FALSE;
static char_u *oldicon = NULL;
static int did_set_icon = FALSE;
static void may_core_dump(void);
#ifdef HAVE_UNION_WAIT
typedef union wait waitstatus;
#else
typedef int waitstatus;
#endif
static int WaitForChar(long msec, int *interrupted, int ignore_input);
static int WaitForCharOrMouse(long msec, int *interrupted, int ignore_input);
#ifdef VMS
int RealWaitForChar(int, long, int *, int *interrupted);
#else
static int RealWaitForChar(int, long, int *, int *interrupted);
#endif
#ifdef FEAT_XCLIPBOARD
static int do_xterm_trace(void);
# define XT_TRACE_DELAY 50 // delay for xterm tracing
#endif
static void handle_resize(void);
#if defined(SIGWINCH)
static void sig_winch SIGPROTOARG;
#endif
#if defined(SIGTSTP)
static void sig_tstp SIGPROTOARG;
// volatile because it is used in signal handler sig_tstp() and sigcont_handler().
static volatile sig_atomic_t in_mch_suspend = FALSE;
#endif
#if defined(SIGINT)
static void catch_sigint SIGPROTOARG;
#endif
#if defined(SIGUSR1)
static void catch_sigusr1 SIGPROTOARG;
#endif
#if defined(SIGPWR)
static void catch_sigpwr SIGPROTOARG;
#endif
#if defined(SIGALRM) && defined(FEAT_X11) && !defined(FEAT_GUI_GTK)
# define SET_SIG_ALARM
static void sig_alarm SIGPROTOARG;
// volatile because it is used in signal handler sig_alarm().
static volatile sig_atomic_t sig_alarm_called;
#endif
static void deathtrap SIGPROTOARG;
static void catch_int_signal(void);
static void set_signals(void);
static void catch_signals(void (*func_deadly)(), void (*func_other)());
#ifdef HAVE_SIGPROCMASK
# define SIGSET_DECL(set) sigset_t set;
# define BLOCK_SIGNALS(set) block_signals(set)
# define UNBLOCK_SIGNALS(set) unblock_signals(set)
#else
# define SIGSET_DECL(set)
# define BLOCK_SIGNALS(set) do { /**/ } while (0)
# define UNBLOCK_SIGNALS(set) do { /**/ } while (0)
#endif
static int have_wildcard(int, char_u **);
static int have_dollars(int, char_u **);
static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
#ifndef SIG_ERR
# define SIG_ERR ((void (*)())-1)
#endif
// volatile because it is used in signal handler sig_winch().
static volatile sig_atomic_t do_resize = FALSE;
// volatile because it is used in signal handler sig_tstp().
static volatile sig_atomic_t got_tstp = FALSE;
static char_u *extra_shell_arg = NULL;
static int show_shell_mess = TRUE;
// volatile because it is used in signal handler deathtrap().
static volatile sig_atomic_t deadly_signal = 0; // The signal we caught
// volatile because it is used in signal handler deathtrap().
static volatile sig_atomic_t in_mch_delay = FALSE; // sleeping in mch_delay()
#if defined(FEAT_JOB_CHANNEL) && !defined(USE_SYSTEM)
static int dont_check_job_ended = 0;
#endif
// Current terminal mode from mch_settmode(). Can differ from cur_tmode.
static tmode_T mch_cur_tmode = TMODE_COOK;
#ifdef USE_XSMP
typedef struct
{
SmcConn smcconn; // The SM connection ID
IceConn iceconn; // The ICE connection ID
char *clientid; // The client ID for the current smc session
Bool save_yourself; // If we're in the middle of a save_yourself
Bool shutdown; // If we're in shutdown mode
} xsmp_config_T;
static xsmp_config_T xsmp;
#endif
#ifdef SYS_SIGLIST_DECLARED
/*
* I have seen
* extern char *_sys_siglist[NSIG];
* on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
* that describe the signals. That is nearly what we want here. But
* autoconf does only check for sys_siglist (without the underscore), I
* do not want to change everything today.... jw.
* This is why AC_DECL_SYS_SIGLIST is commented out in configure.ac.
*/
#endif
static struct signalinfo
{
int sig; // Signal number, eg. SIGSEGV etc
char *name; // Signal name (not char_u!).
char deadly; // Catch as a deadly signal?
} signal_info[] =
{
#ifdef SIGHUP
{SIGHUP, "HUP", TRUE},
#endif
#ifdef SIGQUIT
{SIGQUIT, "QUIT", TRUE},
#endif
#ifdef SIGILL
{SIGILL, "ILL", TRUE},
#endif
#ifdef SIGTRAP
{SIGTRAP, "TRAP", TRUE},
#endif
#ifdef SIGABRT
{SIGABRT, "ABRT", TRUE},
#endif
#ifdef SIGEMT
{SIGEMT, "EMT", TRUE},
#endif
#ifdef SIGFPE
{SIGFPE, "FPE", TRUE},
#endif
#ifdef SIGBUS
{SIGBUS, "BUS", TRUE},
#endif
#if defined(SIGSEGV) && !defined(FEAT_MZSCHEME)
// MzScheme uses SEGV in its garbage collector
{SIGSEGV, "SEGV", TRUE},
#endif
#ifdef SIGSYS
{SIGSYS, "SYS", TRUE},
#endif
#ifdef SIGALRM
{SIGALRM, "ALRM", FALSE}, // Perl's alarm() can trigger it
#endif
#ifdef SIGTERM
{SIGTERM, "TERM", TRUE},
#endif
#if defined(SIGVTALRM) && !defined(FEAT_RUBY)
{SIGVTALRM, "VTALRM", TRUE},
#endif
#if defined(SIGPROF) && !defined(FEAT_MZSCHEME) && !defined(WE_ARE_PROFILING)
// MzScheme uses SIGPROF for its own needs; On Linux with profiling
// this makes Vim exit. WE_ARE_PROFILING is defined in Makefile.
{SIGPROF, "PROF", TRUE},
#endif
#ifdef SIGXCPU
{SIGXCPU, "XCPU", TRUE},
#endif
#ifdef SIGXFSZ
{SIGXFSZ, "XFSZ", TRUE},
#endif
#ifdef SIGUSR1
{SIGUSR1, "USR1", FALSE},
#endif
#if defined(SIGUSR2) && !defined(FEAT_SYSMOUSE)
// Used for sysmouse handling
{SIGUSR2, "USR2", TRUE},
#endif
#ifdef SIGINT
{SIGINT, "INT", FALSE},
#endif
#ifdef SIGWINCH
{SIGWINCH, "WINCH", FALSE},
#endif
#ifdef SIGTSTP
{SIGTSTP, "TSTP", FALSE},
#endif
#ifdef SIGPIPE
{SIGPIPE, "PIPE", FALSE},
#endif
{-1, "Unknown!", FALSE}
};
int
mch_chdir(char *path)
{
if (p_verbose >= 5)
{
verbose_enter();
smsg("chdir(%s)", path);
verbose_leave();
}
# ifdef VMS
return chdir(vms_fixfilename(path));
# else
return chdir(path);
# endif
}
// Why is NeXT excluded here (and not in os_unixx.h)?
#if defined(ECHOE) && defined(ICANON) \
&& (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
&& !defined(__NeXT__)
# define NEW_TTY_SYSTEM
#endif
/*
* Write s[len] to the screen (stdout).
*/
void
mch_write(char_u *s, int len)
{
vim_ignored = (int)write(1, (char *)s, len);
if (p_wd) // Unix is too fast, slow down a bit more
RealWaitForChar(read_cmd_fd, p_wd, NULL, NULL);
}
/*
* Function passed to inchar_loop() to handle window resizing.
* If "check_only" is TRUE: Return whether there was a resize.
* If "check_only" is FALSE: Deal with the window resized.
*/
static int
resize_func(int check_only)
{
if (check_only)
return do_resize;
while (do_resize)
handle_resize();
return FALSE;
}
/*
* mch_inchar(): low level input function.
* Get a characters from the keyboard.
* Return the number of characters that are available.
* If wtime == 0 do not wait for characters.
* If wtime == n wait a short time for characters.
* If wtime == -1 wait forever for characters.
*/
int
mch_inchar(
char_u *buf,
int maxlen,
long wtime, // don't use "time", MIPS cannot handle it
int tb_change_cnt)
{
return inchar_loop(buf, maxlen, wtime, tb_change_cnt,
WaitForChar, resize_func);
}
static void
handle_resize(void)
{
do_resize = FALSE;
shell_resized();
}
/*
* Return non-zero if a character is available.
*/
int
mch_char_avail(void)
{
return WaitForChar(0L, NULL, FALSE);
}
#if defined(FEAT_TERMINAL) || defined(PROTO)
/*
* Check for any pending input or messages.
*/
int
mch_check_messages(void)
{
return WaitForChar(0L, NULL, TRUE);
}
#endif
#if defined(HAVE_TOTAL_MEM) || defined(PROTO)
# ifdef HAVE_SYS_RESOURCE_H
# include <sys/resource.h>
# endif
# if defined(HAVE_SYS_SYSCTL_H) && defined(HAVE_SYSCTL)
# include <sys/sysctl.h>
# endif
# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
# include <sys/sysinfo.h>
# endif
# ifdef MACOS_X
# include <mach/mach_host.h>
# include <mach/mach_port.h>
# endif
/*
* Return total amount of memory available in Kbyte.
* Doesn't change when memory has been allocated.
*/
long_u
mch_total_mem(int special UNUSED)
{
long_u mem = 0;
long_u shiftright = 10; // how much to shift "mem" right for Kbyte
# ifdef MACOS_X
{
// Mac (Darwin) way of getting the amount of RAM available
mach_port_t host = mach_host_self();
kern_return_t kret;
# ifdef HOST_VM_INFO64
struct vm_statistics64 vm_stat;
natural_t count = HOST_VM_INFO64_COUNT;
kret = host_statistics64(host, HOST_VM_INFO64,
(host_info64_t)&vm_stat, &count);
# else
struct vm_statistics vm_stat;
natural_t count = HOST_VM_INFO_COUNT;
kret = host_statistics(host, HOST_VM_INFO,
(host_info_t)&vm_stat, &count);
# endif
if (kret == KERN_SUCCESS)
// get the amount of user memory by summing each usage
mem = (long_u)(vm_stat.free_count + vm_stat.active_count
+ vm_stat.inactive_count
# ifdef MAC_OS_X_VERSION_10_9
+ vm_stat.compressor_page_count
# endif
) * sysconf(_SC_PAGESIZE);
mach_port_deallocate(mach_task_self(), host);
}
# endif
# ifdef HAVE_SYSCTL
if (mem == 0)
{
// BSD way of getting the amount of RAM available.
int mib[2];
size_t len = sizeof(long_u);
# ifdef HW_USERMEM64
long_u physmem;
# else
// sysctl() may return 32 bit or 64 bit, accept both
union {
int_u u32;
long_u u64;
} physmem;
# endif
mib[0] = CTL_HW;
# ifdef HW_USERMEM64
mib[1] = HW_USERMEM64;
# else
mib[1] = HW_USERMEM;
# endif
if (sysctl(mib, 2, &physmem, &len, NULL, 0) == 0)
{
# ifdef HW_USERMEM64
mem = (long_u)physmem;
# else
if (len == sizeof(physmem.u64))
mem = (long_u)physmem.u64;
else
mem = (long_u)physmem.u32;
# endif
}
}
# endif
# if defined(HAVE_SYS_SYSINFO_H) && defined(HAVE_SYSINFO)
if (mem == 0)
{
struct sysinfo sinfo;
// Linux way of getting amount of RAM available
if (sysinfo(&sinfo) == 0)
{
# ifdef HAVE_SYSINFO_MEM_UNIT
// avoid overflow as much as possible
while (shiftright > 0 && (sinfo.mem_unit & 1) == 0)
{
sinfo.mem_unit = sinfo.mem_unit >> 1;
--shiftright;
}
mem = sinfo.totalram * sinfo.mem_unit;
# else
mem = sinfo.totalram;
# endif
}
}
# endif
# ifdef HAVE_SYSCONF
if (mem == 0)
{
long pagesize, pagecount;
// Solaris way of getting amount of RAM available
pagesize = sysconf(_SC_PAGESIZE);
pagecount = sysconf(_SC_PHYS_PAGES);
if (pagesize > 0 && pagecount > 0)
{
// avoid overflow as much as possible
while (shiftright > 0 && (pagesize & 1) == 0)
{
pagesize = (long_u)pagesize >> 1;
--shiftright;
}
mem = (long_u)pagesize * pagecount;
}
}
# endif
// Return the minimum of the physical memory and the user limit, because
// using more than the user limit may cause Vim to be terminated.
# if defined(HAVE_SYS_RESOURCE_H) && defined(HAVE_GETRLIMIT)
{
struct rlimit rlp;
if (getrlimit(RLIMIT_DATA, &rlp) == 0
&& rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
# ifdef RLIM_INFINITY
&& rlp.rlim_cur != RLIM_INFINITY
# endif
&& ((long_u)rlp.rlim_cur >> 10) < (mem >> shiftright)
)
{
mem = (long_u)rlp.rlim_cur;
shiftright = 10;
}
}
# endif
if (mem > 0)
return mem >> shiftright;
return (long_u)0x1fffff;
}
#endif
/*
* "flags": MCH_DELAY_IGNOREINPUT - don't read input
* MCH_DELAY_SETTMODE - use settmode() even for short delays
*/
void
mch_delay(long msec, int flags)
{
tmode_T old_tmode;
int call_settmode;
#ifdef FEAT_MZSCHEME
long total = msec; // remember original value
#endif
if (flags & MCH_DELAY_IGNOREINPUT)
{
// Go to cooked mode without echo, to allow SIGINT interrupting us
// here. But we don't want QUIT to kill us (CTRL-\ used in a
// shell may produce SIGQUIT).
// Only do this if sleeping for more than half a second.
in_mch_delay = TRUE;
call_settmode = mch_cur_tmode == TMODE_RAW
&& (msec > 500 || (flags & MCH_DELAY_SETTMODE));
if (call_settmode)
{
old_tmode = mch_cur_tmode;
settmode(TMODE_SLEEP);
}
/*
* Everybody sleeps in a different way...
* Prefer nanosleep(), some versions of usleep() can only sleep up to
* one second.
*/
#ifdef FEAT_MZSCHEME
do
{
// if total is large enough, wait by portions in p_mzq
if (total > p_mzq)
msec = p_mzq;
else
msec = total;
total -= msec;
#endif
#ifdef HAVE_NANOSLEEP
{
struct timespec ts;
ts.tv_sec = msec / 1000;
ts.tv_nsec = (msec % 1000) * 1000000;
(void)nanosleep(&ts, NULL);
}
#else
# ifdef HAVE_USLEEP
while (msec >= 1000)
{
usleep((unsigned int)(999 * 1000));
msec -= 999;
}
usleep((unsigned int)(msec * 1000));
# else
# ifndef HAVE_SELECT
poll(NULL, 0, (int)msec);
# else
{
struct timeval tv;
tv.tv_sec = msec / 1000;
tv.tv_usec = (msec % 1000) * 1000;
// NOTE: Solaris 2.6 has a bug that makes select() hang here. Get
// a patch from Sun to fix this. Reported by Gunnar Pedersen.
select(0, NULL, NULL, NULL, &tv);
}
# endif // HAVE_SELECT
# endif // HAVE_NANOSLEEP
#endif // HAVE_USLEEP
#ifdef FEAT_MZSCHEME
}
while (total > 0);
#endif
if (call_settmode)
settmode(old_tmode);
in_mch_delay = FALSE;
}
else
WaitForChar(msec, NULL, FALSE);
}
#if defined(HAVE_STACK_LIMIT) \
|| (!defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGSTACK))
# define HAVE_CHECK_STACK_GROWTH
/*
* Support for checking for an almost-out-of-stack-space situation.
*/
/*
* Return a pointer to an item on the stack. Used to find out if the stack
* grows up or down.
*/
static int stack_grows_downwards;
/*
* Find out if the stack grows upwards or downwards.
* "p" points to a variable on the stack of the caller.
*/
static void
check_stack_growth(char *p)
{
int i;
stack_grows_downwards = (p > (char *)&i);
}
#endif
#if defined(HAVE_STACK_LIMIT) || defined(PROTO)
static char *stack_limit = NULL;
#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
# include <pthread.h>
# include <pthread_np.h>
#endif
/*
* Find out until how var the stack can grow without getting into trouble.
* Called when starting up and when switching to the signal stack in
* deathtrap().
*/
static void
get_stack_limit(void)
{
struct rlimit rlp;
int i;
long lim;
// Set the stack limit to 15/16 of the allowable size. Skip this when the
// limit doesn't fit in a long (rlim_cur might be "long long").
if (getrlimit(RLIMIT_STACK, &rlp) == 0
&& rlp.rlim_cur < ((rlim_t)1 << (sizeof(long_u) * 8 - 1))
# ifdef RLIM_INFINITY
&& rlp.rlim_cur != RLIM_INFINITY
# endif
)
{
lim = (long)rlp.rlim_cur;
#if defined(_THREAD_SAFE) && defined(HAVE_PTHREAD_NP_H)
{
pthread_attr_t attr;
size_t size;
// On FreeBSD the initial thread always has a fixed stack size, no
// matter what the limits are set to. Normally it's 1 Mbyte.
pthread_attr_init(&attr);
if (pthread_attr_get_np(pthread_self(), &attr) == 0)
{
pthread_attr_getstacksize(&attr, &size);
if (lim > (long)size)
lim = (long)size;
}
pthread_attr_destroy(&attr);
}
#endif
if (stack_grows_downwards)
{
stack_limit = (char *)((long)&i - (lim / 16L * 15L));
if (stack_limit >= (char *)&i)
// overflow, set to 1/16 of current stack position
stack_limit = (char *)((long)&i / 16L);
}
else
{
stack_limit = (char *)((long)&i + (lim / 16L * 15L));
if (stack_limit <= (char *)&i)
stack_limit = NULL; // overflow
}
}
}
/*
* Return FAIL when running out of stack space.
* "p" must point to any variable local to the caller that's on the stack.
*/
int
mch_stackcheck(char *p)
{
if (stack_limit != NULL)
{
if (stack_grows_downwards)
{
if (p < stack_limit)
return FAIL;
}
else if (p > stack_limit)
return FAIL;
}
return OK;
}
#endif
#if defined(HAVE_SIGALTSTACK) || defined(HAVE_SIGSTACK)
/*
* Support for using the signal stack.
* This helps when we run out of stack space, which causes a SIGSEGV. The
* signal handler then must run on another stack, since the normal stack is
* completely full.
*/
# ifdef HAVE_SIGALTSTACK
static stack_t sigstk; // for sigaltstack()
# else
static struct sigstack sigstk; // for sigstack()
# endif
/*
* Get a size of signal stack.
* Preference (if available): sysconf > SIGSTKSZ > guessed size
*/
static long int get_signal_stack_size()
{
# ifdef HAVE_SYSCONF_SIGSTKSZ
long int size = -1;
// return size only if sysconf doesn't return an error
if ((size = sysconf(_SC_SIGSTKSZ)) > -1)
return size;
# endif
# ifdef SIGSTKSZ
// if sysconf() isn't available or gives error, return SIGSTKSZ
// if defined
return SIGSTKSZ;
# endif
// otherwise guess the size
return 8000;
}
static char *signal_stack;
static void
init_signal_stack(void)
{
if (signal_stack != NULL)
{
# ifdef HAVE_SIGALTSTACK
# ifdef HAVE_SS_BASE
sigstk.ss_base = signal_stack;
# else
sigstk.ss_sp = signal_stack;
# endif
sigstk.ss_size = get_signal_stack_size();
sigstk.ss_flags = 0;
(void)sigaltstack(&sigstk, NULL);
# else
sigstk.ss_sp = signal_stack;
if (stack_grows_downwards)
sigstk.ss_sp += get_signal_stack_size() - 1;
sigstk.ss_onstack = 0;
(void)sigstack(&sigstk, NULL);
# endif
}
}
#endif
/*
* We need correct prototypes for a signal function, otherwise mean compilers
* will barf when the second argument to signal() is ``wrong''.
* Let me try it with a few tricky defines from my own osdef.h (jw).
*/
#if defined(SIGWINCH)
static void
sig_winch SIGDEFARG(sigarg)
{
// this is not required on all systems, but it doesn't hurt anybody
signal(SIGWINCH, (void (*)())sig_winch);
do_resize = TRUE;
}
#endif
#if defined(SIGTSTP)
static void
sig_tstp SIGDEFARG(sigarg)
{
// Second time we get called we actually need to suspend
if (in_mch_suspend)
{
signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
raise(sigarg);
}
else
got_tstp = TRUE;
#if !defined(__ANDROID__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
// This is not required on all systems. On some systems (at least Android,
// OpenBSD, and DragonFlyBSD) this breaks suspending with CTRL-Z.
signal(SIGTSTP, (void (*)())sig_tstp);
#endif
}
#endif
#if defined(SIGINT)
static void
catch_sigint SIGDEFARG(sigarg)
{
// this is not required on all systems, but it doesn't hurt anybody
signal(SIGINT, (void (*)())catch_sigint);
got_int = TRUE;
}
#endif
#if defined(SIGUSR1)
static void
catch_sigusr1 SIGDEFARG(sigarg)
{
// this is not required on all systems, but it doesn't hurt anybody
signal(SIGUSR1, (void (*)())catch_sigusr1);
got_sigusr1 = TRUE;
}
#endif
#if defined(SIGPWR)
static void
catch_sigpwr SIGDEFARG(sigarg)
{
// this is not required on all systems, but it doesn't hurt anybody
signal(SIGPWR, (void (*)())catch_sigpwr);
/*
* I'm not sure we get the SIGPWR signal when the system is really going
* down or when the batteries are almost empty. Just preserve the swap
* files and don't exit, that can't do any harm.
*/
ml_sync_all(FALSE, FALSE);
}
#endif
#ifdef SET_SIG_ALARM
/*
* signal function for alarm().
*/
static void
sig_alarm SIGDEFARG(sigarg)
{
// doesn't do anything, just to break a system call
sig_alarm_called = TRUE;
}
#endif
#if (defined(HAVE_SETJMP_H) \
&& ((defined(FEAT_X11) && defined(FEAT_XCLIPBOARD)) \
|| defined(FEAT_LIBCALL))) \
|| defined(PROTO)
# define USING_SETJMP 1
// argument to SETJMP()
static JMP_BUF lc_jump_env;
# ifdef SIGHASARG
// Caught signal number, 0 when no signal was caught; used for mch_libcall().
// Volatile because it is used in signal handlers.
static volatile sig_atomic_t lc_signal;
# endif
// TRUE when lc_jump_env is valid.
// Volatile because it is used in signal handler deathtrap().
static volatile sig_atomic_t lc_active INIT(= FALSE);
/*
* A simplistic version of setjmp() that only allows one level of using.
* Used to protect areas where we could crash.
* Don't call twice before calling mch_endjmp()!.
*
* Usage:
* mch_startjmp();
* if (SETJMP(lc_jump_env) != 0)
* {
* mch_didjmp();
* emsg("crash!");
* }
* else
* {
* do_the_work;
* mch_endjmp();
* }
* Note: Can't move SETJMP() here, because a function calling setjmp() must
* not return before the saved environment is used.
* Returns OK for normal return, FAIL when the protected code caused a
* problem and LONGJMP() was used.
*/
static void
mch_startjmp(void)
{
# ifdef SIGHASARG
lc_signal = 0;
# endif
lc_active = TRUE;
}
static void
mch_endjmp(void)
{
lc_active = FALSE;
}
static void