blob: 5379197f94428854faacf35f9fcb0f928c4cec5d (
plain)
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
|
/*
* sb_close.c
*
* IO functions.
*
* Copyright 1999-2012 Gentoo Foundation
* Licensed under the GPL-2
*/
#include "headers.h"
#include "sbutil.h"
/* General purpose function to _reliably_ close a file
*
* Returns 0 if successful or negative number on error (and errno set)
*/
int sb_close(int fd)
{
int res;
do {
res = close(fd);
} while ((res < 0) && (EINTR == errno));
/* Do not care about errors here */
if (-1 != res)
errno = 0;
return res;
}
/* Quickly close all the open fds (good for daemonization) */
void sb_close_all_fds(void)
{
DIR *dirp;
struct dirent *de;
int dfd, fd;
const char *fd_dir = sb_get_fd_dir();
dirp = opendir(fd_dir);
if (!dirp)
sb_ebort("could not process %s\n", fd_dir);
dfd = dirfd(dirp);
while ((de = readdir(dirp)) != NULL) {
if (de->d_name[0] == '.')
continue;
fd = atoi(de->d_name);
if (fd != dfd)
close(fd);
}
closedir(dirp);
}
|