[hackers] [skvm] Code formatting. || Dimitrios Papastamos <stateless [at] archlinux.us>

From: <hg_AT_suckless.org>
Date: Sun, 4 Oct 2009 11:51:18 +0000 (UTC)

changeset: 23:86db9c494a02
tag: tip
user: Dimitrios Papastamos <stateless [at] archlinux.us>
date: Sun Oct 04 12:51:13 2009 +0100
files: .astylerc skvm.c
description:
Code formatting.

diff -r d9636e8385d1 -r 86db9c494a02 .astylerc
--- a/.astylerc Sun Oct 04 02:11:12 2009 +0100
+++ b/.astylerc Sun Oct 04 12:51:13 2009 +0100
@@ -1,4 +1,4 @@
-indent=spaces=3
+indent=spaces=4
 indent-switches
 indent-classes
 indent-cases
@@ -6,7 +6,7 @@
 indent-preprocessor
 max-instatement-indent=80
 suffix=none
-brackets=attach
+brackets=linux
 pad=oper
 one-line=keep-statements
 convert-tabs
diff -r d9636e8385d1 -r 86db9c494a02 skvm.c
--- a/skvm.c Sun Oct 04 02:11:12 2009 +0100
+++ b/skvm.c Sun Oct 04 12:51:13 2009 +0100
@@ -45,28 +45,28 @@
 #define LOCKMODE (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
 #define FREE_WRAP(ptr) do { free(ptr); ptr = NULL; } while (0)
 #define INIT_NODE(p1, p2) \
- do { \
- p1->mountp = p2->mountp; \
- p1->did = p2->did; \
- p1->dev = p2->dev; \
- p1->label = p2->label; \
- p1->fstype = p2->fstype; \
- p1->opt = p2->opt; \
- p1->volume = p2->volume; \
- p1->drive = p2->drive; \
- } while (0)
+ do { \
+ p1->mountp = p2->mountp; \
+ p1->did = p2->did; \
+ p1->dev = p2->dev; \
+ p1->label = p2->label; \
+ p1->fstype = p2->fstype; \
+ p1->opt = p2->opt; \
+ p1->volume = p2->volume; \
+ p1->drive = p2->drive; \
+ } while (0)
 #define EXEC_CMD(args) \
- g_spawn_sync( \
- "/", \
- (args), \
- (gchar **)NULL, \
- 0, \
- (GSpawnChildSetupFunc)NULL, \
- (gpointer)NULL, \
- (gchar **)NULL, \
- (gchar **)NULL, \
- (gint *)NULL, \
- (GError **)NULL)
+ g_spawn_sync( \
+ "/", \
+ (args), \
+ (gchar **)NULL, \
+ 0, \
+ (GSpawnChildSetupFunc)NULL, \
+ (gpointer)NULL, \
+ (gchar **)NULL, \
+ (gchar **)NULL, \
+ (gint *)NULL, \
+ (GError **)NULL)
 
 /* ============================
  * = Structs
@@ -74,19 +74,19 @@
  */
 
 struct device_t {
- char *mountp; /* mount point */
- char *did; /* volume's unique id */
- char *dev; /* device associated with this volume */
- char *label; /* volume's label */
- char *fstype; /* filesystem type */
- char *opt; /* mount options */
- int use_fstab; /* true if this device has an fstab entry
+ char *mountp; /* mount point */
+ char *did; /* volume's unique id */
+ char *dev; /* device associated with this volume */
+ char *label; /* volume's label */
+ char *fstype; /* filesystem type */
+ char *opt; /* mount options */
+ int use_fstab; /* true if this device has an fstab entry
                                  before inserting the device */
- int should_remove_entry; /* true if upon removal of the device
+ int should_remove_entry; /* true if upon removal of the device
                                  an entry in fstab needs to be removed */
- LibHalVolume *volume;
- LibHalDrive *drive;
- struct device_t *next;
+ LibHalVolume *volume;
+ LibHalDrive *drive;
+ struct device_t *next;
 };
 
 /* ============================
@@ -154,214 +154,214 @@
 int
 main(int argc, char *argv[])
 {
- int c;
+ int c;
 
- static struct option long_options[] = {
- { "debug", 0, (int *)NULL, 'd' }
- };
+ static struct option long_options[] = {
+ { "debug", 0, (int *)NULL, 'd' }
+ };
 
- while ((c = getopt_long(argc, argv, "d", long_options,
- (int *)NULL)) != -1) {
- switch (c) {
- case 'd':
- debug_mode_flag = 1;
- break;
- }
- }
+ while ((c = getopt_long(argc, argv, "d", long_options,
+ (int *)NULL)) != -1) {
+ switch (c) {
+ case 'd':
+ debug_mode_flag = 1;
+ break;
+ }
+ }
 
- if (debug_mode_flag)
- setvbuf(stdout, (char *)NULL, _IONBF, 0); /* avoid surprises */
+ if (debug_mode_flag)
+ setvbuf(stdout, (char *)NULL, _IONBF, 0); /* avoid surprises */
 
