Commit d3d1ee3ab28711360937839423158cc185f710f2
Committed by
Greg Kroah-Hartman
1 parent
038336a5b4
Exists in
master
and in
20 other branches
tools: hv: use getmntent in hv_vss_daemon
As suggested by Paolo Bonzini, use getmntent instead of parsing output of mount(1). Signed-off-by: Olaf Hering <olaf@aepfle.de> Signed-off-by: K. Y. Srinivasan <kys@microsoft.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Showing 1 changed file with 21 additions and 18 deletions Side-by-side Diff
tools/hv/hv_vss_daemon.c
... | ... | @@ -23,6 +23,7 @@ |
23 | 23 | #include <sys/poll.h> |
24 | 24 | #include <linux/types.h> |
25 | 25 | #include <stdio.h> |
26 | +#include <mntent.h> | |
26 | 27 | #include <stdlib.h> |
27 | 28 | #include <unistd.h> |
28 | 29 | #include <string.h> |
... | ... | @@ -47,11 +48,10 @@ |
47 | 48 | { |
48 | 49 | char *fs_op; |
49 | 50 | char cmd[512]; |
50 | - char buf[512]; | |
51 | - FILE *file; | |
52 | - char *p; | |
53 | - char *x; | |
54 | - int error = 0; | |
51 | + char match[] = "/dev/"; | |
52 | + FILE *mounts; | |
53 | + struct mntent *ent; | |
54 | + int error = 0, root_seen = 0; | |
55 | 55 | |
56 | 56 | switch (operation) { |
57 | 57 | case VSS_OP_FREEZE: |
58 | 58 | |
59 | 59 | |
60 | 60 | |
61 | 61 | |
62 | 62 | |
... | ... | @@ -64,25 +64,28 @@ |
64 | 64 | return -1; |
65 | 65 | } |
66 | 66 | |
67 | - file = popen("mount | awk '/^\\/dev\\// { print $3}'", "r"); | |
68 | - if (file == NULL) | |
67 | + mounts = setmntent("/proc/mounts", "r"); | |
68 | + if (mounts == NULL) | |
69 | 69 | return -1; |
70 | 70 | |
71 | - while ((p = fgets(buf, sizeof(buf), file)) != NULL) { | |
72 | - x = strchr(p, '\n'); | |
73 | - *x = '\0'; | |
74 | - if (!strncmp(p, "/", sizeof("/"))) | |
71 | + while((ent = getmntent(mounts))) { | |
72 | + if (strncmp(ent->mnt_fsname, match, strlen(match))) | |
75 | 73 | continue; |
76 | - | |
77 | - sprintf(cmd, "%s %s %s", "fsfreeze ", fs_op, p); | |
74 | + if (strcmp(ent->mnt_dir, "/") == 0) { | |
75 | + root_seen = 1; | |
76 | + continue; | |
77 | + } | |
78 | + snprintf(cmd, sizeof(cmd), "fsfreeze %s '%s'", fs_op, ent->mnt_dir); | |
78 | 79 | syslog(LOG_INFO, "VSS cmd is %s\n", cmd); |
79 | - error = system(cmd); | |
80 | + error |= system(cmd); | |
80 | 81 | } |
81 | - pclose(file); | |
82 | + endmntent(mounts); | |
82 | 83 | |
83 | - sprintf(cmd, "%s %s %s", "fsfreeze ", fs_op, "/"); | |
84 | - syslog(LOG_INFO, "VSS cmd is %s\n", cmd); | |
85 | - error = system(cmd); | |
84 | + if (root_seen) { | |
85 | + sprintf(cmd, "fsfreeze %s /", fs_op); | |
86 | + syslog(LOG_INFO, "VSS cmd is %s\n", cmd); | |
87 | + error |= system(cmd); | |
88 | + } | |
86 | 89 | |
87 | 90 | return error; |
88 | 91 | } |