Introduce _cleanup_endmntent_
This commit is contained in:
parent
9a5cb1371b
commit
5862d652ba
|
@ -236,31 +236,24 @@ finish:
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *disk_mount_point(const char *label) {
|
static char *disk_mount_point(const char *label) {
|
||||||
char *mp = NULL;
|
|
||||||
_cleanup_free_ char *device = NULL;
|
_cleanup_free_ char *device = NULL;
|
||||||
FILE *f = NULL;
|
_cleanup_endmntent_ FILE *f = NULL;
|
||||||
struct mntent *m;
|
struct mntent *m;
|
||||||
|
|
||||||
/* Yeah, we don't support native systemd unit files here for now */
|
/* Yeah, we don't support native systemd unit files here for now */
|
||||||
|
|
||||||
if (asprintf(&device, "/dev/mapper/%s", label) < 0)
|
if (asprintf(&device, "/dev/mapper/%s", label) < 0)
|
||||||
goto finish;
|
return NULL;
|
||||||
|
|
||||||
f = setmntent("/etc/fstab", "r");
|
f = setmntent("/etc/fstab", "r");
|
||||||
if (!f)
|
if (!f)
|
||||||
goto finish;
|
return NULL;
|
||||||
|
|
||||||
while ((m = getmntent(f)))
|
while ((m = getmntent(f)))
|
||||||
if (path_equal(m->mnt_fsname, device)) {
|
if (path_equal(m->mnt_fsname, device))
|
||||||
mp = strdup(m->mnt_dir);
|
return strdup(m->mnt_dir);
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
finish:
|
return NULL;
|
||||||
if (f)
|
|
||||||
endmntent(f);
|
|
||||||
|
|
||||||
return mp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_password(const char *name, usec_t until, bool accept_cached, char ***passwords) {
|
static int get_password(const char *name, usec_t until, bool accept_cached, char ***passwords) {
|
||||||
|
|
|
@ -301,15 +301,12 @@ static int add_mount(
|
||||||
}
|
}
|
||||||
|
|
||||||
static int parse_fstab(const char *prefix, bool initrd) {
|
static int parse_fstab(const char *prefix, bool initrd) {
|
||||||
_cleanup_free_ char *fstab_path = NULL;
|
char *fstab_path;
|
||||||
FILE *f;
|
_cleanup_endmntent_ FILE *f;
|
||||||
int r = 0;
|
int r = 0;
|
||||||
struct mntent *me;
|
struct mntent *me;
|
||||||
|
|
||||||
fstab_path = strjoin(strempty(prefix), "/etc/fstab", NULL);
|
fstab_path = strappenda(strempty(prefix), "/etc/fstab");
|
||||||
if (!fstab_path)
|
|
||||||
return log_oom();
|
|
||||||
|
|
||||||
f = setmntent(fstab_path, "r");
|
f = setmntent(fstab_path, "r");
|
||||||
if (!f) {
|
if (!f) {
|
||||||
if (errno == ENOENT)
|
if (errno == ENOENT)
|
||||||
|
@ -328,10 +325,8 @@ static int parse_fstab(const char *prefix, bool initrd) {
|
||||||
|
|
||||||
what = fstab_node_to_udev_node(me->mnt_fsname);
|
what = fstab_node_to_udev_node(me->mnt_fsname);
|
||||||
where = strjoin(strempty(prefix), me->mnt_dir, NULL);
|
where = strjoin(strempty(prefix), me->mnt_dir, NULL);
|
||||||
if (!what || !where) {
|
if (!what || !where)
|
||||||
r = log_oom();
|
return log_oom();
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_path(where))
|
if (is_path(where))
|
||||||
path_kill_slashes(where);
|
path_kill_slashes(where);
|
||||||
|
@ -369,8 +364,6 @@ static int parse_fstab(const char *prefix, bool initrd) {
|
||||||
r = k;
|
r = k;
|
||||||
}
|
}
|
||||||
|
|
||||||
finish:
|
|
||||||
endmntent(f);
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
int ret = EXIT_FAILURE;
|
int ret = EXIT_FAILURE;
|
||||||
FILE *f = NULL;
|
_cleanup_endmntent_ FILE *f = NULL;
|
||||||
struct mntent* me;
|
struct mntent* me;
|
||||||
Hashmap *pids = NULL;
|
Hashmap *pids = NULL;
|
||||||
|
|
||||||
|
@ -57,13 +57,11 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
f = setmntent("/etc/fstab", "r");
|
f = setmntent("/etc/fstab", "r");
|
||||||
if (!f) {
|
if (!f) {
|
||||||
if (errno == ENOENT) {
|
if (errno == ENOENT)
|
||||||
ret = EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
log_error("Failed to open /etc/fstab: %m");
|
log_error("Failed to open /etc/fstab: %m");
|
||||||
goto finish;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
pids = hashmap_new(trivial_hash_func, trivial_compare_func);
|
pids = hashmap_new(trivial_hash_func, trivial_compare_func);
|
||||||
|
@ -162,8 +160,5 @@ finish:
|
||||||
if (pids)
|
if (pids)
|
||||||
hashmap_free_free(pids);
|
hashmap_free_free(pids);
|
||||||
|
|
||||||
if (f)
|
|
||||||
endmntent(f);
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <locale.h>
|
#include <locale.h>
|
||||||
|
#include <mntent.h>
|
||||||
|
|
||||||
#include "macro.h"
|
#include "macro.h"
|
||||||
#include "time-util.h"
|
#include "time-util.h"
|
||||||
|
@ -578,6 +579,11 @@ static inline void umaskp(mode_t *u) {
|
||||||
umask(*u);
|
umask(*u);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline void endmntentp(FILE **f) {
|
||||||
|
if (*f)
|
||||||
|
endmntent(*f);
|
||||||
|
}
|
||||||
|
|
||||||
#define _cleanup_free_ _cleanup_(freep)
|
#define _cleanup_free_ _cleanup_(freep)
|
||||||
#define _cleanup_fclose_ _cleanup_(fclosep)
|
#define _cleanup_fclose_ _cleanup_(fclosep)
|
||||||
#define _cleanup_pclose_ _cleanup_(pclosep)
|
#define _cleanup_pclose_ _cleanup_(pclosep)
|
||||||
|
@ -585,6 +591,7 @@ static inline void umaskp(mode_t *u) {
|
||||||
#define _cleanup_closedir_ _cleanup_(closedirp)
|
#define _cleanup_closedir_ _cleanup_(closedirp)
|
||||||
#define _cleanup_umask_ _cleanup_(umaskp)
|
#define _cleanup_umask_ _cleanup_(umaskp)
|
||||||
#define _cleanup_globfree_ _cleanup_(globfree)
|
#define _cleanup_globfree_ _cleanup_(globfree)
|
||||||
|
#define _cleanup_endmntent_ _cleanup_(endmntentp)
|
||||||
|
|
||||||
_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
|
_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {
|
||||||
if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
|
if (_unlikely_(b == 0 || a > ((size_t) -1) / b))
|
||||||
|
|
Loading…
Reference in New Issue