1380
1454
ret = avahi_simple_poll_iterate(s, (int)block_time);
1383
if (ret > 0 or errno != EINTR){
1457
if(ret > 0 or errno != EINTR){
1384
1458
return (ret != 1) ? ret : 0;
1390
bool run_network_hooks(const char *mode, const char *interface,
1464
/* Set effective uid to 0, return errno */
1465
__attribute__((warn_unused_result))
1466
error_t raise_privileges(void){
1467
error_t old_errno = errno;
1468
error_t ret_errno = 0;
1469
if(seteuid(0) == -1){
1476
/* Set effective and real user ID to 0. Return errno. */
1477
__attribute__((warn_unused_result))
1478
error_t raise_privileges_permanently(void){
1479
error_t old_errno = errno;
1480
error_t ret_errno = raise_privileges();
1485
if(setuid(0) == -1){
1492
/* Set effective user ID to unprivileged saved user ID */
1493
__attribute__((warn_unused_result))
1494
error_t lower_privileges(void){
1495
error_t old_errno = errno;
1496
error_t ret_errno = 0;
1497
if(seteuid(uid) == -1){
1504
/* Lower privileges permanently */
1505
__attribute__((warn_unused_result))
1506
error_t lower_privileges_permanently(void){
1507
error_t old_errno = errno;
1508
error_t ret_errno = 0;
1509
if(setuid(uid) == -1){
1516
__attribute__((nonnull))
1517
void run_network_hooks(const char *mode, const char *interface,
1391
1518
const float delay){
1392
struct dirent **direntries;
1393
struct dirent *direntry;
1395
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1519
struct dirent **direntries = NULL;
1520
if(hookdir_fd == -1){
1521
hookdir_fd = open(hookdir, O_RDONLY);
1522
if(hookdir_fd == -1){
1523
if(errno == ENOENT){
1525
fprintf_plus(stderr, "Network hook directory \"%s\" not"
1526
" found\n", hookdir);
1529
perror_plus("open");
1535
#if __GLIBC_PREREQ(2, 15)
1536
int numhooks = scandirat(hookdir_fd, ".", &direntries,
1537
runnable_hook, alphasort);
1538
#else /* not __GLIBC_PREREQ(2, 15) */
1539
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1541
#endif /* not __GLIBC_PREREQ(2, 15) */
1542
#else /* not __GLIBC__ */
1543
int numhooks = scandir(hookdir, &direntries, runnable_hook,
1545
#endif /* not __GLIBC__ */
1397
1546
if(numhooks == -1){
1398
1547
perror_plus("scandir");
1400
int devnull = open("/dev/null", O_RDONLY);
1401
for(int i = 0; i < numhooks; i++){
1402
direntry = direntries[i];
1403
char *fullname = NULL;
1404
ret = asprintf(&fullname, "%s/%s", hookdir, direntry->d_name);
1550
struct dirent *direntry;
1552
int devnull = open("/dev/null", O_RDONLY);
1553
for(int i = 0; i < numhooks; i++){
1554
direntry = direntries[i];
1556
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1559
pid_t hook_pid = fork();
1562
/* Raise privileges */
1563
errno = raise_privileges_permanently();
1565
perror_plus("Failed to raise privileges");
1572
perror_plus("setgid");
1575
/* Reset supplementary groups */
1577
ret = setgroups(0, NULL);
1579
perror_plus("setgroups");
1582
ret = dup2(devnull, STDIN_FILENO);
1584
perror_plus("dup2(devnull, STDIN_FILENO)");
1587
ret = close(devnull);
1589
perror_plus("close");
1592
ret = dup2(STDERR_FILENO, STDOUT_FILENO);
1594
perror_plus("dup2(STDERR_FILENO, STDOUT_FILENO)");
1597
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1599
perror_plus("setenv");
1602
ret = setenv("DEVICE", interface, 1);
1604
perror_plus("setenv");
1607
ret = setenv("VERBOSITY", debug ? "1" : "0", 1);
1609
perror_plus("setenv");
1612
ret = setenv("MODE", mode, 1);
1614
perror_plus("setenv");
1618
ret = asprintf(&delaystring, "%f", (double)delay);
1406
1620
perror_plus("asprintf");
1410
fprintf_plus(stderr, "Running network hook \"%s\"\n",
1413
pid_t hook_pid = fork();
1416
/* Raise privileges */
1420
perror_plus("seteuid");
1422
/* Raise privileges even more */
1426
perror_plus("setuid");
1432
perror_plus("setgid");
1434
/* Reset supplementary groups */
1436
ret = setgroups(0, NULL);
1438
perror_plus("setgroups");
1440
dup2(devnull, STDIN_FILENO);
1442
dup2(STDERR_FILENO, STDOUT_FILENO);
1443
ret = setenv("MANDOSNETHOOKDIR", hookdir, 1);
1445
perror_plus("setenv");
1448
ret = setenv("DEVICE", interface, 1);
1450
perror_plus("setenv");
1453
ret = setenv("VERBOSE", debug ? "1" : "0", 1);
1455
perror_plus("setenv");
1458
ret = setenv("MODE", mode, 1);
1460
perror_plus("setenv");
1464
ret = asprintf(&delaystring, "%f", delay);
1466
perror_plus("asprintf");
1469
ret = setenv("DELAY", delaystring, 1);
1472
perror_plus("setenv");
1623
ret = setenv("DELAY", delaystring, 1);
1475
1625
free(delaystring);
1476
if(execl(fullname, direntry->d_name, mode, NULL) == -1){
1477
perror_plus("execl");
1478
_exit(EXIT_FAILURE);
1482
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1483
perror_plus("waitpid");
1487
if(WIFEXITED(status)){
1488
if(WEXITSTATUS(status) != 0){
1489
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1490
" with status %d\n", direntry->d_name,
1491
WEXITSTATUS(status));
1495
} else if(WIFSIGNALED(status)){
1496
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1497
" signal %d\n", direntry->d_name,
1502
fprintf_plus(stderr, "Warning: network hook \"%s\""
1503
" crashed\n", direntry->d_name);
1510
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1626
perror_plus("setenv");
1630
if(connect_to != NULL){
1631
ret = setenv("CONNECT", connect_to, 1);
1633
perror_plus("setenv");
1637
int hook_fd = openat(hookdir_fd, direntry->d_name, O_RDONLY);
1639
perror_plus("openat");
1640
_exit(EXIT_FAILURE);
1642
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1643
perror_plus("close");
1644
_exit(EXIT_FAILURE);
1646
if(fexecve(hook_fd, (char *const []){ direntry->d_name, NULL },
1648
perror_plus("fexecve");
1649
_exit(EXIT_FAILURE);
1653
if(TEMP_FAILURE_RETRY(waitpid(hook_pid, &status, 0)) == -1){
1654
perror_plus("waitpid");
1657
if(WIFEXITED(status)){
1658
if(WEXITSTATUS(status) != 0){
1659
fprintf_plus(stderr, "Warning: network hook \"%s\" exited"
1660
" with status %d\n", direntry->d_name,
1661
WEXITSTATUS(status));
1664
} else if(WIFSIGNALED(status)){
1665
fprintf_plus(stderr, "Warning: network hook \"%s\" died by"
1666
" signal %d\n", direntry->d_name,
1670
fprintf_plus(stderr, "Warning: network hook \"%s\""
1671
" crashed\n", direntry->d_name);
1676
fprintf_plus(stderr, "Network hook \"%s\" ran successfully\n",
1681
if((int)TEMP_FAILURE_RETRY(close(hookdir_fd)) == -1){
1682
perror_plus("close");
1689
__attribute__((nonnull, warn_unused_result))
1690
error_t bring_up_interface(const char *const interface,
1692
error_t old_errno = errno;
1694
struct ifreq network;
1695
unsigned int if_index = if_nametoindex(interface);
1697
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1707
if(not interface_is_up(interface)){
1708
error_t ret_errno = 0, ioctl_errno = 0;
1709
if(not get_flags(interface, &network)){
1711
fprintf_plus(stderr, "Failed to get flags for interface "
1712
"\"%s\"\n", interface);
1716
network.ifr_flags |= IFF_UP; /* set flag */
1718
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1721
perror_plus("socket");
1727
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1729
perror_plus("close");
1736
fprintf_plus(stderr, "Bringing up interface \"%s\"\n",
1740
/* Raise privileges */
1741
ret_errno = raise_privileges();
1744
perror_plus("Failed to raise privileges");
1749
bool restore_loglevel = false;
1751
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1752
messages about the network interface to mess up the prompt */
1753
ret_linux = klogctl(8, NULL, 5);
1754
if(ret_linux == -1){
1755
perror_plus("klogctl");
1757
restore_loglevel = true;
1760
#endif /* __linux__ */
1761
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1762
ioctl_errno = errno;
1764
if(restore_loglevel){
1765
ret_linux = klogctl(7, NULL, 0);
1766
if(ret_linux == -1){
1767
perror_plus("klogctl");
1770
#endif /* __linux__ */
1772
/* If raise_privileges() succeeded above */
1774
/* Lower privileges */
1775
ret_errno = lower_privileges();
1778
perror_plus("Failed to lower privileges");
1782
/* Close the socket */
1783
ret = (int)TEMP_FAILURE_RETRY(close(sd));
1785
perror_plus("close");
1788
if(ret_setflags == -1){
1789
errno = ioctl_errno;
1790
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1795
fprintf_plus(stderr, "Interface \"%s\" is already up; good\n",
1799
/* Sleep checking until interface is running.
1800
Check every 0.25s, up to total time of delay */
1801
for(int i=0; i < delay * 4; i++){
1802
if(interface_is_running(interface)){
1805
struct timespec sleeptime = { .tv_nsec = 250000000 };
1806
ret = nanosleep(&sleeptime, NULL);
1807
if(ret == -1 and errno != EINTR){
1808
perror_plus("nanosleep");
1816
__attribute__((nonnull, warn_unused_result))
1817
error_t take_down_interface(const char *const interface){
1818
error_t old_errno = errno;
1819
struct ifreq network;
1820
unsigned int if_index = if_nametoindex(interface);
1822
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1826
if(interface_is_up(interface)){
1827
error_t ret_errno = 0, ioctl_errno = 0;
1828
if(not get_flags(interface, &network) and debug){
1830
fprintf_plus(stderr, "Failed to get flags for interface "
1831
"\"%s\"\n", interface);
1835
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
1837
int sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1840
perror_plus("socket");
1846
fprintf_plus(stderr, "Taking down interface \"%s\"\n",
1850
/* Raise privileges */
1851
ret_errno = raise_privileges();
1854
perror_plus("Failed to raise privileges");
1857
int ret_setflags = ioctl(sd, SIOCSIFFLAGS, &network);
1858
ioctl_errno = errno;
1860
/* If raise_privileges() succeeded above */
1862
/* Lower privileges */
1863
ret_errno = lower_privileges();
1866
perror_plus("Failed to lower privileges");
1870
/* Close the socket */
1871
int ret = (int)TEMP_FAILURE_RETRY(close(sd));
1873
perror_plus("close");
1876
if(ret_setflags == -1){
1877
errno = ioctl_errno;
1878
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
1883
fprintf_plus(stderr, "Interface \"%s\" is already down; odd\n",
1519
1891
int main(int argc, char *argv[]){
1892
mandos_context mc = { .server = NULL, .dh_bits = 1024,
1893
.priority = "SECURE256:!CTYPE-X.509:"
1894
"+CTYPE-OPENPGP", .current_server = NULL,
1895
.interfaces = NULL, .interfaces_size = 0 };
1520
1896
AvahiSServiceBrowser *sb = NULL;
1523
1899
intmax_t tmpmax;
1525
1901
int exitcode = EXIT_SUCCESS;
1526
const char *interface = "";
1527
struct ifreq network;
1529
bool take_down_interface = false;
1532
char tempdir[] = "/tmp/mandosXXXXXX";
1533
bool tempdir_created = false;
1902
char *interfaces_to_take_down = NULL;
1903
size_t interfaces_to_take_down_size = 0;
1904
char run_tempdir[] = "/run/tmp/mandosXXXXXX";
1905
char old_tempdir[] = "/tmp/mandosXXXXXX";
1906
char *tempdir = NULL;
1534
1907
AvahiIfIndex if_index = AVAHI_IF_UNSPEC;
1535
1908
const char *seckey = PATHDIR "/" SECKEY;
1536
1909
const char *pubkey = PATHDIR "/" PUBKEY;
1910
char *interfaces_hooks = NULL;
1538
1912
bool gnutls_initialized = false;
1539
1913
bool gpgme_initialized = false;
1766
2144
/* Lower privileges */
1770
perror_plus("seteuid");
2145
ret_errno = lower_privileges();
2148
perror_plus("Failed to lower privileges");
2153
/* Remove invalid interface names (except "none") */
2155
char *interface = NULL;
2156
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2158
if(strcmp(interface, "none") != 0
2159
and if_nametoindex(interface) == 0){
2160
if(interface[0] != '\0'){
2161
fprintf_plus(stderr, "Not using nonexisting interface"
2162
" \"%s\"\n", interface);
2164
argz_delete(&mc.interfaces, &mc.interfaces_size, interface);
1775
2170
/* Run network hooks */
1776
if(not run_network_hooks("start", interface, delay)){
2172
if(mc.interfaces != NULL){
2173
interfaces_hooks = malloc(mc.interfaces_size);
2174
if(interfaces_hooks == NULL){
2175
perror_plus("malloc");
2178
memcpy(interfaces_hooks, mc.interfaces, mc.interfaces_size);
2179
argz_stringify(interfaces_hooks, mc.interfaces_size, (int)',');
2181
run_network_hooks("start", interfaces_hooks != NULL ?
2182
interfaces_hooks : "", delay);
1781
2186
avahi_set_log_function(empty_log);
1784
if(interface[0] == '\0'){
1785
struct dirent **direntries;
1786
/* First look for interfaces that are up */
1787
ret = scandir(sys_class_net, &direntries, up_interface,
1790
/* No up interfaces, look for any good interfaces */
1792
ret = scandir(sys_class_net, &direntries, good_interface,
1796
/* Pick the first interface returned */
1797
interface = strdup(direntries[0]->d_name);
1799
fprintf_plus(stderr, "Using interface \"%s\"\n", interface);
1801
if(interface == NULL){
1802
perror_plus("malloc");
1804
exitcode = EXIT_FAILURE;
1810
fprintf_plus(stderr, "Could not find a network interface\n");
1811
exitcode = EXIT_FAILURE;
1816
2189
/* Initialize Avahi early so avahi_simple_poll_quit() can be called
1817
2190
from the signal handler */
1818
2191
/* Initialize the pseudo-RNG for Avahi */
1819
2192
srand((unsigned int) time(NULL));
1820
mc.simple_poll = avahi_simple_poll_new();
1821
if(mc.simple_poll == NULL){
2193
simple_poll = avahi_simple_poll_new();
2194
if(simple_poll == NULL){
1822
2195
fprintf_plus(stderr,
1823
2196
"Avahi: Failed to create simple poll object.\n");
1824
2197
exitcode = EX_UNAVAILABLE;
1891
/* If the interface is down, bring it up */
1892
if(strcmp(interface, "none") != 0){
1893
if_index = (AvahiIfIndex) if_nametoindex(interface);
1895
fprintf_plus(stderr, "No such interface: \"%s\"\n", interface);
1896
exitcode = EX_UNAVAILABLE;
1904
/* Re-raise priviliges */
1908
perror_plus("seteuid");
1912
/* Lower kernel loglevel to KERN_NOTICE to avoid KERN_INFO
1913
messages about the network interface to mess up the prompt */
1914
ret = klogctl(8, NULL, 5);
1915
bool restore_loglevel = true;
1917
restore_loglevel = false;
1918
perror_plus("klogctl");
1920
#endif /* __linux__ */
1922
sd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP);
1924
perror_plus("socket");
1925
exitcode = EX_OSERR;
1927
if(restore_loglevel){
1928
ret = klogctl(7, NULL, 0);
1930
perror_plus("klogctl");
1933
#endif /* __linux__ */
1934
/* Lower privileges */
1938
perror_plus("seteuid");
1942
strcpy(network.ifr_name, interface);
1943
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1945
perror_plus("ioctl SIOCGIFFLAGS");
1947
if(restore_loglevel){
1948
ret = klogctl(7, NULL, 0);
1950
perror_plus("klogctl");
1953
#endif /* __linux__ */
1954
exitcode = EX_OSERR;
1955
/* Lower privileges */
1959
perror_plus("seteuid");
1963
if((network.ifr_flags & IFF_UP) == 0){
1964
network.ifr_flags |= IFF_UP;
1965
take_down_interface = true;
1966
ret = ioctl(sd, SIOCSIFFLAGS, &network);
1968
take_down_interface = false;
1969
perror_plus("ioctl SIOCSIFFLAGS +IFF_UP");
1970
exitcode = EX_OSERR;
1972
if(restore_loglevel){
1973
ret = klogctl(7, NULL, 0);
1975
perror_plus("klogctl");
2264
/* If no interfaces were specified, make a list */
2265
if(mc.interfaces == NULL){
2266
struct dirent **direntries = NULL;
2267
/* Look for any good interfaces */
2268
ret = scandir(sys_class_net, &direntries, good_interface,
2271
/* Add all found interfaces to interfaces list */
2272
for(int i = 0; i < ret; ++i){
2273
ret_errno = argz_add(&mc.interfaces, &mc.interfaces_size,
2274
direntries[i]->d_name);
2277
perror_plus("argz_add");
2281
fprintf_plus(stderr, "Will use interface \"%s\"\n",
2282
direntries[i]->d_name);
2290
fprintf_plus(stderr, "Could not find a network interface\n");
2291
exitcode = EXIT_FAILURE;
2296
/* Bring up interfaces which are down, and remove any "none"s */
2298
char *interface = NULL;
2299
while((interface = argz_next(mc.interfaces, mc.interfaces_size,
2301
/* If interface name is "none", stop bringing up interfaces.
2302
Also remove all instances of "none" from the list */
2303
if(strcmp(interface, "none") == 0){
2304
argz_delete(&mc.interfaces, &mc.interfaces_size,
2307
while((interface = argz_next(mc.interfaces,
2308
mc.interfaces_size, interface))){
2309
if(strcmp(interface, "none") == 0){
2310
argz_delete(&mc.interfaces, &mc.interfaces_size,
1978
#endif /* __linux__ */
1979
/* Lower privileges */
1983
perror_plus("seteuid");
1988
/* Sleep checking until interface is running.
1989
Check every 0.25s, up to total time of delay */
1990
for(int i=0; i < delay * 4; i++){
1991
ret = ioctl(sd, SIOCGIFFLAGS, &network);
1993
perror_plus("ioctl SIOCGIFFLAGS");
1994
} else if(network.ifr_flags & IFF_RUNNING){
1997
struct timespec sleeptime = { .tv_nsec = 250000000 };
1998
ret = nanosleep(&sleeptime, NULL);
1999
if(ret == -1 and errno != EINTR){
2000
perror_plus("nanosleep");
2003
if(not take_down_interface){
2004
/* We won't need the socket anymore */
2005
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2007
perror_plus("close");
2011
if(restore_loglevel){
2012
/* Restores kernel loglevel to default */
2013
ret = klogctl(7, NULL, 0);
2015
perror_plus("klogctl");
2018
#endif /* __linux__ */
2019
/* Lower privileges */
2021
/* Lower privileges */
2024
perror_plus("seteuid");
2317
bool interface_was_up = interface_is_up(interface);
2318
errno = bring_up_interface(interface, delay);
2319
if(not interface_was_up){
2321
perror_plus("Failed to bring up interface");
2323
errno = argz_add(&interfaces_to_take_down,
2324
&interfaces_to_take_down_size,
2327
perror_plus("argz_add");
2332
if(debug and (interfaces_to_take_down == NULL)){
2333
fprintf_plus(stderr, "No interfaces were brought up\n");
2337
/* If we only got one interface, explicitly use only that one */
2338
if(argz_count(mc.interfaces, mc.interfaces_size) == 1){
2340
fprintf_plus(stderr, "Using only interface \"%s\"\n",
2343
if_index = (AvahiIfIndex)if_nametoindex(mc.interfaces);
2032
ret = init_gnutls_global(pubkey, seckey);
2350
ret = init_gnutls_global(pubkey, seckey, &mc);
2034
2352
fprintf_plus(stderr, "init_gnutls_global failed\n");
2035
2353
exitcode = EX_UNAVAILABLE;
2226
2556
mc.current_server->prev->next = NULL;
2227
2557
while(mc.current_server != NULL){
2228
2558
server *next = mc.current_server->next;
2560
#pragma GCC diagnostic push
2561
#pragma GCC diagnostic ignored "-Wcast-qual"
2563
free((char *)(mc.current_server->ip));
2565
#pragma GCC diagnostic pop
2229
2567
free(mc.current_server);
2230
2568
mc.current_server = next;
2234
/* Run network hooks */
2235
run_network_hooks("stop", interface, delay);
2237
/* Re-raise priviliges */
2572
/* Re-raise privileges */
2242
perror_plus("seteuid");
2574
ret_errno = raise_privileges();
2577
perror_plus("Failed to raise privileges");
2580
/* Run network hooks */
2581
run_network_hooks("stop", interfaces_hooks != NULL ?
2582
interfaces_hooks : "", delay);
2584
/* Take down the network interfaces which were brought up */
2586
char *interface = NULL;
2587
while((interface=argz_next(interfaces_to_take_down,
2588
interfaces_to_take_down_size,
2590
ret_errno = take_down_interface(interface);
2593
perror_plus("Failed to take down interface");
2596
if(debug and (interfaces_to_take_down == NULL)){
2597
fprintf_plus(stderr, "No interfaces needed to be taken"
2245
/* Take down the network interface */
2246
if(take_down_interface and geteuid() == 0){
2247
ret = ioctl(sd, SIOCGIFFLAGS, &network);
2249
perror_plus("ioctl SIOCGIFFLAGS");
2250
} else if(network.ifr_flags & IFF_UP){
2251
network.ifr_flags &= ~(short)IFF_UP; /* clear flag */
2252
ret = ioctl(sd, SIOCSIFFLAGS, &network);
2254
perror_plus("ioctl SIOCSIFFLAGS -IFF_UP");
2257
ret = (int)TEMP_FAILURE_RETRY(close(sd));
2259
perror_plus("close");
2603
ret_errno = lower_privileges_permanently();
2606
perror_plus("Failed to lower privileges permanently");
2263
/* Lower privileges permanently */
2267
perror_plus("setuid");
2610
free(interfaces_to_take_down);
2611
free(interfaces_hooks);
2270
2613
/* Removes the GPGME temp directory and all files inside */
2271
if(tempdir_created){
2614
if(tempdir != NULL){
2272
2615
struct dirent **direntries = NULL;
2273
struct dirent *direntry = NULL;
2274
int numentries = scandir(tempdir, &direntries, notdotentries,
2276
if (numentries > 0){
2277
for(int i = 0; i < numentries; i++){
2278
direntry = direntries[i];
2279
char *fullname = NULL;
2280
ret = asprintf(&fullname, "%s/%s", tempdir,
2283
perror_plus("asprintf");
2286
ret = remove(fullname);
2288
fprintf_plus(stderr, "remove(\"%s\"): %s\n", fullname,
2616
int tempdir_fd = (int)TEMP_FAILURE_RETRY(open(tempdir, O_RDONLY |
2618
if(tempdir_fd == -1){
2619
perror_plus("open");
2622
#if __GLIBC_PREREQ(2, 15)
2623
int numentries = scandirat(tempdir_fd, ".", &direntries,
2624
notdotentries, alphasort);
2625
#else /* not __GLIBC_PREREQ(2, 15) */
2626
int numentries = scandir(tempdir, &direntries, notdotentries,
2628
#endif /* not __GLIBC_PREREQ(2, 15) */
2629
#else /* not __GLIBC__ */
2630
int numentries = scandir(tempdir, &direntries, notdotentries,
2632
#endif /* not __GLIBC__ */
2633
if(numentries >= 0){
2634
for(int i = 0; i < numentries; i++){
2635
ret = unlinkat(tempdir_fd, direntries[i]->d_name, 0);
2637
fprintf_plus(stderr, "unlinkat(open(\"%s\", O_RDONLY),"
2638
" \"%s\", 0): %s\n", tempdir,
2639
direntries[i]->d_name, strerror(errno));
2643
/* need to clean even if 0 because man page doesn't specify */
2645
if(numentries == -1){
2646
perror_plus("scandir");
2648
ret = rmdir(tempdir);
2649
if(ret == -1 and errno != ENOENT){
2650
perror_plus("rmdir");
2295
/* need to clean even if 0 because man page doesn't specify */
2297
if (numentries == -1){
2298
perror_plus("scandir");
2300
ret = rmdir(tempdir);
2301
if(ret == -1 and errno != ENOENT){
2302
perror_plus("rmdir");
2653
TEMP_FAILURE_RETRY(close(tempdir_fd));