comparison src/os_unix.c @ 32425:c0b3e3c7786c v9.0.1544

patch 9.0.1544: recent glibc marks sigset() as a deprecated Commit: https://github.com/vim/vim/commit/378447fc183b589039a5bf257923a86d439b0a91 Author: ichizok <gclient.gaap@gmail.com> Date: Thu May 11 22:25:42 2023 +0100 patch 9.0.1544: recent glibc marks sigset() as a deprecated Problem: Recent glibc marks sigset() as a deprecated. Solution: Use sigaction() in mch_signal() if possible. (Ozaki Kiichi, closes #12373)
author Bram Moolenaar <Bram@vim.org>
date Thu, 11 May 2023 23:30:04 +0200
parents ca4e81de477a
children 87f59a64efab
comparison
equal deleted inserted replaced
32424:620f87399687 32425:c0b3e3c7786c
212 static int have_dollars(int, char_u **); 212 static int have_dollars(int, char_u **);
213 213
214 static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file); 214 static int save_patterns(int num_pat, char_u **pat, int *num_file, char_u ***file);
215 215
216 #ifndef SIG_ERR 216 #ifndef SIG_ERR
217 # define SIG_ERR ((void (*)())-1) 217 # define SIG_ERR ((sighandler_T)-1)
218 #endif
219 #ifndef SIG_HOLD
220 # define SIG_HOLD ((sighandler_T)-2)
218 #endif 221 #endif
219 222
220 // volatile because it is used in signal handler sig_winch(). 223 // volatile because it is used in signal handler sig_winch().
221 static volatile sig_atomic_t do_resize = FALSE; 224 static volatile sig_atomic_t do_resize = FALSE;
222 // volatile because it is used in signal handler sig_tstp(). 225 // volatile because it is used in signal handler sig_tstp().
338 {SIGPIPE, "PIPE", FALSE}, 341 {SIGPIPE, "PIPE", FALSE},
339 #endif 342 #endif
340 {-1, "Unknown!", FALSE} 343 {-1, "Unknown!", FALSE}
341 }; 344 };
342 345
346 sighandler_T
347 mch_signal(int sig, sighandler_T func)
348 {
349 #if defined(HAVE_SIGACTION) && defined(HAVE_SIGPROCMASK)
350 // Modern implementation: use sigaction().
351 struct sigaction sa, old;
352 sigset_t curset;
353 int blocked;
354
355 if (sigprocmask(SIG_BLOCK, NULL, &curset) == -1)
356 return SIG_ERR;
357
358 blocked = sigismember(&curset, sig);
359
360 if (func == SIG_HOLD)
361 {
362 if (blocked)
363 return SIG_HOLD;
364
365 sigemptyset(&curset);
366 sigaddset(&curset, sig);
367
368 if (sigaction(sig, NULL, &old) == -1
369 || sigprocmask(SIG_BLOCK, &curset, NULL) == -1)
370 return SIG_ERR;
371 return old.sa_handler;
372 }
373
374 if (blocked)
375 {
376 sigemptyset(&curset);
377 sigaddset(&curset, sig);
378
379 if (sigprocmask(SIG_UNBLOCK, &curset, NULL) == -1)
380 return SIG_ERR;
381 }
382
383 sa.sa_handler = func;
384 sigemptyset(&sa.sa_mask);
385 # ifdef SA_RESTART
386 sa.sa_flags = SA_RESTART;
387 # else
388 sa.sa_flags = 0;
389 # endif
390 if (sigaction(sig, &sa, &old) == -1)
391 return SIG_ERR;
392 return blocked ? SIG_HOLD: old.sa_handler;
393 #elif defined(HAVE_SIGSET)
394 // Using sigset() is preferred above signal().
395 return sigset(sig, func);
396 #else
397 // Oldest and most compatible solution.
398 return signal(sig, func);
399 #endif
400 }
401
343 int 402 int
344 mch_chdir(char *path) 403 mch_chdir(char *path)
345 { 404 {
346 if (p_verbose >= 5) 405 if (p_verbose >= 5)
347 { 406 {
348 verbose_enter(); 407 verbose_enter();
349 smsg("chdir(%s)", path); 408 smsg("chdir(%s)", path);
350 verbose_leave(); 409 verbose_leave();
351 } 410 }
352 # ifdef VMS 411 #ifdef VMS
353 return chdir(vms_fixfilename(path)); 412 return chdir(vms_fixfilename(path));
354 # else 413 #else
355 return chdir(path); 414 return chdir(path);
356 # endif 415 #endif
357 } 416 }
358 417
359 // Why is NeXT excluded here (and not in os_unixx.h)? 418 // Why is NeXT excluded here (and not in os_unixx.h)?
360 #if defined(ECHOE) && defined(ICANON) \ 419 #if defined(ECHOE) && defined(ICANON) \
361 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \ 420 && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) \
867 #if defined(SIGWINCH) 926 #if defined(SIGWINCH)
868 static void 927 static void
869 sig_winch SIGDEFARG(sigarg) 928 sig_winch SIGDEFARG(sigarg)
870 { 929 {
871 // this is not required on all systems, but it doesn't hurt anybody 930 // this is not required on all systems, but it doesn't hurt anybody
872 signal(SIGWINCH, (void (*)(int))sig_winch); 931 mch_signal(SIGWINCH, sig_winch);
873 do_resize = TRUE; 932 do_resize = TRUE;
874 } 933 }
875 #endif 934 #endif
876 935
877 #if defined(SIGTSTP) 936 #if defined(SIGTSTP)
879 sig_tstp SIGDEFARG(sigarg) 938 sig_tstp SIGDEFARG(sigarg)
880 { 939 {
881 // Second time we get called we actually need to suspend 940 // Second time we get called we actually need to suspend
882 if (in_mch_suspend) 941 if (in_mch_suspend)
883 { 942 {
884 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL); 943 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN : SIG_DFL);
885 raise(sigarg); 944 raise(sigarg);
886 } 945 }
887 else 946 else
888 got_tstp = TRUE; 947 got_tstp = TRUE;
889 948
890 #if !defined(__ANDROID__) && !defined(__OpenBSD__) && !defined(__DragonFly__) 949 #if !defined(__ANDROID__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
891 // This is not required on all systems. On some systems (at least Android, 950 // This is not required on all systems. On some systems (at least Android,
892 // OpenBSD, and DragonFlyBSD) this breaks suspending with CTRL-Z. 951 // OpenBSD, and DragonFlyBSD) this breaks suspending with CTRL-Z.
893 signal(SIGTSTP, (void (*)(int))sig_tstp); 952 mch_signal(SIGTSTP, sig_tstp);
894 #endif 953 #endif
895 } 954 }
896 #endif 955 #endif
897 956
898 #if defined(SIGINT) 957 #if defined(SIGINT)
899 static void 958 static void
900 catch_sigint SIGDEFARG(sigarg) 959 catch_sigint SIGDEFARG(sigarg)
901 { 960 {
902 // this is not required on all systems, but it doesn't hurt anybody 961 // this is not required on all systems, but it doesn't hurt anybody
903 signal(SIGINT, (void (*)(int))catch_sigint); 962 mch_signal(SIGINT, catch_sigint);
904 got_int = TRUE; 963 got_int = TRUE;
905 } 964 }
906 #endif 965 #endif
907 966
908 #if defined(SIGUSR1) 967 #if defined(SIGUSR1)
909 static void 968 static void
910 catch_sigusr1 SIGDEFARG(sigarg) 969 catch_sigusr1 SIGDEFARG(sigarg)
911 { 970 {
912 // this is not required on all systems, but it doesn't hurt anybody 971 // this is not required on all systems, but it doesn't hurt anybody
913 signal(SIGUSR1, (void (*)(int))catch_sigusr1); 972 mch_signal(SIGUSR1, catch_sigusr1);
914 got_sigusr1 = TRUE; 973 got_sigusr1 = TRUE;
915 } 974 }
916 #endif 975 #endif
917 976
918 #if defined(SIGPWR) 977 #if defined(SIGPWR)
919 static void 978 static void
920 catch_sigpwr SIGDEFARG(sigarg) 979 catch_sigpwr SIGDEFARG(sigarg)
921 { 980 {
922 // this is not required on all systems, but it doesn't hurt anybody 981 // this is not required on all systems, but it doesn't hurt anybody
923 signal(SIGPWR, (void (*)())catch_sigpwr); 982 mch_signal(SIGPWR, catch_sigpwr);
924 /* 983 /*
925 * I'm not sure we get the SIGPWR signal when the system is really going 984 * I'm not sure we get the SIGPWR signal when the system is really going
926 * down or when the batteries are almost empty. Just preserve the swap 985 * down or when the batteries are almost empty. Just preserve the swap
927 * files and don't exit, that can't do any harm. 986 * files and don't exit, that can't do any harm.
928 */ 987 */
1362 #ifdef SIGTSTP 1421 #ifdef SIGTSTP
1363 // Check whether we were invoked with SIGTSTP set to be ignored. If it is 1422 // Check whether we were invoked with SIGTSTP set to be ignored. If it is
1364 // that indicates the shell (or program) that launched us does not support 1423 // that indicates the shell (or program) that launched us does not support
1365 // tty job control and thus we should ignore that signal. If invoked as a 1424 // tty job control and thus we should ignore that signal. If invoked as a
1366 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored. 1425 // restricted editor (e.g., as "rvim") SIGTSTP is always ignored.
1367 ignore_sigtstp = restricted || SIG_IGN == signal(SIGTSTP, SIG_ERR); 1426 ignore_sigtstp = restricted || SIG_IGN == mch_signal(SIGTSTP, SIG_ERR);
1368 #endif 1427 #endif
1369 set_signals(); 1428 set_signals();
1370 1429
1371 #ifdef MACOS_CONVERT 1430 #ifdef MACOS_CONVERT
1372 mac_conv_init(); 1431 mac_conv_init();
1381 { 1440 {
1382 #if defined(SIGWINCH) 1441 #if defined(SIGWINCH)
1383 /* 1442 /*
1384 * WINDOW CHANGE signal is handled with sig_winch(). 1443 * WINDOW CHANGE signal is handled with sig_winch().
1385 */ 1444 */
1386 signal(SIGWINCH, (void (*)(int))sig_winch); 1445 mch_signal(SIGWINCH, sig_winch);
1387 #endif 1446 #endif
1388 1447
1389 #ifdef SIGTSTP 1448 #ifdef SIGTSTP
1390 // See mch_init() for the conditions under which we ignore SIGTSTP. 1449 // See mch_init() for the conditions under which we ignore SIGTSTP.
1391 // In the GUI default TSTP processing is OK. 1450 // In the GUI default TSTP processing is OK.
1392 // Checking both gui.in_use and gui.starting because gui.in_use is not set 1451 // Checking both gui.in_use and gui.starting because gui.in_use is not set
1393 // at this point (set after menus are displayed), but gui.starting is set. 1452 // at this point (set after menus are displayed), but gui.starting is set.
1394 signal(SIGTSTP, ignore_sigtstp ? SIG_IGN 1453 mch_signal(SIGTSTP, ignore_sigtstp ? SIG_IGN
1395 # ifdef FEAT_GUI 1454 # ifdef FEAT_GUI
1396 : gui.in_use || gui.starting ? SIG_DFL 1455 : gui.in_use || gui.starting ? SIG_DFL
1397 # endif 1456 # endif
1398 : (void (*)(int))sig_tstp); 1457 : sig_tstp);
1399 #endif 1458 #endif
1400 #if defined(SIGCONT) 1459 #if defined(SIGCONT)
1401 signal(SIGCONT, sigcont_handler); 1460 mch_signal(SIGCONT, sigcont_handler);
1402 #endif 1461 #endif
1403 #ifdef SIGPIPE 1462 #ifdef SIGPIPE
1404 /* 1463 /*
1405 * We want to ignore breaking of PIPEs. 1464 * We want to ignore breaking of PIPEs.
1406 */ 1465 */
1407 signal(SIGPIPE, SIG_IGN); 1466 mch_signal(SIGPIPE, SIG_IGN);
1408 #endif 1467 #endif
1409 1468
1410 #ifdef SIGINT 1469 #ifdef SIGINT
1411 catch_int_signal(); 1470 catch_int_signal();
1412 #endif 1471 #endif
1413 1472
1414 #ifdef SIGUSR1 1473 #ifdef SIGUSR1
1415 /* 1474 /*
1416 * Call user's handler on SIGUSR1 1475 * Call user's handler on SIGUSR1
1417 */ 1476 */
1418 signal(SIGUSR1, (void (*)(int))catch_sigusr1); 1477 mch_signal(SIGUSR1, catch_sigusr1);
1419 #endif 1478 #endif
1420 1479
1421 /* 1480 /*
1422 * Ignore alarm signals (Perl's alarm() generates it). 1481 * Ignore alarm signals (Perl's alarm() generates it).
1423 */ 1482 */
1424 #ifdef SIGALRM 1483 #ifdef SIGALRM
1425 signal(SIGALRM, SIG_IGN); 1484 mch_signal(SIGALRM, SIG_IGN);
1426 #endif 1485 #endif
1427 1486
1428 #ifdef SIGPWR 1487 #ifdef SIGPWR
1429 /* 1488 /*
1430 * Catch SIGPWR (power failure?) to preserve the swap files, so that no 1489 * Catch SIGPWR (power failure?) to preserve the swap files, so that no
1431 * work will be lost. 1490 * work will be lost.
1432 */ 1491 */
1433 signal(SIGPWR, (void (*)())catch_sigpwr); 1492 mch_signal(SIGPWR, catch_sigpwr);
1434 #endif 1493 #endif
1435 1494
1436 /* 1495 /*
1437 * Arrange for other signals to gracefully shutdown Vim. 1496 * Arrange for other signals to gracefully shutdown Vim.
1438 */ 1497 */
1441 #if defined(FEAT_GUI) && defined(SIGHUP) 1500 #if defined(FEAT_GUI) && defined(SIGHUP)
1442 /* 1501 /*
1443 * When the GUI is running, ignore the hangup signal. 1502 * When the GUI is running, ignore the hangup signal.
1444 */ 1503 */
1445 if (gui.in_use) 1504 if (gui.in_use)
1446 signal(SIGHUP, SIG_IGN); 1505 mch_signal(SIGHUP, SIG_IGN);
1447 #endif 1506 #endif
1448 } 1507 }
1449 1508
1450 #if defined(SIGINT) || defined(PROTO) 1509 #if defined(SIGINT) || defined(PROTO)
1451 /* 1510 /*
1452 * Catch CTRL-C (only works while in Cooked mode). 1511 * Catch CTRL-C (only works while in Cooked mode).
1453 */ 1512 */
1454 static void 1513 static void
1455 catch_int_signal(void) 1514 catch_int_signal(void)
1456 { 1515 {
1457 signal(SIGINT, (void (*)(int))catch_sigint); 1516 mch_signal(SIGINT, catch_sigint);
1458 } 1517 }
1459 #endif 1518 #endif
1460 1519
1461 void 1520 void
1462 reset_signals(void) 1521 reset_signals(void)
1463 { 1522 {
1464 catch_signals(SIG_DFL, SIG_DFL); 1523 catch_signals(SIG_DFL, SIG_DFL);
1465 #if defined(SIGCONT) 1524 #if defined(SIGCONT)
1466 // SIGCONT isn't in the list, because its default action is ignore 1525 // SIGCONT isn't in the list, because its default action is ignore
1467 signal(SIGCONT, SIG_DFL); 1526 mch_signal(SIGCONT, SIG_DFL);
1468 #endif 1527 #endif
1469 } 1528 }
1470 1529
1471 static void 1530 static void
1472 catch_signals( 1531 catch_signals(
1504 sv.sv_handler = func_deadly; 1563 sv.sv_handler = func_deadly;
1505 sv.sv_mask = 0; 1564 sv.sv_mask = 0;
1506 sv.sv_flags = SV_ONSTACK; 1565 sv.sv_flags = SV_ONSTACK;
1507 sigvec(signal_info[i].sig, &sv, NULL); 1566 sigvec(signal_info[i].sig, &sv, NULL);
1508 # else 1567 # else
1509 signal(signal_info[i].sig, func_deadly); 1568 mch_signal(signal_info[i].sig, func_deadly);
1510 # endif 1569 # endif
1511 #endif 1570 #endif
1512 } 1571 }
1513 else if (func_other != SIG_ERR) 1572 else if (func_other != SIG_ERR)
1514 { 1573 {
1515 // Deal with non-deadly signals. 1574 // Deal with non-deadly signals.
1516 #ifdef SIGTSTP 1575 #ifdef SIGTSTP
1517 signal(signal_info[i].sig, 1576 mch_signal(signal_info[i].sig,
1518 signal_info[i].sig == SIGTSTP && ignore_sigtstp 1577 signal_info[i].sig == SIGTSTP && ignore_sigtstp
1519 ? SIG_IGN : func_other); 1578 ? SIG_IGN : func_other);
1520 #else 1579 #else
1521 signal(signal_info[i].sig, func_other); 1580 mch_signal(signal_info[i].sig, func_other);
1522 #endif 1581 #endif
1523 } 1582 }
1524 } 1583 }
1525 } 1584 }
1526 1585
1921 return result; // Don't do all these X calls again 1980 return result; // Don't do all these X calls again
1922 1981
1923 if (x11_window != 0 && x11_display == NULL) 1982 if (x11_window != 0 && x11_display == NULL)
1924 { 1983 {
1925 #ifdef SET_SIG_ALARM 1984 #ifdef SET_SIG_ALARM
1926 void (*sig_save)(); 1985 sighandler_T sig_save;
1927 #endif 1986 #endif
1928 #ifdef ELAPSED_FUNC 1987 #ifdef ELAPSED_FUNC
1929 elapsed_T start_tv; 1988 elapsed_T start_tv;
1930 1989
1931 if (p_verbose > 0) 1990 if (p_verbose > 0)
1936 /* 1995 /*
1937 * Opening the Display may hang if the DISPLAY setting is wrong, or 1996 * Opening the Display may hang if the DISPLAY setting is wrong, or
1938 * the network connection is bad. Set an alarm timer to get out. 1997 * the network connection is bad. Set an alarm timer to get out.
1939 */ 1998 */
1940 sig_alarm_called = FALSE; 1999 sig_alarm_called = FALSE;
1941 sig_save = (void (*)())signal(SIGALRM, (void (*)())sig_alarm); 2000 sig_save = mch_signal(SIGALRM, sig_alarm);
1942 alarm(2); 2001 alarm(2);
1943 #endif 2002 #endif
1944 x11_display = XOpenDisplay(NULL); 2003 x11_display = XOpenDisplay(NULL);
1945 2004
1946 #ifdef SET_SIG_ALARM 2005 #ifdef SET_SIG_ALARM
1947 alarm(0); 2006 alarm(0);
1948 signal(SIGALRM, (void (*)())sig_save); 2007 mch_signal(SIGALRM, sig_save);
1949 if (p_verbose > 0 && sig_alarm_called) 2008 if (p_verbose > 0 && sig_alarm_called)
1950 verb_msg(_("Opening the X display timed out")); 2009 verb_msg(_("Opening the X display timed out"));
1951 #endif 2010 #endif
1952 if (x11_display != NULL) 2011 if (x11_display != NULL)
1953 { 2012 {
3517 static void 3576 static void
3518 may_core_dump(void) 3577 may_core_dump(void)
3519 { 3578 {
3520 if (deadly_signal != 0) 3579 if (deadly_signal != 0)
3521 { 3580 {
3522 signal(deadly_signal, SIG_DFL); 3581 mch_signal(deadly_signal, SIG_DFL);
3523 kill(getpid(), deadly_signal); // Die using the signal we caught 3582 kill(getpid(), deadly_signal); // Die using the signal we caught
3524 } 3583 }
3525 } 3584 }
3526 3585
3527 #ifndef VMS 3586 #ifndef VMS
4826 # if defined(SIGHUP) 4885 # if defined(SIGHUP)
4827 // When doing "!xterm&" and 'shell' is bash: the shell 4886 // When doing "!xterm&" and 'shell' is bash: the shell
4828 // will exit and send SIGHUP to all processes in its 4887 // will exit and send SIGHUP to all processes in its
4829 // group, killing the just started process. Ignore SIGHUP 4888 // group, killing the just started process. Ignore SIGHUP
4830 // to avoid that. (suggested by Simon Schubert) 4889 // to avoid that. (suggested by Simon Schubert)
4831 signal(SIGHUP, SIG_IGN); 4890 mch_signal(SIGHUP, SIG_IGN);
4832 # endif 4891 # endif
4833 } 4892 }
4834 # endif 4893 # endif
4835 # ifdef FEAT_GUI 4894 # ifdef FEAT_GUI
4836 if (pty_slave_fd >= 0) 4895 if (pty_slave_fd >= 0)
7281 // gpm library tries to handling TSTP causes 7340 // gpm library tries to handling TSTP causes
7282 // problems. Anyways, we close connection to Gpm whenever 7341 // problems. Anyways, we close connection to Gpm whenever
7283 // we are going to suspend or starting an external process 7342 // we are going to suspend or starting an external process
7284 // so we shouldn't have problem with this 7343 // so we shouldn't have problem with this
7285 # ifdef SIGTSTP 7344 # ifdef SIGTSTP
7286 signal(SIGTSTP, restricted ? SIG_IGN : (void (*)())sig_tstp); 7345 mch_signal(SIGTSTP, restricted ? SIG_IGN : sig_tstp);
7287 # endif 7346 # endif
7288 return 1; // succeed 7347 return 1; // succeed
7289 } 7348 }
7290 if (gpm_fd == -2) 7349 if (gpm_fd == -2)
7291 Gpm_Close(); // We don't want to talk to xterm via gpm 7350 Gpm_Close(); // We don't want to talk to xterm via gpm
7413 mouse.u.mode.mode = 0; 7472 mouse.u.mode.mode = 0;
7414 mouse.u.mode.signal = SIGUSR2; 7473 mouse.u.mode.signal = SIGUSR2;
7415 if (ioctl(1, CONS_MOUSECTL, &mouse) == -1) 7474 if (ioctl(1, CONS_MOUSECTL, &mouse) == -1)
7416 return FAIL; 7475 return FAIL;
7417 7476
7418 signal(SIGUSR2, (void (*)())sig_sysmouse); 7477 mch_signal(SIGUSR2, sig_sysmouse);
7419 mouse.operation = MOUSE_SHOW; 7478 mouse.operation = MOUSE_SHOW;
7420 ioctl(1, CONS_MOUSECTL, &mouse); 7479 ioctl(1, CONS_MOUSECTL, &mouse);
7421 return OK; 7480 return OK;
7422 } 7481 }
7423 7482
7428 static void 7487 static void
7429 sysmouse_close(void) 7488 sysmouse_close(void)
7430 { 7489 {
7431 struct mouse_info mouse; 7490 struct mouse_info mouse;
7432 7491
7433 signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL); 7492 mch_signal(SIGUSR2, restricted ? SIG_IGN : SIG_DFL);
7434 mouse.operation = MOUSE_MODE; 7493 mouse.operation = MOUSE_MODE;
7435 mouse.u.mode.mode = 0; 7494 mouse.u.mode.mode = 0;
7436 mouse.u.mode.signal = 0; 7495 mouse.u.mode.signal = 0;
7437 ioctl(1, CONS_MOUSECTL, &mouse); 7496 ioctl(1, CONS_MOUSECTL, &mouse);
7438 } 7497 }