int randseed(bool srand_it)
{
- FILE *f;
+ __do_fclose FILE *f = NULL;
/*
* srand pre-seed function based on /dev/urandom
*/
unsigned int seed = time(NULL) + getpid();
- f = fopen("/dev/urandom", "r");
+ f = fopen("/dev/urandom", "re");
if (f) {
int ret = fread(&seed, sizeof(seed), 1, f);
if (ret != 1)
SYSDEBUG("Unable to fread /dev/urandom, fallback to time+pid rand seed");
-
- fclose(f);
}
if (srand_it)
uid_t get_ns_uid(uid_t orig)
{
- char *line = NULL;
+ __do_free char *line = NULL;
+ __do_fclose FILE *f = NULL;
size_t sz = 0;
uid_t nsid, hostid, range;
- FILE *f;
- f = fopen("/proc/self/uid_map", "r");
+ f = fopen("/proc/self/uid_map", "re");
if (!f) {
SYSERROR("Failed to open uid_map");
return 0;
if (sscanf(line, "%u %u %u", &nsid, &hostid, &range) != 3)
continue;
- if (hostid <= orig && hostid + range > orig) {
- nsid += orig - hostid;
- goto found;
- }
+ if (hostid <= orig && hostid + range > orig)
+ return nsid += orig - hostid;
}
- nsid = LXC_INVALID_UID;
-
-found:
- fclose(f);
- free(line);
- return nsid;
+ return LXC_INVALID_UID;
}
gid_t get_ns_gid(gid_t orig)
{
- char *line = NULL;
+ __do_free char *line = NULL;
+ __do_fclose FILE *f = NULL;
size_t sz = 0;
gid_t nsid, hostid, range;
- FILE *f;
- f = fopen("/proc/self/gid_map", "r");
+ f = fopen("/proc/self/gid_map", "re");
if (!f) {
SYSERROR("Failed to open gid_map");
return 0;
if (sscanf(line, "%u %u %u", &nsid, &hostid, &range) != 3)
continue;
- if (hostid <= orig && hostid + range > orig) {
- nsid += orig - hostid;
- goto found;
- }
+ if (hostid <= orig && hostid + range > orig)
+ return nsid += orig - hostid;
}
- nsid = LXC_INVALID_GID;
-
-found:
- fclose(f);
- free(line);
- return nsid;
+ return LXC_INVALID_GID;
}
bool dir_exists(const char *path)
int i;
size_t len = 0;
- f = fopen("/proc/self/mountinfo", "r");
+ f = fopen("/proc/self/mountinfo", "re");
if (!f)
return 0;
*/
bool detect_ramfs_rootfs(void)
{
- FILE *f;
- char *p, *p2;
- char *line = NULL;
+ __do_free char *line = NULL;
+ __do_free void *fopen_cache = NULL;
+ __do_fclose FILE *f = NULL;
size_t len = 0;
- int i;
- f = fopen("/proc/self/mountinfo", "r");
- if (!f) {
- SYSERROR("Failed to open mountinfo");
+ f = fopen_cached("/proc/self/mountinfo", "re", &fopen_cache);
+ if (!f)
return false;
- }
while (getline(&line, &len, f) != -1) {
+ int i;
+ char *p, *p2;
+
for (p = line, i = 0; p && i < 4; i++)
p = strchr(p + 1, ' ');
if (!p)
p2 = strchr(p + 1, ' ');
if (!p2)
continue;
-
*p2 = '\0';
if (strcmp(p + 1, "/") == 0) {
/* This is '/'. Is it the ramfs? */
p = strchr(p2 + 1, '-');
- if (p && strncmp(p, "- rootfs rootfs ", 16) == 0) {
- free(line);
- fclose(f);
- INFO("Rootfs is located on ramfs");
+ if (p && strncmp(p, "- rootfs rootfs ", 16) == 0)
return true;
- }
}
}
- free(line);
- fclose(f);
return false;
}
#define __PROC_STATUS_LEN (6 + INTTYPE_TO_STRLEN(pid_t) + 7 + 1)
bool task_blocks_signal(pid_t pid, int signal)
{
+ __do_free char *line = NULL;
+ __do_fclose FILE *f = NULL;
int ret;
char status[__PROC_STATUS_LEN] = {0};
- FILE *f;
uint64_t sigblk = 0, one = 1;
size_t n = 0;
bool bret = false;
- char *line = NULL;
ret = snprintf(status, __PROC_STATUS_LEN, "/proc/%d/status", pid);
if (ret < 0 || ret >= __PROC_STATUS_LEN)
return bret;
- f = fopen(status, "r");
+ f = fopen(status, "re");
if (!f)
- return bret;
+ return false;
while (getline(&line, &n, f) != -1) {
char *numstr;
numstr = lxc_trim_whitespace_in_place(line + 7);
ret = lxc_safe_uint64(numstr, &sigblk, 16);
if (ret < 0)
- goto out;
+ return false;
break;
}
if (sigblk & (one << (signal - 1)))
bret = true;
-out:
- free(line);
- fclose(f);
return bret;
}
if (fd_cloexec(dupfd, true) < 0)
return -1;
- /* transfer ownership of fd */
f = fdopen(dupfd, "re");
if (!f)
return -1;
+
+ /* Transfer ownership of fd. */
move_fd(dupfd);
ret = 0;