- if (init_skvm() < 0)
- return EXIT_FAILURE;
+ if (init_skvm() < 0)
+ return EXIT_FAILURE;
 
- openlog(argv[0], LOG_PID, LOG_DAEMON);
- signal(SIGTERM, cleanup);
+ openlog(argv[0], LOG_PID, LOG_DAEMON);
+ signal(SIGTERM, cleanup);
 
- if (!debug_mode_flag) {
- if (daemon(0, 0) < 0 || is_running()) {
- deinit();
- return EXIT_FAILURE;
- }
- }
+ if (!debug_mode_flag) {
+ if (daemon(0, 0) < 0 || is_running()) {
+ deinit();
+ return EXIT_FAILURE;
+ }
+ }
 
- loop = g_main_loop_new((GMainContext *)NULL, FALSE);
- g_main_run(loop);
+ loop = g_main_loop_new((GMainContext *)NULL, FALSE);
+ g_main_run(loop);
 
- deinit();
- return EXIT_SUCCESS;
+ deinit();
+ return EXIT_SUCCESS;
 }
 
 static int
 init_skvm(void)
 {
- DBusError error;
+ DBusError error;
 
- dbus_error_init(&error);
- if (init_dbus(&error) < 0) {
- if (dbus_error_is_set(&error)) {
- syslog(LOG_ERR, "%s:%d: %s:%s", __FILE__, __LINE__,
- error.name, error.message);
- dbus_error_free(&error);
- return -1;
- }
- }
+ dbus_error_init(&error);
+ if (init_dbus(&error) < 0) {
+ if (dbus_error_is_set(&error)) {
+ syslog(LOG_ERR, "%s:%d: %s:%s", __FILE__, __LINE__,
+ error.name, error.message);
+ dbus_error_free(&error);
+ return -1;
+ }
+ }
 
- return init_hal();
+ return init_hal();
 }
 
 static int
 init_dbus(DBusError *error)
 {
- if (!(dbus_conn = dbus_bus_get(DBUS_BUS_SYSTEM, error)))
- return -1;
- dbus_connection_setup_with_g_main (dbus_conn, (GMainContext *)NULL);
- dbus_connection_flush(dbus_conn);
- return 0;
+ if (!(dbus_conn = dbus_bus_get(DBUS_BUS_SYSTEM, error)))
+ return -1;
+ dbus_connection_setup_with_g_main (dbus_conn, (GMainContext *)NULL);
+ dbus_connection_flush(dbus_conn);
+ return 0;
 }
 
 static int
 init_hal(void)
 {
- DBusError error;
+ DBusError error;
 
- if (!(hal_ctx = libhal_ctx_new()))
- return -1;
+ if (!(hal_ctx = libhal_ctx_new()))
+ return -1;
 
- libhal_ctx_set_dbus_connection (hal_ctx, dbus_conn);
+ libhal_ctx_set_dbus_connection (hal_ctx, dbus_conn);
 
- libhal_ctx_set_device_added(hal_ctx, device_added);
- libhal_ctx_set_device_removed(hal_ctx, device_removed);
+ libhal_ctx_set_device_added(hal_ctx, device_added);
+ libhal_ctx_set_device_removed(hal_ctx, device_removed);
 
- dbus_error_init(&error);
- if (!libhal_device_property_watch_all(hal_ctx, &error)) {
- if (dbus_error_is_set(&error)) {
- syslog(LOG_ERR, "%s:%d: %s:%s", __FILE__, __LINE__,
- error.name, error.message);
- dbus_error_free (&error);
- libhal_ctx_free (hal_ctx);
- return -1;
- }
- }
+ dbus_error_init(&error);
+ if (!libhal_device_property_watch_all(hal_ctx, &error)) {
+ if (dbus_error_is_set(&error)) {
+ syslog(LOG_ERR, "%s:%d: %s:%s", __FILE__, __LINE__,
+ error.name, error.message);
+ dbus_error_free (&error);
+ libhal_ctx_free (hal_ctx);
+ return -1;
+ }
+ }
 
- if (!libhal_ctx_init(hal_ctx, &error)) {
- if (dbus_error_is_set(&error)) {
- syslog(LOG_ERR, "%s:%d: %s:%s", __FILE__, __LINE__,
- error.name, error.message);
- dbus_error_free(&error);
- libhal_ctx_free(hal_ctx);
- return -1;
- }
- }
+ if (!libhal_ctx_init(hal_ctx, &error)) {
+ if (dbus_error_is_set(&error)) {
+ syslog(LOG_ERR, "%s:%d: %s:%s", __FILE__, __LINE__,
+ error.name, error.message);
+ dbus_error_free(&error);
+ libhal_ctx_free(hal_ctx);
+ return -1;
+ }
+ }
 
- return 0;
+ return 0;
 }
 
 static void
 deinit(void)
 {
- struct device_t *iter, *tmp;
+ struct device_t *iter, *tmp;
 
- if (hal_ctx) {
- libhal_ctx_shutdown(hal_ctx, (DBusError *)NULL);
- libhal_ctx_free(hal_ctx);
- }
+ if (hal_ctx) {
+ libhal_ctx_shutdown(hal_ctx, (DBusError *)NULL);
+ libhal_ctx_free(hal_ctx);
+ }
 
- dbus_connection_unref(dbus_conn);
+ dbus_connection_unref(dbus_conn);
 
- iter = head;
+ iter = head;
 
- while (iter) {
- tmp = iter;
- if (!is_mounted(iter->dev))
- /* don't care to check the return values */
- do_umount(iter);
- rmdir(iter->mountp);
- if (iter->should_remove_entry)
- remove_fstab_entry(iter);
- iter = iter->next;
- free_device(tmp);
- }
+ while (iter) {
+ tmp = iter;
+ if (!is_mounted(iter->dev))
+ /* don't care to check the return values */
+ do_umount(iter);
+ rmdir(iter->mountp);
+ if (iter->should_remove_entry)
+ remove_fstab_entry(iter);
+ iter = iter->next;
+ free_device(tmp);
+ }
 
- if (pid_fd >= 0)
- if (close(pid_fd) < 0) /* releases lock */
- syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__, strerror(errno));
- closelog();
+ if (pid_fd >= 0)
+ if (close(pid_fd) < 0) /* releases lock */
+ syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__, strerror(errno));
+ closelog();
 }
 
 /* Callback function, called when a new device has been inserted. */
 static void
 device_added(LibHalContext *context, const char *did)
 {
- const char *dudi, *fstype;
- char *dev, *mountp, *mountable, *label, *locked_reason;
- LibHalVolume *volume;
- LibHalDrive *drive;
- struct device_t *device;
+ const char *dudi, *fstype;
+ char *dev, *mountp, *mountable, *label, *locked_reason;
+ LibHalVolume *volume;
+ LibHalDrive *drive;
+ struct device_t *device;
 
- if (libhal_device_property_exists(context, did, "info.locked",
- (DBusError *)NULL)
- && libhal_device_get_property_bool(context, did, "info.locked",
- (DBusError *)NULL)) {
- if (debug_mode_flag) {
- locked_reason = libhal_device_get_property_string(
- context, did, "info.locked.reason",
- (DBusError *)NULL);
- if (locked_reason) {
- if (debug_mode_flag)
- printf("%s%d: %s\n", __FILE__, __LINE__, locked_reason);
- libhal_free_string(locked_reason);
- }
- }
- return;
- }
+ if (libhal_device_property_exists(context, did, "info.locked",
+ (DBusError *)NULL)
+ && libhal_device_get_property_bool(context, did, "info.locked",
+ (DBusError *)NULL)) {
+ if (debug_mode_flag) {
+ locked_reason = libhal_device_get_property_string(
+ context, did, "info.locked.reason",
+ (DBusError *)NULL);
+ if (locked_reason) {
+ if (debug_mode_flag)
+ printf("%s%d: %s\n", __FILE__, __LINE__, locked_reason);
+ libhal_free_string(locked_reason);
+ }
+ }
+ return;
+ }
 
- if (!libhal_device_query_capability(context, did, "volume",
- (DBusError *)NULL))
- return;
- label = libhal_device_get_property_string(context, did, "volume.label",
- (DBusError *)NULL);
- if (!(mountable = libhal_device_get_property_string(
- context, did, "volume.fsusage", (DBusError *)NULL))
- || strcmp(mountable, "filesystem"))
- goto out;
- if (!(volume = libhal_volume_from_udi(context, did)))
- goto out;
- if (!(dudi = libhal_volume_get_storage_device_udi(volume)))
- goto out;
- if (!(drive = libhal_drive_from_udi(context, dudi)))
- goto out;
- if (!libhal_drive_is_hotpluggable(drive)
- && !libhal_drive_uses_removable_media(drive))
- goto out;
- if (!(fstype = libhal_volume_get_fstype(volume)))
- goto out;
- if (!(dev = libhal_device_get_property_string(context, did, "block.device",
- (DBusError *)NULL)))
- goto out;
- mountp = get_mount_point(dev, label);
- if (!mountp)
- goto out;
- device = get_device(mountp, did, dev, label, fstype, volume, drive);
- if (!device)
- goto out;
- consider_fstab(device);
- if (file_exists(device->mountp) < 0)
- mkdir(device->mountp, 0750);
- do_mount(device) < 0 ? free_device(device) : add_to_device_list(device);
+ if (!libhal_device_query_capability(context, did, "volume",
+ (DBusError *)NULL))
+ return;
+ label = libhal_device_get_property_string(context, did, "volume.label",
+ (DBusError *)NULL);
+ if (!(mountable = libhal_device_get_property_string(
+ context, did, "volume.fsusage", (DBusError *)NULL))
+ || strcmp(mountable, "filesystem"))
+ goto out;
+ if (!(volume = libhal_volume_from_udi(context, did)))
+ goto out;
+ if (!(dudi = libhal_volume_get_storage_device_udi(volume)))
+ goto out;
+ if (!(drive = libhal_drive_from_udi(context, dudi)))
+ goto out;
+ if (!libhal_drive_is_hotpluggable(drive)
+ && !libhal_drive_uses_removable_media(drive))
+ goto out;
+ if (!(fstype = libhal_volume_get_fstype(volume)))
+ goto out;
+ if (!(dev = libhal_device_get_property_string(context, did, "block.device",
+ (DBusError *)NULL)))
+ goto out;
+ mountp = get_mount_point(dev, label);
+ if (!mountp)
+ goto out;
+ device = get_device(mountp, did, dev, label, fstype, volume, drive);
+ if (!device)
+ goto out;
+ consider_fstab(device);
+ if (file_exists(device->mountp) < 0)
+ mkdir(device->mountp, 0750);
+ do_mount(device) < 0 ? free_device(device) : add_to_device_list(device);
 
- if (device) {
- if (!add_fstab_entry(device))
- device->should_remove_entry = 1;
- if (debug_mode_flag)
- debug_dump_device(device);
- }
+ if (device) {
+ if (!add_fstab_entry(device))
+ device->should_remove_entry = 1;
+ if (debug_mode_flag)
+ debug_dump_device(device);
+ }
 
 out:
- if (mountable)
- libhal_free_string(mountable);
- if (label)
- libhal_free_string(label);
+ if (mountable)
+ libhal_free_string(mountable);
+ if (label)
+ libhal_free_string(label);
 }
 
 /* Callback function, called when a device has been removed. */
