Skip to content

Commit

Permalink
Use *open_proc* where possible
Browse files Browse the repository at this point in the history
Using open_proc/fopen_proc/__open_proc is better since
 - it uses openat
 - it comes with nice error reporting

Let's use it in places where we can. Even if it does not give any
improvements (such as in cr-check.c), error message unification
is good enough reason to do so.

Requested-by: Pavel Emelyanov <[email protected]>
Signed-off-by: Kir Kolyshkin <[email protected]>
Signed-off-by: Andrei Vagin <[email protected]>
  • Loading branch information
kolyshkin authored and avagin committed Apr 11, 2017
1 parent ba78d15 commit df9d945
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 28 deletions.
6 changes: 2 additions & 4 deletions criu/autofs.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,9 @@ static int access_autofs_mount(struct mount_info *pm)
if (new_pid_ns < 0)
return -1;

old_pid_ns = open("/proc/self/ns/pid", O_RDONLY);
if (old_pid_ns < 0) {
pr_perror("Can't open /proc/self/ns/pid");
old_pid_ns = open_proc(PROC_SELF, "ns/pid");
if (old_pid_ns < 0)
goto close_new_pid_ns;
}

if (switch_ns(pm->nsid->ns_pid, &mnt_ns_desc, &old_mnt_ns)) {
pr_err("failed to switch to mount namespace\n");
Expand Down
12 changes: 4 additions & 8 deletions criu/cr-check.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,9 @@ static int check_fcntl(void)
u32 v[2];
int fd;

fd = open("/proc/self/comm", O_RDONLY);
if (fd < 0) {
pr_perror("Can't open self comm file");
fd = open_proc(PROC_SELF, "comm");
if (fd < 0)
return -1;
}

if (fcntl(fd, F_GETOWNER_UIDS, (long)v)) {
pr_perror("Can'r fetch file owner UIDs");
Expand Down Expand Up @@ -726,11 +724,9 @@ static unsigned long get_ring_len(unsigned long addr)
FILE *maps;
char buf[256];

maps = fopen("/proc/self/maps", "r");
if (!maps) {
pr_perror("No maps proc file");
maps = fopen_proc(PROC_SELF, "maps");
if (!maps)
return 0;
}

while (fgets(buf, sizeof(buf), maps)) {
unsigned long start, end;
Expand Down
9 changes: 3 additions & 6 deletions criu/kerndat.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,8 @@ int kerndat_get_dirty_track(void)
goto no_dt;

ret = -1;
pm2 = open("/proc/self/pagemap", O_RDONLY);
pm2 = open_proc(PROC_SELF, "pagemap");
if (pm2 < 0) {
pr_perror("Can't open pagemap file");
munmap(map, PAGE_SIZE);
return ret;
}
Expand Down Expand Up @@ -397,11 +396,9 @@ int kerndat_fdinfo_has_lock()
int fd, pfd = -1, exit_code = -1, len;
char buf[PAGE_SIZE];

fd = open("/proc/locks", O_RDONLY);
if (fd < 0) {
pr_perror("Unable to open /proc/locks");
fd = open_proc(PROC_GEN, "locks");
if (fd < 0)
return -1;
}

if (flock(fd, LOCK_SH)) {
pr_perror("Can't take a lock");
Expand Down
8 changes: 2 additions & 6 deletions criu/namespaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,12 @@ int switch_ns(int pid, struct ns_desc *nd, int *rst)

int switch_ns_by_fd(int nsfd, struct ns_desc *nd, int *rst)
{
char buf[32];
int ret = -1;

if (rst) {
snprintf(buf, sizeof(buf), "/proc/self/ns/%s", nd->str);
*rst = open(buf, O_RDONLY);
if (*rst < 0) {
pr_perror("Can't open ns file");
*rst = open_proc(PROC_SELF, "ns/%s", nd->str);
if (*rst < 0)
goto err_ns;
}
}

ret = setns(nsfd, nd->cflag);
Expand Down
6 changes: 2 additions & 4 deletions criu/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -1715,11 +1715,9 @@ int netns_keep_nsfd(void)
* that before we leave the existing namespaces.
*/

ns_fd = open("/proc/self/ns/net", O_RDONLY | O_CLOEXEC);
if (ns_fd < 0) {
pr_perror("Can't cache net fd");
ns_fd = __open_proc(PROC_SELF, 0, O_RDONLY | O_CLOEXEC, "ns/net");
if (ns_fd < 0)
return -1;
}

ret = install_service_fd(NS_FD_OFF, ns_fd);
if (ret < 0)
Expand Down

0 comments on commit df9d945

Please sign in to comment.