mirror of
https://github.com/libguestfs/libguestfs.git
synced 2026-03-21 22:53:37 +00:00
Use 'error' function consistently throughout.
Wherever we had code which did:
if (something_bad) {
perror (...);
exit (EXIT_FAILURE);
}
replace this with use of the error(3) function:
if (something_bad)
error (EXIT_FAILURE, errno, ...);
The error(3) function is supplied by glibc, or by gnulib on platforms
which don't have it, and is much more flexible than perror(3). Since
we already use error(3), there seems to be no downside to mandating it
everywhere.
Note there is one nasty catch with error(3): error (EXIT_SUCCESS, ...)
does *not* exit! This is also the reason why error(3) cannot be
marked as __attribute__((noreturn)).
Because the examples can't use gnulib, I did not change them.
To search for multiline patterns of the above form, pcregrep -M turns
out to be very useful:
pcregrep --buffer-size 10M -M '\bperror\b.*\n.*\bexit\b' `git ls-files`
This commit is contained in:
@@ -28,6 +28,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <error.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
@@ -60,17 +61,13 @@ main (int argc, char *argv[])
|
||||
}
|
||||
|
||||
/* Create the pipe. */
|
||||
if (pipe (pipefd) == -1) {
|
||||
perror ("pipe");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (pipe (pipefd) == -1)
|
||||
error (EXIT_FAILURE, errno, "pipe");
|
||||
|
||||
/* Create the guestunmount subprocess. */
|
||||
pid = fork ();
|
||||
if (pid == -1) {
|
||||
perror ("fork");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (pid == -1)
|
||||
error (EXIT_FAILURE, errno, "fork");
|
||||
|
||||
if (pid == 0) { /* child - guestunmount */
|
||||
char fd_str[64];
|
||||
@@ -92,10 +89,8 @@ main (int argc, char *argv[])
|
||||
sleep (2);
|
||||
|
||||
r = waitpid (pid, &status, WNOHANG);
|
||||
if (r == -1) {
|
||||
perror ("waitpid");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (r == -1)
|
||||
error (EXIT_FAILURE, errno, "waitpid");
|
||||
if (r != 0) {
|
||||
char status_string[80];
|
||||
|
||||
@@ -113,10 +108,8 @@ main (int argc, char *argv[])
|
||||
close (pipefd[1]);
|
||||
|
||||
r = waitpid (pid, &status, 0);
|
||||
if (r == -1) {
|
||||
perror ("waitpid");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
if (r == -1)
|
||||
error (EXIT_FAILURE, errno, "waitpid");
|
||||
if (!WIFEXITED (status) || WEXITSTATUS (status) != 3) {
|
||||
char status_string[80];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user