@@ -369,171 +369,171 @@
 device_removed(LibHalContext *context __attribute__ ((unused)),
                const char *did)
 {
- char *mountp = NULL;
- struct device_t *iter = head, *prev = NULL;
+ char *mountp = NULL;
+ struct device_t *iter = head, *prev = NULL;
 
- while (iter) {
- if (!strcmp(did, iter->did)) {
- mountp = iter->mountp;
- if (!file_exists(mountp)) {
- if (!is_mounted(iter->dev))
- if (do_umount(iter))
- syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__,
- strerror(errno));
- if (rmdir(mountp) < 0)
- syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__,
- strerror(errno));
- if (iter->should_remove_entry && remove_fstab_entry(iter))
- syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__,
- "cannot remove fstab entry");
- remove_from_device_list(prev, iter);
- }
- return;
- }
- prev = iter;
- iter = iter->next;
- }
+ while (iter) {
+ if (!strcmp(did, iter->did)) {
+ mountp = iter->mountp;
+ if (!file_exists(mountp)) {
+ if (!is_mounted(iter->dev))
+ if (do_umount(iter))
+ syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__,
+ strerror(errno));
+ if (rmdir(mountp) < 0)
+ syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__,
+ strerror(errno));
+ if (iter->should_remove_entry && remove_fstab_entry(iter))
+ syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__,
+ "cannot remove fstab entry");
+ remove_from_device_list(prev, iter);
+ }
+ return;
+ }
+ prev = iter;
+ iter = iter->next;
+ }
 }
 
 static struct device_t *
 get_device(char *mountp, const char *did, char *dev, char *label,
            const char *fstype, LibHalVolume *volume, LibHalDrive *drive) {
- struct device_t *device = malloc(sizeof(*device));
+ struct device_t *device = malloc(sizeof(*device));
 
- if (device) {
- device->mountp = mountp;
- device->did = strdup(did);
- device->dev = dev;
- device->label = strdup(label);
- device->fstype = strdup(fstype);
- device->opt = NULL;
- device->volume = volume;
- device->drive = drive;
- device->next = NULL;
- device->use_fstab = 0;
- device->should_remove_entry = 0;
- if (!device->did || !device->label || !device->fstype) {
- free_device(device);
- device = NULL;
- }
- }
+ if (device) {
+ device->mountp = mountp;
+ device->did = strdup(did);
+ device->dev = dev;
+ device->label = strdup(label);
+ device->fstype = strdup(fstype);
+ device->opt = NULL;
+ device->volume = volume;
+ device->drive = drive;
+ device->next = NULL;
+ device->use_fstab = 0;
+ device->should_remove_entry = 0;
+ if (!device->did || !device->label || !device->fstype) {
+ free_device(device);
+ device = NULL;
+ }
+ }
 
- return device;
+ return device;
 }
 
 static void
 add_to_device_list(struct device_t *device)
 {
- if (!device)
- return;
+ if (!device)
+ return;
 
- if (!head) {
- head = device;
- tail = head;
- INIT_NODE(head, device);
- } else {
- tail->next = device;
- tail = tail->next;
- INIT_NODE(tail, device);
- }
+ if (!head) {
+ head = device;
+ tail = head;
+ INIT_NODE(head, device);
+ } else {
+ tail->next = device;
+ tail = tail->next;
+ INIT_NODE(tail, device);
+ }
 }
 
 static void
 remove_from_device_list(struct device_t *prev, struct device_t *curr)
 {
- if (!curr)
- return;
+ if (!curr)
+ return;
 
- if (curr == head) {
- head = curr->next;
- } else if (curr == tail) {
- tail = prev;
- tail->next = NULL;
- } else {
- if (!prev)
- return;
- prev->next = curr->next;
- }
+ if (curr == head) {
+ head = curr->next;
+ } else if (curr == tail) {
+ tail = prev;
+ tail->next = NULL;
+ } else {
+ if (!prev)
+ return;
+ prev->next = curr->next;
+ }
 
- free_device(curr);
+ free_device(curr);
 }
 
 static void
 free_device(struct device_t *device)
 {
- if (!device)
- return;
+ if (!device)
+ return;
 
- FREE_WRAP(device->mountp);
- FREE_WRAP(device->did);
- if (device->dev)
- libhal_free_string(device->dev);
- FREE_WRAP(device->label);
- FREE_WRAP(device->fstype);
- FREE_WRAP(device->opt);
- if (device->volume)
- libhal_volume_free(device->volume);
- if (device->drive)
- libhal_drive_free(device->drive);
+ FREE_WRAP(device->mountp);
+ FREE_WRAP(device->did);
+ if (device->dev)
+ libhal_free_string(device->dev);
+ FREE_WRAP(device->label);
+ FREE_WRAP(device->fstype);
+ FREE_WRAP(device->opt);
+ if (device->volume)
+ libhal_volume_free(device->volume);
+ if (device->drive)
+ libhal_drive_free(device->drive);
 
- FREE_WRAP(device);
+ FREE_WRAP(device);
 }
 
 static int
 is_mounted(const char *dev)
 {
- FILE *mtab;
- struct mntent *entry;
+ FILE *mtab;
+ struct mntent *entry;
 
- mtab = setmntent("/etc/mtab", "r");
- if (!mtab)
- return -1;
+ mtab = setmntent("/etc/mtab", "r");
+ if (!mtab)
+ return -1;
 
- while ((entry = getmntent(mtab))) {
- if (!strcmp(entry->mnt_fsname, dev)) {
- endmntent(mtab);
- return 0;
- }
- }
+ while ((entry = getmntent(mtab))) {
+ if (!strcmp(entry->mnt_fsname, dev)) {
+ endmntent(mtab);
+ return 0;
+ }
+ }
 
- endmntent(mtab);
- return -1;
+ endmntent(mtab);
+ return -1;
 }
 
 static char *
 get_mount_point(const char *dev, const char *label)
 {
- const char *extra;
- char *mountp, *dev_tmp;
- size_t len;
- struct dirent *dirent;
- DIR *dir;
+ const char *extra;
+ char *mountp, *dev_tmp;
+ size_t len;
+ struct dirent *dirent;
+ DIR *dir;
 
- if (!is_mounted(dev) || !(dev_tmp = strrchr(dev, '/')))
- return NULL;
+ if (!is_mounted(dev) || !(dev_tmp = strrchr(dev, '/')))
+ return NULL;
 
- ++dev_tmp;
- len = strlen(dev_tmp) + 1 + strlen(BASE_MNT_DIR);
- extra = dev_tmp;
- if (label && strcmp(label, "")) {
- if ((dir = opendir(BASE_MNT_DIR))) {
- dirent = readdir(dir);
- while (dirent) {
- if (!strcmp(dirent->d_name, label))
- goto out;
+ ++dev_tmp;
+ len = strlen(dev_tmp) + 1 + strlen(BASE_MNT_DIR);
+ extra = dev_tmp;
+ if (label && strcmp(label, "")) {
+ if ((dir = opendir(BASE_MNT_DIR))) {
             dirent = readdir(dir);
- }
- }
+ while (dirent) {
+ if (!strcmp(dirent->d_name, label))
+ goto out;
+ dirent = readdir(dir);
+ }
+ }
 
- len = strlen(label) + 1 + strlen(BASE_MNT_DIR);
- extra = label;
- }
+ len = strlen(label) + 1 + strlen(BASE_MNT_DIR);
+ extra = label;
+ }
 
 out:
- mountp = malloc(len);
- if (mountp)
- snprintf(mountp, len, "%s%s", BASE_MNT_DIR, extra);
- return mountp;
+ mountp = malloc(len);
+ if (mountp)
+ snprintf(mountp, len, "%s%s", BASE_MNT_DIR, extra);
+ return mountp;
 }
 
 /*
@@ -543,29 +543,29 @@
 static int
 resolve_symlink(const char *restrict symlnk, char *restrict d, size_t d_len)
 {
- char file[d_len], buf[d_len];
- ssize_t len;
- size_t f_len = strlen(symlnk) + 1;
- struct stat bf;
+ char file[d_len], buf[d_len];
+ ssize_t len;
+ size_t f_len = strlen(symlnk) + 1;
+ struct stat bf;
 
- if (f_len > d_len)
- return -1;
- memcpy(file, symlnk, f_len);
- do {
- len = readlink(file, buf, sizeof(buf) - 1);
- if (len < 0) {
- if (lstat(file, &bf) < 0)
+ if (f_len > d_len)
+ return -1;
+ memcpy(file, symlnk, f_len);
+ do {
+ len = readlink(file, buf, sizeof(buf) - 1);
+ if (len < 0) {
+ if (lstat(file, &bf) < 0)
+ return -1;
+ if (!file_exists(file) && !S_ISLNK(bf.st_mode))
+ break;
             return -1;
- if (!file_exists(file) && !S_ISLNK(bf.st_mode))
- break;
- return -1;
- }
- buf[len] = '\0';
- memcpy(file, buf, len + 1);
- } while (1);
+ }
+ buf[len] = '\0';
+ memcpy(file, buf, len + 1);
+ } while (1);
 
- memcpy(d, file, strlen(file) + 1);
- return 0;
+ memcpy(d, file, strlen(file) + 1);
+ return 0;
 }
 
 /*
@@ -578,195 +578,195 @@
 static void
 consider_fstab(struct device_t *device)
 {
- FILE *fp, *mtab;
- struct mntent *entry, *i;
- char rlink[1024], *tmp, *str, *did;
- size_t len;
+ FILE *fp, *mtab;
+ struct mntent *entry, *i;
+ char rlink[1024], *tmp, *str, *did;
+ size_t len;
 
- if (!device)
- return;
+ if (!device)
+ return;
 
- if (!(fp = setmntent("/etc/fstab", "r")))
- return;
+ if (!(fp = setmntent("/etc/fstab", "r")))
+ return;
 
- /* retrieve the uuid of the device */
- if ((did = strrchr(device->did, '/'))) {
- did = strstr(did, "uuid");
- if (did) {
- did = strchr(did, '_');
- if (did)
- ++did;
- }
- }
+ /* retrieve the uuid of the device */
+ if ((did = strrchr(device->did, '/'))) {
+ did = strstr(did, "uuid");
+ if (did) {
+ did = strchr(did, '_');
+ if (did)
+ ++did;
+ }
+ }
 
