Introduce udev object cleanup functions

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2013-10-12 20:28:21 -04:00
parent b506291ff1
commit 1ca208fb4f
18 changed files with 222 additions and 352 deletions

View File

@ -647,6 +647,7 @@ libsystemd_shared_la_SOURCES = \
src/shared/sparse-endian.h \
src/shared/util.c \
src/shared/util.h \
src/shared/udev-util.h \
src/shared/virt.c \
src/shared/virt.h \
src/shared/efivars.c \

View File

@ -19,15 +19,15 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <libudev.h>
#include "util.h"
#include "mkdir.h"
#include "fileio.h"
#include "libudev.h"
#include "udev-util.h"
int main(int argc, char *argv[]) {
struct udev *udev = NULL;
struct udev_device *device = NULL;
_cleanup_udev_unref_ struct udev *udev = NULL;
_cleanup_udev_device_unref_ struct udev_device *device = NULL;
_cleanup_free_ char *saved = NULL;
int r;
@ -45,13 +45,13 @@ int main(int argc, char *argv[]) {
r = mkdir_p("/var/lib/systemd/backlight", 0755);
if (r < 0) {
log_error("Failed to create backlight directory: %s", strerror(-r));
goto finish;
return EXIT_FAILURE;
}
udev = udev_new();
if (!udev) {
r = log_oom();
goto finish;
log_oom();
return EXIT_FAILURE;
}
errno = 0;
@ -59,26 +59,24 @@ int main(int argc, char *argv[]) {
if (!device)
device = udev_device_new_from_subsystem_sysname(udev, "leds", argv[2]);
if (!device) {
if (errno != 0) {
if (errno != 0)
log_error("Failed to get backlight device '%s': %m", argv[2]);
r = -errno;
} else
else
r = log_oom();
goto finish;
return EXIT_FAILURE;
}
if (!streq_ptr(udev_device_get_subsystem(device), "backlight") &&
!streq_ptr(udev_device_get_subsystem(device), "leds")) {
log_error("Not a backlight device: %s", argv[2]);
r = -ENODEV;
goto finish;
return EXIT_FAILURE;
}
saved = strappend("/var/lib/systemd/backlight/", udev_device_get_sysname(device));
if (!saved) {
r = log_oom();
goto finish;
log_oom();
return EXIT_FAILURE;
}
if (streq(argv[1], "load")) {
@ -87,19 +85,17 @@ int main(int argc, char *argv[]) {
r = read_one_line_file(saved, &value);
if (r < 0) {
if (r == -ENOENT) {
r = 0;
goto finish;
}
if (r == -ENOENT)
return EXIT_SUCCESS;
log_error("Failed to read %s: %s", saved, strerror(-r));
goto finish;
return EXIT_FAILURE;
}
r = udev_device_set_sysattr_value(device, "brightness", value);
if (r < 0) {
log_error("Failed to write system attribute: %s", strerror(-r));
goto finish;
return EXIT_FAILURE;
}
} else if (streq(argv[1], "save")) {
@ -108,28 +104,19 @@ int main(int argc, char *argv[]) {
value = udev_device_get_sysattr_value(device, "brightness");
if (!value) {
log_error("Failed to read system attribute: %s", strerror(-r));
goto finish;
return EXIT_FAILURE;
}
r = write_string_file(saved, value);
if (r < 0) {
log_error("Failed to write %s: %s", saved, strerror(-r));
goto finish;
return EXIT_FAILURE;
}
} else {
log_error("Unknown verb %s.", argv[1]);
r = -EINVAL;
goto finish;
return EXIT_FAILURE;
}
finish:
if (device)
udev_device_unref(device);
if (udev)
udev_unref(udev);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
return EXIT_SUCCESS;
}

View File

@ -27,7 +27,6 @@
#include <unistd.h>
#include <linux/loop.h>
#include <linux/dm-ioctl.h>
#include <libudev.h>
#include "list.h"
#include "mount-setup.h"
@ -35,6 +34,8 @@
#include "path-util.h"
#include "util.h"
#include "virt.h"
#include "libudev.h"
#include "udev-util.h"
typedef struct MountPoint {
char *path;
@ -201,145 +202,108 @@ finish:
}
static int loopback_list_get(MountPoint **head) {
int r;
struct udev *udev;
struct udev_enumerate *e = NULL;
_cleanup_udev_unref_ struct udev *udev;
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
assert(head);
if (!(udev = udev_new())) {
r = -ENOMEM;
goto finish;
}
udev = udev_new();
if (!udev)
return -ENOMEM;
if (!(e = udev_enumerate_new(udev))) {
r = -ENOMEM;
goto finish;
}
e = udev_enumerate_new(udev);
if (!e)
return -ENOMEM;
if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
udev_enumerate_add_match_sysname(e, "loop*") < 0 ||
udev_enumerate_add_match_sysattr(e, "loop/backing_file", NULL) < 0) {
r = -EIO;
goto finish;
}
udev_enumerate_add_match_sysattr(e, "loop/backing_file", NULL) < 0)
return -EIO;
if (udev_enumerate_scan_devices(e) < 0) {
r = -EIO;
goto finish;
}
if (udev_enumerate_scan_devices(e) < 0)
return -EIO;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
MountPoint *lb;
struct udev_device *d;
_cleanup_udev_device_unref_ struct udev_device *d;
char *loop;
const char *dn;
if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
r = -ENOMEM;
goto finish;
}
d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!d)
return -ENOMEM;
if (!(dn = udev_device_get_devnode(d))) {
udev_device_unref(d);
dn = udev_device_get_devnode(d);
if (!dn)
continue;
}
loop = strdup(dn);
udev_device_unref(d);
if (!loop)
return -ENOMEM;
if (!loop) {
r = -ENOMEM;
goto finish;
}
if (!(lb = new0(MountPoint, 1))) {
lb = new0(MountPoint, 1);
if (!lb) {
free(loop);
r = -ENOMEM;
goto finish;
return -ENOMEM;
}
lb->path = loop;
LIST_PREPEND(MountPoint, mount_point, *head, lb);
}
r = 0;
finish:
if (e)
udev_enumerate_unref(e);
if (udev)
udev_unref(udev);
return r;
return 0;
}
static int dm_list_get(MountPoint **head) {
int r;
struct udev *udev;
struct udev_enumerate *e = NULL;
_cleanup_udev_unref_ struct udev *udev;
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
struct udev_list_entry *item = NULL, *first = NULL;
assert(head);
if (!(udev = udev_new())) {
r = -ENOMEM;
goto finish;
}
udev = udev_new();
if (!udev)
return -ENOMEM;
if (!(e = udev_enumerate_new(udev))) {
r = -ENOMEM;
goto finish;
}
e = udev_enumerate_new(udev);
if (!e)
return -ENOMEM;
if (udev_enumerate_add_match_subsystem(e, "block") < 0 ||
udev_enumerate_add_match_sysname(e, "dm-*") < 0) {
r = -EIO;
goto finish;
}
udev_enumerate_add_match_sysname(e, "dm-*") < 0)
return -EIO;
if (udev_enumerate_scan_devices(e) < 0) {
r = -EIO;
goto finish;
}
if (udev_enumerate_scan_devices(e) < 0)
return -EIO;
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
MountPoint *m;
struct udev_device *d;
_cleanup_udev_device_unref_ struct udev_device *d;
dev_t devnum;
char *node;
const char *dn;
if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
r = -ENOMEM;
goto finish;
}
d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!d)
return -ENOMEM;
devnum = udev_device_get_devnum(d);
dn = udev_device_get_devnode(d);
if (major(devnum) == 0 || !dn) {
udev_device_unref(d);
if (major(devnum) == 0 || !dn)
continue;
}
node = strdup(dn);
udev_device_unref(d);
if (!node)
return -ENOMEM;
if (!node) {
r = -ENOMEM;
goto finish;
}
if (!(m = new(MountPoint, 1))) {
m = new(MountPoint, 1);
if (!m) {
free(node);
r = -ENOMEM;
goto finish;
return -ENOMEM;
}
m->path = node;
@ -347,16 +311,7 @@ static int dm_list_get(MountPoint **head) {
LIST_PREPEND(MountPoint, mount_point, *head, m);
}
r = 0;
finish:
if (e)
udev_enumerate_unref(e);
if (udev)
udev_unref(udev);
return r;
return 0;
}
static int delete_loopback(const char *device) {

View File

@ -25,7 +25,6 @@
#include <mntent.h>
#include <libcryptsetup.h>
#include <libudev.h>
#include "fileio.h"
#include "log.h"
@ -34,6 +33,8 @@
#include "strv.h"
#include "ask-password-api.h"
#include "def.h"
#include "libudev.h"
#include "udev-util.h"
static const char *opt_type = NULL; /* CRYPT_LUKS1, CRYPT_TCRYPT or CRYPT_PLAIN */
static char *opt_cipher = NULL;
@ -184,7 +185,7 @@ static void log_glue(int level, const char *msg, void *usrptr) {
log_debug("%s", msg);
}
static char *disk_description(const char *path) {
static char* disk_description(const char *path) {
static const char name_fields[] = {
"ID_PART_ENTRY_NAME\0"
@ -193,10 +194,9 @@ static char *disk_description(const char *path) {
"ID_MODEL\0"
};
struct udev *udev = NULL;
struct udev_device *device = NULL;
_cleanup_udev_unref_ struct udev *udev = NULL;
_cleanup_udev_device_unref_ struct udev_device *device = NULL;
struct stat st;
char *description = NULL;
const char *i;
assert(path);
@ -213,26 +213,17 @@ static char *disk_description(const char *path) {
device = udev_device_new_from_devnum(udev, 'b', st.st_rdev);
if (!device)
goto finish;
return NULL;
NULSTR_FOREACH(i, name_fields) {
const char *name;
name = udev_device_get_property_value(device, i);
if (!isempty(name)) {
description = strdup(name);
break;
}
if (!isempty(name))
return strdup(name);
}
finish:
if (device)
udev_device_unref(device);
if (udev)
udev_unref(udev);
return description;
return NULL;
}
static char *disk_mount_point(const char *label) {

View File

@ -27,7 +27,6 @@
#include <fcntl.h>
#include <sys/file.h>
#include <libudev.h>
#include <dbus/dbus.h>
#include "util.h"
@ -36,6 +35,8 @@
#include "bus-errors.h"
#include "virt.h"
#include "fileio.h"
#include "libudev.h"
#include "udev-util.h"
static bool arg_skip = false;
static bool arg_force = false;
@ -251,8 +252,8 @@ int main(int argc, char *argv[]) {
int i = 0, r = EXIT_FAILURE, q;
pid_t pid;
siginfo_t status;
struct udev *udev = NULL;
struct udev_device *udev_device = NULL;
_cleanup_udev_unref_ struct udev *udev = NULL;
_cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
const char *device;
bool root_directory;
int progress_pipe[2] = { -1, -1 };
@ -400,12 +401,6 @@ int main(int argc, char *argv[]) {
touch("/run/systemd/quotacheck");
finish:
if (udev_device)
udev_device_unref(udev_device);
if (udev)
udev_unref(udev);
close_pipe(progress_pipe);
return r;

View File

@ -36,6 +36,7 @@
#include "missing.h"
#include "sd-id128.h"
#include "libudev.h"
#include "udev-util.h"
#include "special.h"
#include "unit-name.h"
#include "virt.h"
@ -53,10 +54,7 @@
static const char *arg_dest = "/tmp";
static inline void blkid_free_probep(blkid_probe *b) {
if (*b)
blkid_free_probe(*b);
}
define_trivial_cleanup_func(blkid_probe, blkid_free_probe)
#define _cleanup_blkid_freep_probe_ _cleanup_(blkid_free_probep)
static int verify_gpt_partition(const char *node, sd_id128_t *type, unsigned *nr, char **fstype) {
@ -237,8 +235,9 @@ static int add_home(const char *path, const char *fstype) {
}
static int enumerate_partitions(struct udev *udev, dev_t dev) {
struct udev_enumerate *e = NULL;
struct udev_device *parent = NULL, *d = NULL;
struct udev_device *parent = NULL;
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
_cleanup_udev_device_unref_ struct udev_device *d = NULL;
struct udev_list_entry *first, *item;
unsigned home_nr = (unsigned) -1;
_cleanup_free_ char *home = NULL, *home_fstype = NULL;
@ -249,71 +248,58 @@ static int enumerate_partitions(struct udev *udev, dev_t dev) {
return log_oom();
d = udev_device_new_from_devnum(udev, 'b', dev);
if (!d) {
r = log_oom();
goto finish;
}
if (!d)
return log_oom();
parent = udev_device_get_parent(d);
if (!parent) {
r = log_oom();
goto finish;
}
if (!parent)
return log_oom();
r = udev_enumerate_add_match_parent(e, parent);
if (r < 0) {
r = log_oom();
goto finish;
}
if (r < 0)
return log_oom();
r = udev_enumerate_add_match_subsystem(e, "block");
if (r < 0) {
r = log_oom();
goto finish;
}
if (r < 0)
return log_oom();
r = udev_enumerate_scan_devices(e);
if (r < 0) {
log_error("Failed to enumerate partitions on /dev/block/%u:%u: %s",
major(dev), minor(dev), strerror(-r));
goto finish;
return r;
}
first = udev_enumerate_get_list_entry(e);
udev_list_entry_foreach(item, first) {
_cleanup_free_ char *fstype = NULL;
const char *node = NULL;
struct udev_device *q;
_cleanup_udev_device_unref_ struct udev_device *q;
sd_id128_t type_id;
unsigned nr;
q = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item));
if (!q) {
r = log_oom();
goto finish;
}
if (!q)
return log_oom();
if (udev_device_get_devnum(q) == udev_device_get_devnum(d))
goto skip;
continue;
if (udev_device_get_devnum(q) == udev_device_get_devnum(parent))
goto skip;
continue;
node = udev_device_get_devnode(q);
if (!node) {
r = log_oom();
goto finish;
}
if (!node)
return log_oom();
r = verify_gpt_partition(node, &type_id, &nr, &fstype);
if (r < 0) {
log_error("Failed to verify GPT partition %s: %s",
node, strerror(-r));
udev_device_unref(q);
goto finish;
return r;
}
if (r == 0)
goto skip;
continue;
if (sd_id128_equal(type_id, GPT_SWAP))
add_swap(node, fstype);
@ -321,10 +307,8 @@ static int enumerate_partitions(struct udev *udev, dev_t dev) {
if (!home || nr < home_nr) {
free(home);
home = strdup(node);
if (!home) {
r = log_oom();
goto finish;
}
if (!home)
return log_oom();
home_nr = nr;
@ -333,22 +317,11 @@ static int enumerate_partitions(struct udev *udev, dev_t dev) {
fstype = NULL;
}
}
skip:
udev_device_unref(q);
}
if (home && home_fstype)
add_home(home, home_fstype);
finish:
if (d)
udev_device_unref(d);
if (e)
udev_enumerate_unref(e);
return r;
}
@ -425,41 +398,31 @@ static int get_block_device(const char *path, dev_t *dev) {
}
static int devno_to_devnode(struct udev *udev, dev_t devno, char **ret) {
struct udev_device *d = NULL;
_cleanup_udev_device_unref_ struct udev_device *d;
const char *t;
char *n;
int r;
d = udev_device_new_from_devnum(udev, 'b', devno);
if (!d)
return log_oom();
t = udev_device_get_devnode(d);
if (!t) {
r = -ENODEV;
goto finish;
}
if (!t)
return -ENODEV;
n = strdup(t);
if (!n) {
r = -ENOMEM;
goto finish;
}
if (!n)
return -ENOMEM;
*ret = n;
r = 0;
finish:
udev_device_unref(d);
return r;
return 0;
}
int main(int argc, char *argv[]) {
_cleanup_free_ char *node = NULL;
struct udev *udev = NULL;
_cleanup_udev_unref_ struct udev *udev = NULL;
dev_t devno;
int r;
int r = 0;
if (argc > 1 && argc != 4) {
log_error("This program takes three or no arguments.");
@ -478,13 +441,11 @@ int main(int argc, char *argv[]) {
if (in_initrd()) {
log_debug("In initrd, exiting.");
r = 0;
goto finish;
}
if (detect_container(NULL) > 0) {
log_debug("In a container, exiting.");
r = 0;
goto finish;
}
@ -523,8 +484,5 @@ int main(int argc, char *argv[]) {
r = enumerate_partitions(udev, devno);
finish:
if (udev)
udev_unref(udev);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}

View File

@ -135,11 +135,8 @@ struct sd_journal {
char *journal_make_match_string(sd_journal *j);
void journal_print_header(sd_journal *j);
static inline void journal_closep(sd_journal **j) {
sd_journal_close(*j);
}
#define _cleanup_journal_close_ _cleanup_(journal_closep)
define_trivial_cleanup_func(sd_journal*, sd_journal_close)
#define _cleanup_journal_close_ _cleanup_(sd_journal_closep)
#define JOURNAL_FOREACH_DATA_RETVAL(j, data, l, retval) \
for (sd_journal_restart_data(j); ((retval) = sd_journal_enumerate_data((j), &(data), &(l))) > 0; )

View File

@ -26,6 +26,7 @@
#include "util.h"
#include "sysfs-show.h"
#include "path-util.h"
#include "udev-util.h"
static int show_sysfs_one(
struct udev *udev,
@ -143,9 +144,9 @@ static int show_sysfs_one(
}
int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
struct udev *udev;
_cleanup_udev_unref_ struct udev *udev;
_cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
struct udev_list_entry *first = NULL;
struct udev_enumerate *e;
int r;
if (n_columns <= 0)
@ -162,10 +163,8 @@ int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
return -ENOMEM;
e = udev_enumerate_new(udev);
if (!e) {
r = -ENOMEM;
goto finish;
}
if (!e)
return ENOMEM;
if (!streq(seat, "seat0"))
r = udev_enumerate_add_match_tag(e, seat);
@ -173,22 +172,15 @@ int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
r = udev_enumerate_add_match_tag(e, "seat");
if (r < 0)
goto finish;
return r;
r = udev_enumerate_scan_devices(e);
if (r < 0)
goto finish;
return r;
first = udev_enumerate_get_list_entry(e);
if (first)
show_sysfs_one(udev, seat, &first, "/", prefix, n_columns);
finish:
if (e)
udev_enumerate_unref(e);
if (udev)
udev_unref(udev);
return r;
}

View File

@ -27,13 +27,14 @@
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <libudev.h>
#include "log.h"
#include "readahead-common.h"
#include "util.h"
#include "missing.h"
#include "fileio.h"
#include "libudev.h"
#include "udev-util.h"
int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
assert(fd >= 0);
@ -60,9 +61,9 @@ int file_verify(int fd, const char *fn, off_t file_size_max, struct stat *st) {
int fs_on_ssd(const char *p) {
struct stat st;
struct udev *udev = NULL;
struct udev_device *udev_device = NULL, *look_at = NULL;
bool b = false;
_cleanup_udev_unref_ struct udev *udev = NULL;
_cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
struct udev_device *look_at = NULL;
const char *devtype, *rotational, *model, *id;
int r;
@ -128,7 +129,7 @@ int fs_on_ssd(const char *p) {
udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
if (!udev_device)
goto finish;
return false;
devtype = udev_device_get_property_value(udev_device, "DEVTYPE");
if (devtype && streq(devtype, "partition"))
@ -137,46 +138,34 @@ int fs_on_ssd(const char *p) {
look_at = udev_device;
if (!look_at)
goto finish;
return false;
/* First, try high-level property */
id = udev_device_get_property_value(look_at, "ID_SSD");
if (id) {
b = streq(id, "1");
goto finish;
}
if (id)
return streq(id, "1");
/* Second, try kernel attribute */
rotational = udev_device_get_sysattr_value(look_at, "queue/rotational");
if (rotational) {
b = streq(rotational, "0");
goto finish;
}
if (rotational)
return streq(rotational, "0");
/* Finally, fallback to heuristics */
look_at = udev_device_get_parent(look_at);
if (!look_at)
goto finish;
return false;
model = udev_device_get_sysattr_value(look_at, "model");
if (model)
b = !!strstr(model, "SSD");
return !!strstr(model, "SSD");
finish:
if (udev_device)
udev_device_unref(udev_device);
if (udev)
udev_unref(udev);
return b;
return false;
}
int fs_on_read_only(const char *p) {
struct stat st;
struct udev *udev = NULL;
struct udev_device *udev_device = NULL;
bool b = false;
_cleanup_udev_unref_ struct udev *udev = NULL;
_cleanup_udev_device_unref_ struct udev_device *udev_device = NULL;
const char *read_only;
assert(p);
@ -187,24 +176,19 @@ int fs_on_read_only(const char *p) {
if (major(st.st_dev) == 0)
return false;
if (!(udev = udev_new()))
udev = udev_new();
if (!udev)
return -ENOMEM;
if (!(udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev)))
goto finish;
udev_device = udev_device_new_from_devnum(udev, 'b', st.st_dev);
if (!udev_device)
return false;
if ((read_only = udev_device_get_sysattr_value(udev_device, "ro")))
if ((b = streq(read_only, "1")))
goto finish;
read_only = udev_device_get_sysattr_value(udev_device, "ro");
if (read_only)
return streq(read_only, "1");
finish:
if (udev_device)
udev_device_unref(udev_device);
if (udev)
udev_unref(udev);
return b;
return false;
}
bool enough_ram(void) {

View File

@ -49,8 +49,5 @@ int fdset_iterate(FDSet *s, Iterator *i);
#define FDSET_FOREACH(fd, fds, i) \
for ((i) = ITERATOR_FIRST, (fd) = fdset_iterate((fds), &(i)); (fd) >= 0; (fd) = fdset_iterate((fds), &(i)))
static inline void fdset_freep(FDSet **fds) {
if (*fds)
fdset_free(*fds);
}
define_trivial_cleanup_func(FDSet*, fdset_free)
#define _cleanup_fdset_free_ _cleanup_(fdset_freep)

View File

@ -44,10 +44,8 @@ typedef struct {
Hashmap *have_installed;
} InstallContext;
#define _cleanup_lookup_paths_free_ \
__attribute__((cleanup(lookup_paths_free)))
#define _cleanup_install_context_done_ \
__attribute__((cleanup(install_context_done)))
#define _cleanup_lookup_paths_free_ _cleanup_(lookup_paths_free)
#define _cleanup_install_context_done_ _cleanup_(install_context_done)
static int lookup_paths_init_from_scope(LookupPaths *paths, UnitFileScope scope) {
assert(paths);

View File

@ -28,19 +28,13 @@
* for each set use. */
#include "hashmap.h"
#include "util.h"
typedef struct Set Set;
Set *set_new(hash_func_t hash_func, compare_func_t compare_func);
void set_free(Set* s);
static inline void set_freep(Set **s) {
set_free(*s);
}
void set_free_free(Set *s);
static inline void set_free_freep(Set **s) {
set_free_free(*s);
}
Set* set_copy(Set *s);
int set_ensure_allocated(Set **s, hash_func_t hash_func, compare_func_t compare_func);
@ -79,5 +73,7 @@ char **set_get_strv(Set *s);
#define SET_FOREACH_BACKWARDS(e, s, i) \
for ((i) = ITERATOR_LAST, (e) = set_iterate_backwards((s), &(i)); (e); (e) = set_iterate_backwards((s), &(i)))
define_trivial_cleanup_func(Set*, set_free)
define_trivial_cleanup_func(Set*, set_free_free)
#define _cleanup_set_free_ _cleanup_(set_freep)
#define _cleanup_set_free_free_ _cleanup_(set_free_freep)

View File

@ -24,16 +24,13 @@
#include <stdarg.h>
#include <stdbool.h>
#include "macro.h"
#include "util.h"
char *strv_find(char **l, const char *name) _pure_;
char *strv_find_prefix(char **l, const char *name) _pure_;
void strv_free(char **l);
static inline void strv_freep(char ***l) {
strv_free(*l);
}
define_trivial_cleanup_func(char**, strv_free)
#define _cleanup_strv_free_ _cleanup_(strv_freep)
char **strv_copy(char * const *l);

37
src/shared/udev-util.h Normal file
View File

@ -0,0 +1,37 @@
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#pragma once
/***
This file is part of systemd.
Copyright 2013 Zbigniew Jędrzejewski-Szmek
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "udev.h"
#include "util.h"
define_trivial_cleanup_func(struct udev*, udev_unref)
define_trivial_cleanup_func(struct udev_device*, udev_device_unref)
define_trivial_cleanup_func(struct udev_enumerate*, udev_enumerate_unref)
define_trivial_cleanup_func(struct udev_event*, udev_event_unref)
define_trivial_cleanup_func(struct udev_rules*, udev_rules_unref)
#define _cleanup_udev_unref_ _cleanup_(udev_unrefp)
#define _cleanup_udev_device_unref_ _cleanup_(udev_device_unrefp)
#define _cleanup_udev_enumerate_unref_ _cleanup_(udev_enumerate_unrefp)
#define _cleanup_udev_event_unref_ _cleanup_(udev_event_unrefp)
#define _cleanup_udev_rules_unref_ _cleanup_(udev_rules_unrefp)

View File

@ -556,42 +556,33 @@ static inline void freep(void *p) {
free(*(void**) p);
}
static inline void fclosep(FILE **f) {
if (*f)
fclose(*f);
}
static inline void pclosep(FILE **f) {
if (*f)
pclose(*f);
}
#define define_trivial_cleanup_func(type, func) \
static inline void func##p(type *p) { \
if (*p) \
func(*p); \
} \
static inline void closep(int *fd) {
if (*fd >= 0)
close_nointr_nofail(*fd);
}
static inline void closedirp(DIR **d) {
if (*d)
closedir(*d);
}
static inline void umaskp(mode_t *u) {
umask(*u);
}
static inline void endmntentp(FILE **f) {
if (*f)
endmntent(*f);
}
define_trivial_cleanup_func(FILE*, fclose)
define_trivial_cleanup_func(FILE*, pclose)
define_trivial_cleanup_func(DIR*, closedir)
define_trivial_cleanup_func(FILE*, endmntent)
#define _cleanup_free_ _cleanup_(freep)
#define _cleanup_fclose_ _cleanup_(fclosep)
#define _cleanup_pclose_ _cleanup_(pclosep)
#define _cleanup_close_ _cleanup_(closep)
#define _cleanup_closedir_ _cleanup_(closedirp)
#define _cleanup_umask_ _cleanup_(umaskp)
#define _cleanup_globfree_ _cleanup_(globfree)
#define _cleanup_fclose_ _cleanup_(fclosep)
#define _cleanup_pclose_ _cleanup_(pclosep)
#define _cleanup_closedir_ _cleanup_(closedirp)
#define _cleanup_endmntent_ _cleanup_(endmntentp)
_malloc_ _alloc_(1, 2) static inline void *malloc_multiply(size_t a, size_t b) {

View File

@ -29,6 +29,7 @@
#include <sys/epoll.h>
#include "libudev.h"
#include "udev-util.h"
#include "util.h"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
@ -117,7 +118,7 @@ static void print_device(struct udev_device *device)
static int test_device(struct udev *udev, const char *syspath)
{
struct udev_device *device;
_cleanup_udev_device_unref_ struct udev_device *device;
printf("looking at device: %s\n", syspath);
device = udev_device_new_from_syspath(udev, syspath);
@ -126,13 +127,13 @@ static int test_device(struct udev *udev, const char *syspath)
return -1;
}
print_device(device);
udev_device_unref(device);
return 0;
}
static int test_device_parents(struct udev *udev, const char *syspath)
{
struct udev_device *device;
_cleanup_udev_device_unref_ struct udev_device *device;
struct udev_device *device_parent;
printf("looking at device: %s\n", syspath);
@ -153,7 +154,6 @@ static int test_device_parents(struct udev *udev, const char *syspath)
print_device(device_parent);
device_parent = udev_device_get_parent(device_parent);
} while (device_parent != NULL);
udev_device_unref(device);
return 0;
}

View File

@ -34,6 +34,7 @@
#include "missing.h"
#include "udev.h"
#include "udev-util.h"
void udev_main_log(struct udev *udev, int priority,
const char *file, int line, const char *fn,
@ -82,10 +83,10 @@ out:
int main(int argc, char *argv[])
{
struct udev *udev;
struct udev_event *event = NULL;
struct udev_device *dev = NULL;
struct udev_rules *rules = NULL;
_cleanup_udev_unref_ struct udev *udev = NULL;
_cleanup_udev_event_unref_ struct udev_event *event = NULL;
_cleanup_udev_device_unref_ struct udev_device *dev = NULL;
_cleanup_udev_rules_unref_ struct udev_rules *rules = NULL;
char syspath[UTIL_PATH_SIZE];
const char *devpath;
const char *action;
@ -98,7 +99,8 @@ int main(int argc, char *argv[])
udev = udev_new();
if (udev == NULL)
exit(EXIT_FAILURE);
return EXIT_FAILURE;
log_debug("version %s\n", VERSION);
label_init("/dev");
@ -160,12 +162,7 @@ int main(int argc, char *argv[])
out:
if (event != NULL && event->fd_signal >= 0)
close(event->fd_signal);
udev_event_unref(event);
udev_device_unref(dev);
udev_rules_unref(rules);
label_finish();
udev_unref(udev);
if (err != 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
return err ? EXIT_FAILURE : EXIT_SUCCESS;
}

View File

@ -995,10 +995,7 @@ static void item_free(Item *i) {
free(i);
}
static inline void item_freep(Item **i) {
if (*i)
item_free(*i);
}
define_trivial_cleanup_func(Item*, item_free)
#define _cleanup_item_free_ _cleanup_(item_freep)
static bool item_equal(Item *a, Item *b) {