Skip to content

Commit

Permalink
Clean up error handling
Browse files Browse the repository at this point in the history
Eliminate exit_error() helper, that's what error() is for.
  • Loading branch information
martinpitt committed Nov 24, 2022
1 parent 0f686e1 commit c94147f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 46 deletions.
26 changes: 13 additions & 13 deletions src/umockdev-record.vala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ resolve (string dev)
{
Posix.Stat st;
if (Posix.stat(dev, out st) != 0)
exit_error("Cannot access device %s: %s", dev, strerror(errno));
error("Cannot access device %s: %m", dev);

uint maj = Posix.major(st.st_rdev);
uint min = Posix.minor(st.st_rdev);
Expand All @@ -88,7 +88,7 @@ resolve (string dev)
real = link;

if (!FileUtils.test(Path.build_filename(real, "uevent"), FileTest.EXISTS))
exit_error("Invalid device %s, has no uevent attribute", real);
error("Invalid device %s, has no uevent attribute", real);

return real;
}
Expand Down Expand Up @@ -167,7 +167,7 @@ print_device_attributes(string devpath, string subdir)
d = Dir.open(attr_dir);
} catch (Error e) {
if (subdir == "") {
exit_error("Cannot open directory %s: %s", attr_dir, e.message);
error("Cannot open directory %s: %s", attr_dir, e.message);
} else {
// we ignore this on subdirs, some might be transient or
// inaccessible
Expand Down Expand Up @@ -196,7 +196,7 @@ print_device_attributes(string devpath, string subdir)
try {
stdout.printf("L: %s=%s\n", attr_name, FileUtils.read_link(attr_path));
} catch (Error e) {
exit_error("Cannot read link %s: %s", attr_path, e.message);
error("Cannot read link %s: %s", attr_path, e.message);
}
} else if (FileUtils.test(attr_path, FileTest.IS_REGULAR)) {
uint8[] contents;
Expand Down Expand Up @@ -230,7 +230,7 @@ record_device(string dev)
if (exitcode != 0)
throw new SpawnError.FAILED("udevadm exited with code %i\n%s".printf(exitcode, u_err));
} catch (Error e) {
exit_error("Cannot call udevadm: %s", e.message);
error("Cannot call udevadm: %s", e.message);
}

var properties = new List<string>();
Expand Down Expand Up @@ -297,14 +297,14 @@ split_devfile_arg(string arg, out string dev, out string devnum, out bool is_blo
{
string[] parts = arg.split ("=", 2); // devname, ioctlfilename
if (parts.length != 2)
exit_error("--ioctl argument must be devname=filename");
error("--ioctl argument must be devname=filename");
dev = parts[0];
fname = parts[1];

// build device major/minor
Posix.Stat st;
if (Posix.stat(dev, out st) != 0)
exit_error("Cannot access device %s: %s", dev, strerror(errno));
error("Cannot access device %s: %m", dev);

is_block = Posix.S_ISBLK(st.st_mode);
if (Posix.S_ISCHR(st.st_mode) || Posix.S_ISBLK(st.st_mode)) {
Expand All @@ -318,7 +318,7 @@ split_devfile_arg(string arg, out string dev, out string devnum, out bool is_blo
try {
FileUtils.get_contents(Path.build_filename(dev, "dev"), out devnum);
} catch (Error e) {
exit_error("Cannot open %s/dev: %s", dev, e.message);
error("Cannot open %s/dev: %s", dev, e.message);
}
}
}
Expand Down Expand Up @@ -408,7 +408,7 @@ main (string[] args)
try {
oc.parse (ref args);
} catch (Error e) {
exit_error("Error: %s\nRun %s --help for how to use this program", e.message, args[0]);
error("Error: %s\nRun %s --help for how to use this program", e.message, args[0]);
}

if (opt_version) {
Expand All @@ -417,12 +417,12 @@ main (string[] args)
}

if (opt_all && opt_devices.length > 0)
exit_error("Specifying a device list together with --all is invalid.");
error("Specifying a device list together with --all is invalid.");
if (!opt_all && opt_devices.length == 0)
exit_error("Need to specify at least one device or --all.");
error("Need to specify at least one device or --all.");
if ((opt_ioctl != null || opt_script.length > 0 || opt_evemu_events.length > 0) &&
(opt_all || opt_devices.length < 1))
exit_error("For recording ioctls or scripts you have to specify a command to run");
error("For recording ioctls or scripts you have to specify a command to run");

// device dump mode
if (opt_ioctl == null && opt_script.length == 0 && opt_evemu_events.length == 0) {
Expand Down Expand Up @@ -471,7 +471,7 @@ main (string[] args)
child_pid = spawn_process_under_test (opt_devices, child_watch_cb);
} catch (Error e) {
remove_dir (root_dir);
exit_error ("Cannot run %s: %s", opt_devices[0], e.message);
error ("Cannot run %s: %s", opt_devices[0], e.message);
}

loop.run();
Expand Down
2 changes: 1 addition & 1 deletion src/umockdev-run.vala
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ main (string[] args)
try {
child_pid = spawn_process_under_test (opt_program, child_watch_cb);
} catch (Error e) {
exit_error("Cannot run %s: %s", opt_program[0], e.message);
error("Cannot run %s: %s", opt_program[0], e.message);
}

loop.run();
Expand Down
14 changes: 3 additions & 11 deletions src/umockdev-utils.vala
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@

namespace UMockdevUtils {

public void
exit_error(string message, ...)
{
stderr.vprintf(message, va_list());
stderr.puts("\n");
Process.exit(1);
}

public void
checked_setenv(string variable, string value)
{
if (!Environment.set_variable(variable, value, true))
exit_error("Failed to set env variable %s", variable);
error("Failed to set env variable %s", variable);
}

// only use this in tests, not in runtime code!
Expand Down Expand Up @@ -57,8 +49,8 @@ pud_sig_handler (int sig)

debug ("umockdev: caught signal %i, propagating to child\n", sig);
if (Posix.kill (process_under_test, sig) != 0)
stderr.printf ("umockdev: unable to propagate signal %i to child %i: %s\n",
sig, process_under_test, strerror (errno));
stderr.printf ("umockdev: unable to propagate signal %i to child %i: %m\n",
sig, process_under_test);
}

static void
Expand Down
35 changes: 16 additions & 19 deletions src/umockdev.vala
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public class Testbed: GLib.Object {
if ("/" in name) {
string d = Path.get_dirname(attr_path);
if (DirUtils.create_with_parents(d, 0755) != 0)
error("cannot create attribute subdir '%s': %s", d, strerror(errno));
error("cannot create attribute subdir '%s': %m", d);
}

try {
Expand Down Expand Up @@ -257,9 +257,9 @@ public class Testbed: GLib.Object {
var path = Path.build_filename(this.root_dir, devpath, name);
var dir = Path.get_dirname(path);
if (DirUtils.create_with_parents(dir, 0755) != 0)
error("cannot create attribute dir '%s': %s", dir, strerror(errno));
error("cannot create attribute dir '%s': %m", dir);
if (FileUtils.symlink(value, path) < 0) {
error("Cannot create symlink %s: %s", path, strerror(errno));
error("Cannot create symlink %s: %m", path);
}
}

Expand Down Expand Up @@ -424,12 +424,12 @@ public class Testbed: GLib.Object {

/* create device and corresponding subsystem dir */
if (DirUtils.create_with_parents(dev_dir, 0755) != 0)
error("cannot create dev dir '%s': %s", dev_dir, strerror(errno));
error("cannot create dev dir '%s': %m", dev_dir);
if (!subsystem_is_bus(subsystem)) {
/* class/ symlinks */
var class_dir = Path.build_filename(this.sys_dir, "class", subsystem);
if (DirUtils.create_with_parents(class_dir, 0755) != 0)
error("cannot create class dir '%s': %s", class_dir, strerror(errno));
error("cannot create class dir '%s': %m", class_dir);

/* subsystem symlink */
assert(FileUtils.symlink(Path.build_filename(make_dotdots(dev_path), "class", subsystem),
Expand All @@ -456,7 +456,7 @@ public class Testbed: GLib.Object {
if (subsystem == "block") {
var block_dir = Path.build_filename(this.sys_dir, "block");
if (DirUtils.create_with_parents(block_dir, 0755) != 0)
error("cannot create block dir '%s': %s", block_dir, strerror(errno));
error("cannot create block dir '%s': %m", block_dir);
assert (FileUtils.symlink(Path.build_filename("..", dev_path_no_sys),
Path.build_filename(block_dir, Path.get_basename(name))) == 0);
}
Expand Down Expand Up @@ -489,12 +489,12 @@ public class Testbed: GLib.Object {
string sysdev_dir = Path.build_filename(this.sys_dir, "dev",
(dev_path.contains("/block/") ? "block" : "char"));
if (DirUtils.create_with_parents(sysdev_dir, 0755) != 0)
error("cannot create dir '%s': %s", sysdev_dir, strerror(errno));
error("cannot create dir '%s': %m", sysdev_dir);
string dest = Path.build_filename(sysdev_dir, val);
if (!FileUtils.test(dest, FileTest.EXISTS)) {
if (FileUtils.symlink("../../" + dev_path.substring(5), dest) < 0)
error("add_device %s: failed to symlink %s to %s: %s", name, dest,
dev_path.substring(5), strerror(errno));
error("add_device %s: failed to symlink %s to %s: %m", name, dest,
dev_path.substring(5));
}
}
}
Expand Down Expand Up @@ -1019,13 +1019,11 @@ public class Testbed: GLib.Object {
{
int fd = Posix.socket (Posix.AF_UNIX, type, 0);
if (fd < 0)
throw new FileError.INVAL ("Cannot create socket type %i: %s".printf(
type, strerror(errno)));
throw new FileError.INVAL ("Cannot create socket type %i: %m".printf(type));

string real_path = Path.build_filename (this.root_dir, path);
if (DirUtils.create_with_parents(Path.get_dirname(real_path), 0755) != 0)
throw new FileError.INVAL ("Cannot create socket path: %s".printf(
strerror(errno)));
throw new FileError.INVAL ("Cannot create socket path: %m".printf());

// start thread to accept client connections at first socket creation
if (this.socket_server == null)
Expand Down Expand Up @@ -1422,7 +1420,7 @@ public class Testbed: GLib.Object {
int ptym, ptys;
char[] ptyname_array = new char[8192];
if (Linux.openpty (out ptym, out ptys, ptyname_array, null, null) < 0)
error ("umockdev Testbed.create_node_for_device: openpty() failed: %s", strerror (errno));
error ("umockdev Testbed.create_node_for_device: openpty() failed: %m");
string ptyname = (string) ptyname_array;
debug ("create_node_for_device: creating pty device %s: got pty %s", node_path, ptyname);
Posix.close (ptys);
Expand Down Expand Up @@ -1709,7 +1707,7 @@ private class ScriptRunner {
debug ("ScriptRunner[%s]: read op after sleep; writing data '%s'", this.device, encode(data));
ssize_t l = Posix.write (this.fd, data, data.length);
if (l < 0)
error ("ScriptRunner[%s]: write failed: %s", this.device, strerror (errno));
error ("ScriptRunner[%s]: write failed: %m", this.device);
assert (l == data.length);
break;

Expand Down Expand Up @@ -1795,8 +1793,7 @@ private class ScriptRunner {
if (res < 0) {
if (errno == Posix.EINTR)
continue;
error ("ScriptRunner op_write[%s]: select() failed: %s",
this.device, strerror (errno));
error ("ScriptRunner op_write[%s]: select() failed: %m", this.device);
}

if (res == 0) {
Expand Down Expand Up @@ -1986,7 +1983,7 @@ private class SocketServer {
if (res < 0) {
if (errno == Posix.EINTR)
continue;
error ("socket server thread: select() failed: %s", strerror (errno));
error ("socket server thread: select() failed: %m");
}
if (res == 0)
continue; // timeout
Expand All @@ -2013,7 +2010,7 @@ private class SocketServer {
if (Posix.FD_ISSET (s.fd, fds) > 0) {
int fd = Posix.accept (s.fd, null, null);
if (fd < 0)
error ("socket server thread: accept() failed: %s", strerror (errno));
error ("socket server thread: accept() failed: %m");
string sock_path = null;
try {
sock_path = ((UnixSocketAddress) s.get_local_address()).path;
Expand Down
2 changes: 1 addition & 1 deletion tests/test-umockdev-record.vala
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ t_system_invalid ()
}

spawn ("umockdev-record" + " /sys/class", out sout, out serr, out exit);
assert_cmpstr (serr, CompareOperator.EQ, "Invalid device /sys/class, has no uevent attribute\n");
assert (serr.contains("Invalid device /sys/class, has no uevent attribute\n"));
assert_cmpstr (sout, CompareOperator.EQ, "");
assert_cmpint (exit, CompareOperator.NE, 0);

Expand Down
1 change: 0 additions & 1 deletion tests/test-umockdev-run.vala
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ check_program_error (string program, string run_command, string expected_err)
assert_in (expected_err, serr);

assert_cmpint (exit, CompareOperator.NE, 0);
assert (Process.if_exited (exit));
assert_cmpstr (sout, CompareOperator.EQ, "");
}

Expand Down

0 comments on commit c94147f

Please sign in to comment.