- while ((entry = getmntent(fp))) {
- /* check if we have an entry in fstab that suits our needs */
- if (!strcmp(device->dev, entry->mnt_fsname)
- || (did && strstr(entry->mnt_fsname, "UUID=")
- && strstr(entry->mnt_fsname, did))
- || (strstr(entry->mnt_fsname, "LABEL=")
- && strstr(entry->mnt_fsname, device->label))
- || (!resolve_symlink(entry->mnt_fsname, rlink, 1024)
- && !strcmp(rlink, device->dev))) {
- tmp = device->mountp;
- device->mountp = strdup(entry->mnt_dir);
- device->opt = strdup(entry->mnt_opts);
- if (!device->opt || !device->mountp) {
- FREE_WRAP(device->mountp);
- FREE_WRAP(device->opt);
- device->mountp = tmp;
- } else {
- if (!(str = strrchr(device->dev, '/')))
- break; /* this will cause mount points to be stacked */
- ++str;
- if (!(mtab = setmntent("/etc/mtab", "r")))
- goto out;
- device->use_fstab = 1;
- len = strlen(str) + 1 + strlen(BASE_MNT_DIR);
+ while ((entry = getmntent(fp))) {
+ /* check if we have an entry in fstab that suits our needs */
+ if (!strcmp(device->dev, entry->mnt_fsname)
+ || (did && strstr(entry->mnt_fsname, "UUID=")
+ && strstr(entry->mnt_fsname, did))
+ || (strstr(entry->mnt_fsname, "LABEL=")
+ && strstr(entry->mnt_fsname, device->label))
+ || (!resolve_symlink(entry->mnt_fsname, rlink, 1024)
+ && !strcmp(rlink, device->dev))) {
             tmp = device->mountp;
- while ((i = getmntent(mtab))) {
- /* check if someone else uses our mount point */
- if (!strcmp(i->mnt_dir, device->mountp)) {
- device->mountp = malloc(len);
- if (!device->mountp) {
- device->mountp = tmp;
- } else {
- snprintf(device->mountp, len, "%s%s", BASE_MNT_DIR, str);
- }
- break;
- }
+ device->mountp = strdup(entry->mnt_dir);
+ device->opt = strdup(entry->mnt_opts);
+ if (!device->opt || !device->mountp) {
+ FREE_WRAP(device->mountp);
+ FREE_WRAP(device->opt);
+ device->mountp = tmp;
+ } else {
+ if (!(str = strrchr(device->dev, '/')))
+ break; /* this will cause mount points to be stacked */
+ ++str;
+ if (!(mtab = setmntent("/etc/mtab", "r")))
+ goto out;
+ device->use_fstab = 1;
+ len = strlen(str) + 1 + strlen(BASE_MNT_DIR);
+ tmp = device->mountp;
+ while ((i = getmntent(mtab))) {
+ /* check if someone else uses our mount point */
+ if (!strcmp(i->mnt_dir, device->mountp)) {
+ device->mountp = malloc(len);
+ if (!device->mountp) {
+ device->mountp = tmp;
+ } else {
+ snprintf(device->mountp, len, "%s%s", BASE_MNT_DIR, str);
+ }
+ break;
+ }
+ }
+ endmntent(mtab);
             }
- endmntent(mtab);
- }
- break;
- }
- }
+ break;
+ }
+ }
 out:
- endmntent(fp);
+ endmntent(fp);
 }
 
 static int
 do_mount(const struct device_t *device)
 {
- if (!device)
- return -1;
+ if (!device)
+ return -1;
 
- return EXEC_CMD(
- ((char *[]) {
- MOUNT_CMD_PATH,
- "-t",
- device->fstype,
- "-o",
- (!device->opt) ? DEFAULT_MNT_OPTIONS : device->opt,
- device->dev,
- device->mountp,
- (char *)NULL
- })) ? 0 : -1;
+ return EXEC_CMD(
+ ((char *[]) {
+ MOUNT_CMD_PATH,
+ "-t",
+ device->fstype,
+ "-o",
+ (!device->opt) ? DEFAULT_MNT_OPTIONS : device->opt,
+ device->dev,
+ device->mountp,
+ (char *)NULL
+ })) ? 0 : -1;
 }
 
 static int
 do_umount(const struct device_t *device)
 {
- if (!device)
- return -1;
+ if (!device)
+ return -1;
 
- return EXEC_CMD(
- ((char *[]) {
- UMOUNT_CMD_PATH, device->dev, (char *)NULL
- })) ? 0 : -1;
+ return EXEC_CMD(
+ ((char *[]) {
+ UMOUNT_CMD_PATH, device->dev, (char *)NULL
+ })) ? 0 : -1;
 }
 
 static int
 add_fstab_entry(const struct device_t *device)
 {
- FILE *fstab;
- struct mntent entry;
+ FILE *fstab;
+ struct mntent entry;
 
- if (!device || device->use_fstab)
- return -1;
- fstab = setmntent("/etc/fstab", "a");
- if (!fstab)
- return -1;
+ if (!device || device->use_fstab)
+ return -1;
+ fstab = setmntent("/etc/fstab", "a");
+ if (!fstab)
+ return -1;
 
- entry.mnt_fsname = device->dev;
- entry.mnt_dir = device->mountp;
- entry.mnt_type = device->fstype;
- entry.mnt_opts = (!device->opt) ? DEFAULT_MNT_OPTIONS : device->opt;
- entry.mnt_freq = 0;
- entry.mnt_passno = 0;
+ entry.mnt_fsname = device->dev;
+ entry.mnt_dir = device->mountp;
+ entry.mnt_type = device->fstype;
+ entry.mnt_opts = (!device->opt) ? DEFAULT_MNT_OPTIONS : device->opt;
+ entry.mnt_freq = 0;
+ entry.mnt_passno = 0;
 
- addmntent(fstab, &entry);
- endmntent(fstab);
- return 0;
+ addmntent(fstab, &entry);
+ endmntent(fstab);
+ return 0;
 }
 
 static int
 remove_fstab_entry(const struct device_t *device)
 {
- FILE *fstab = NULL, *line_pp = NULL;
- char buf[BUFSIZ];
- int lines = 0, i = 0;
+ FILE *fstab = NULL, *line_pp = NULL;
+ char buf[BUFSIZ];
+ int lines = 0, i = 0;
 
- if (!device || device->use_fstab)
- return -1;
+ if (!device || device->use_fstab)
+ return -1;
 
- line_pp = popen("wc -l /etc/fstab | awk '{print $1}'", "r");
- if (!line_pp)
- return -1;
+ line_pp = popen("wc -l /etc/fstab | awk '{print $1}'", "r");
+ if (!line_pp)
+ return -1;
 
- if (!fgets(buf, sizeof buf, line_pp)) {
- pclose(line_pp);
- return -1;
- }
+ if (!fgets(buf, sizeof buf, line_pp)) {
+ pclose(line_pp);
+ return -1;
+ }
 
- pclose(line_pp);
- lines = atoi(buf);
+ pclose(line_pp);
+ lines = atoi(buf);
 
- fstab = fopen("/etc/fstab", "r+");
- if (!fstab)
- return -1;
+ fstab = fopen("/etc/fstab", "r+");
+ if (!fstab)
+ return -1;
 
- char tmp[lines][BUFSIZ];
- while (!feof(fstab) && fgets(tmp[i], BUFSIZ, fstab))
- ++i;
+ char tmp[lines][BUFSIZ];
+ while (!feof(fstab) && fgets(tmp[i], BUFSIZ, fstab))
+ ++i;
 
- if (ferror(fstab))
- goto fail;
+ if (ferror(fstab))
+ goto fail;
 
- rewind(fstab);
- if (ftruncate(fileno(fstab), 0) < 0) {
- syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__,
- strerror(errno));
- goto fail;
- }
+ rewind(fstab);
+ if (ftruncate(fileno(fstab), 0) < 0) {
+ syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__,
+ strerror(errno));
+ goto fail;
+ }
 
