1513
1514
return ret_errno;
1519
* Based on the example in the GNU LibC manual chapter 13.13 "File
1520
* Descriptor Flags".
1521
| [[info:libc:Descriptor%20Flags][File Descriptor Flags]] |
1523
__attribute__((warn_unused_result))
1524
static int set_cloexec_flag(int fd){
1525
int ret = (int)TEMP_FAILURE_RETRY(fcntl(fd, F_GETFD, 0));
1526
/* If reading the flags failed, return error indication now. */
1530
/* Store modified flag word in the descriptor. */
1531
return (int)TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD,
1534
#endif /* not O_CLOEXEC */
1516
1536
__attribute__((nonnull))
1517
1537
void run_network_hooks(const char *mode, const char *interface,
1518
1538
const float delay){
1519
1539
struct dirent **direntries;
1520
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1540
if(hookdir_fd == -1){
1541
hookdir_fd = open(hookdir, O_RDONLY |
1544
#else /* not O_CLOEXEC */
1546
#endif /* not O_CLOEXEC */
1548
if(hookdir_fd == -1){
1549
if(errno == ENOENT){
1551
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1552
" found\n", hookdir);
1555
perror_plus("open");
1560
if(set_cloexec_flag(hookdir_fd) < 0){
1561
perror_plus("set_cloexec_flag");
1562
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1563
perror_plus("close");
1569
#endif /* not O_CLOEXEC */
1572
#if __GLIBC_PREREQ(2, 15)
1573
int numhooks = scandirat(hookdir_fd, ".", &direntries,
1574
runnable_hook, alphasort);
1575
#else /* not __GLIBC_PREREQ(2, 15) */
1576
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1578
#endif /* not __GLIBC_PREREQ(2, 15) */
1579
#else /* not __GLIBC__ */
1580
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1582
#endif /* not __GLIBC__ */
1522
1583
if(numhooks == -1){
1523
if(errno == ENOENT){
1525
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1526
" found\n", hookdir);
1529
perror_plus("scandir");
1532
struct dirent *direntry;
1534
int devnull = open("/dev/null", O_RDONLY);
1535
for(int i = 0; i < numhooks; i++){
1536
direntry = direntries[i];
1537
char *fullname = NULL;
1538
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1584
perror_plus("scandir");
1587
struct dirent *direntry;
1589
int devnull = open("/dev/null", O_RDONLY);
1590
for(int i = 0; i < numhooks; i++){
1591
direntry = direntries[i];
1592
char *fullname = NULL;
1593
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1595
perror_plus("asprintf");
1599
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1602
pid_t hook_pid = fork();
1605
/* Raise privileges */
1606
if(raise_privileges_permanently() != 0){
1607
perror_plus("Failed to raise privileges");
1614
perror_plus("setgid");
1617
/* Reset supplementary groups */
1619
ret = setgroups(0, NULL);
1621
perror_plus("setgroups");
1624
ret = dup2(devnull, STDIN_FILENO);
1626
perror_plus("dup2(devnull, STDIN_FILENO)");
1629
ret = close(devnull);
1631
perror_plus("close");
1634
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1636
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1639
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1641
perror_plus("setenv");
1644
ret = setenv("DEVICE", interface, 1);
1646
perror_plus("setenv");
1649
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1651
perror_plus("setenv");
1654
ret = setenv("MODE", mode, 1);
1656
perror_plus("setenv");
1660
ret = asprintf(&delaystring, "%f", (double)delay);
1540
1662
perror_plus("asprintf");
1544
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1547
pid_t hook_pid = fork();
1550
/* Raise privileges */
1551
if(raise_privileges_permanently() != 0){
1552
perror_plus("Failed to raise privileges");
1559
perror_plus("setgid");
1562
/* Reset supplementary groups */
1564
ret = setgroups(0, NULL);
1566
perror_plus("setgroups");
1569
ret = dup2(devnull, STDIN_FILENO);
1571
perror_plus("dup2(devnull, STDIN_FILENO)");
1574
ret = close(devnull);
1576
perror_plus("close");
1579
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1581
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1584
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1586
perror_plus("setenv");
1589
ret = setenv("DEVICE", interface, 1);
1591
perror_plus("setenv");
1594
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1596
perror_plus("setenv");
1599
ret = setenv("MODE", mode, 1);
1601
perror_plus("setenv");
1605
ret = asprintf(&delaystring, "%f", (double)delay);
1607
perror_plus("asprintf");
1610
ret = setenv("DELAY", delaystring, 1);
1613
perror_plus("setenv");
1665
ret = setenv("DELAY", delaystring, 1);
1616
1667
free(delaystring);
1617
if(connect_to != NULL){
1618
ret = setenv("CONNECT", connect_to, 1);
1620
perror_plus("setenv");
1624
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1625
perror_plus("execl");
1626
_exit(EXIT_FAILURE);
1668
perror_plus("setenv");
1672
if(connect_to != NULL){
1673
ret = setenv("CONNECT", connect_to, 1);
1675
perror_plus("setenv");
1679
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1680
perror_plus("execl");
1681
_exit(EXIT_FAILURE);
1685
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1686
perror_plus("waitpid");
1690
if(WIFEXITED(status)){
1691
if(WEXITSTATUS(status) != 0){
1692
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1693
" with status %d\n", direntry->d_name,
1694
WEXITSTATUS(status));
1698
} else if(WIFSIGNALED(status)){
1699
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1700
" signal %d\n", direntry->d_name,
1630
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1631
perror_plus("waitpid");
1635
if(WIFEXITED(status)){
1636
if(WEXITSTATUS(status) != 0){
1637
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1638
" with status %d\n", direntry->d_name,
1639
WEXITSTATUS(status));
1643
} else if(WIFSIGNALED(status)){
1644
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1645
" signal %d\n", direntry->d_name,
1650
fprintf_plus(stderr, "Warning: network hook \"%s\""
1651
" crashed\n", direntry->d_name);
1658
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1705
fprintf_plus(stderr, "Warning: network hook \"%s\""
1706
" crashed\n", direntry->d_name);
1713
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1717
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1718
perror_plus("close");
1666
1725
__attribute__((nonnull, warn_unused_result))