- i = 0;
- while (i < lines) {
- if (!strstr(tmp[i], device->dev))
- fprintf(fstab, "%s", tmp[i]);
- ++i;
- }
+ i = 0;
+ while (i < lines) {
+ if (!strstr(tmp[i], device->dev))
+ fprintf(fstab, "%s", tmp[i]);
+ ++i;
+ }
 
- fclose(fstab);
- return 0;
+ fclose(fstab);
+ return 0;
 
 fail:
- if (fstab)
- fclose(fstab);
- return -1;
+ if (fstab)
+ fclose(fstab);
+ return -1;
 }
 
 static inline int
 file_exists(const char *s)
 {
- struct stat sb;
- return ((stat(s, &sb) < 0 && errno == ENOENT) ? -ENOENT : 0);
+ struct stat sb;
+ return ((stat(s, &sb) < 0 && errno == ENOENT) ? -ENOENT : 0);
 }
 
 static void
 cleanup(int sig __attribute__ ((unused)))
 {
- g_main_loop_quit(loop);
+ g_main_loop_quit(loop);
 }
 
 /*
@@ -776,47 +776,47 @@
 static int
 is_running(void)
 {
- char buf[16];
- int serrno;
+ char buf[16];
+ int serrno;
 
- pid_fd = open(LOCKFILE, O_RDWR | O_CREAT, LOCKMODE);
- if (pid_fd < 0 || flock(pid_fd, LOCK_EX | LOCK_NB) < 0
- || ftruncate(pid_fd, 0) < 0)
- goto out;
+ pid_fd = open(LOCKFILE, O_RDWR | O_CREAT, LOCKMODE);
+ if (pid_fd < 0 || flock(pid_fd, LOCK_EX | LOCK_NB) < 0
+ || ftruncate(pid_fd, 0) < 0)
+ goto out;
 
- snprintf(buf, sizeof(buf), "%jd\n", (intmax_t)getpid());
- write(pid_fd, buf, strlen(buf));
- return 0;
+ snprintf(buf, sizeof(buf), "%jd\n", (intmax_t)getpid());
+ write(pid_fd, buf, strlen(buf));
+ return 0;
 
 out:
- serrno = errno;
- if (pid_fd != -1)
- if (close(pid_fd) < 0)
- syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__,
- strerror(errno));
- errno = serrno;
- pid_fd = -1;
- syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__,
- strerror(errno));
- return -1;
+ serrno = errno;
+ if (pid_fd != -1)
+ if (close(pid_fd) < 0)
+ syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__,
+ strerror(errno));
+ errno = serrno;
+ pid_fd = -1;
+ syslog(LOG_ERR, "%s:%d: %s", __FILE__, __LINE__,
+ strerror(errno));
+ return -1;
 }
 
 static void
 debug_dump_device(const struct device_t *device)
 {
- if (!device)
- return;
+ if (!device)
+ return;
 
- printf("Volume info: %s\n", (!device->did) ? "(null)" : device->did);
- printf("Device: %s\n", (!device->dev) ? "(null)" : device->dev);
- printf("Label: %s\n", (!device->label) ? "(null)" : device->label);
- printf("Filesystem type: %s\n",
- (!device->fstype) ? "(null)" : device->fstype);
- printf("Mount options: %s\n",
- (!device->opt) ? DEFAULT_MNT_OPTIONS : device->opt);
- printf("Mount point: %s\n",
- (!device->mountp) ? "(null)" : device->mountp);
- printf("Uses /etc/fstab: %d\n", device->use_fstab);
- printf("Cleanup after /etc/fstab: %d\n", device->should_remove_entry);
+ printf("Volume info: %s\n", (!device->did) ? "(null)" : device->did);
+ printf("Device: %s\n", (!device->dev) ? "(null)" : device->dev);
+ printf("Label: %s\n", (!device->label) ? "(null)" : device->label);
+ printf("Filesystem type: %s\n",
+ (!device->fstype) ? "(null)" : device->fstype);
+ printf("Mount options: %s\n",
+ (!device->opt) ? DEFAULT_MNT_OPTIONS : device->opt);
+ printf("Mount point: %s\n",
+ (!device->mountp) ? "(null)" : device->mountp);
+ printf("Uses /etc/fstab: %d\n", device->use_fstab);
+ printf("Cleanup after /etc/fstab: %d\n", device->should_remove_entry);
 }
 
Received on Sun Oct 04 2009 - 11:51:18 UTC

This archive was generated by hypermail 2.2.0 : Sun Oct 04 2009 - 12:00:05 UTC