diff --git a/align/scan.c b/align/scan.c index 118529311..7b71cd21e 100644 --- a/align/scan.c +++ b/align/scan.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -130,10 +131,8 @@ main (int argc, char *argv[]) int r; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -149,12 +148,10 @@ main (int argc, char *argv[]) OPTION_format; } else if (STREQ (long_options[option_index].name, "uuid")) { uuid = 1; - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, - long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': @@ -170,11 +167,8 @@ main (int argc, char *argv[]) break; case 'P': - if (sscanf (optarg, "%zu", &max_threads) != 1) { - fprintf (stderr, _("%s: -P option is not numeric\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (sscanf (optarg, "%zu", &max_threads) != 1) + error (EXIT_FAILURE, 0, _("-P option is not numeric")); break; case 'q': @@ -229,16 +223,11 @@ main (int argc, char *argv[]) if (r == -1) exit (EXIT_FAILURE); #else - fprintf (stderr, _("%s: compiled without support for libvirt.\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, _("compiled without support for libvirt")); #endif } else { /* Single guest. */ - if (uuid) { - fprintf (stderr, _("%s: --uuid option cannot be used with -a or -d\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (uuid) + error (EXIT_FAILURE, 0, _("--uuid option cannot be used with -a or -d")); /* Add domains/drives from the command line (for a single guest). */ add_drives (drvs, 'a'); diff --git a/builder/index-validate.c b/builder/index-validate.c index 22e2ccd74..6a87d7ac1 100644 --- a/builder/index-validate.c +++ b/builder/index-validate.c @@ -82,11 +82,10 @@ main (int argc, char *argv[]) compat_1_24_0 = compat_1_24_1 = 1; else if (STREQ (long_options[option_index].name, "compat-1.24.1")) compat_1_24_1 = 1; - else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'V': @@ -121,16 +120,15 @@ main (int argc, char *argv[]) if (ret != 0) { parse_context_free (&context); - fprintf (stderr, _("%s: '%s' could not be validated, see errors above\n"), - guestfs_int_program_name, input); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + _("'%s' could not be validated, see errors above"), input); } if (compat_1_24_1 && context.seen_comments) { parse_context_free (&context); - fprintf (stderr, _("%s: %s contains comments which will not work with virt-builder 1.24.1\n"), - guestfs_int_program_name, input); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + _("%s contains comments which will not work with virt-builder 1.24.1"), + input); } /* Iterate over the parsed sections, semantically validating it. */ @@ -141,9 +139,9 @@ main (int argc, char *argv[]) if (compat_1_24_0) { if (strchr (sections->name, '_')) { parse_context_free (&context); - fprintf (stderr, _("%s: %s: section [%s] has invalid characters which will not work with virt-builder 1.24.0\n"), - guestfs_int_program_name, input, sections->name); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + _("%s: section [%s] has invalid characters which will not work with virt-builder 1.24.0"), + input, sections->name); } } @@ -152,18 +150,18 @@ main (int argc, char *argv[]) if (strchr (fields->key, '[') || strchr (fields->key, ']')) { parse_context_free (&context); - fprintf (stderr, _("%s: %s: section [%s], field '%s' has invalid characters which will not work with virt-builder 1.24.0\n"), - guestfs_int_program_name, input, sections->name, fields->key); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + _("%s: section [%s], field '%s' has invalid characters which will not work with virt-builder 1.24.0"), + input, sections->name, fields->key); } } if (compat_1_24_1) { if (strchr (fields->key, '.') || strchr (fields->key, ',')) { parse_context_free (&context); - fprintf (stderr, _("%s: %s: section [%s], field '%s' has invalid characters which will not work with virt-builder 1.24.1\n"), - guestfs_int_program_name, input, sections->name, fields->key); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + _("%s: section [%s], field '%s' has invalid characters which will not work with virt-builder 1.24.1"), + input, sections->name, fields->key); } } if (STREQ (fields->key, "sig")) @@ -172,9 +170,9 @@ main (int argc, char *argv[]) if (compat_1_24_0 && !seen_sig) { parse_context_free (&context); - fprintf (stderr, _("%s: %s: section [%s] is missing a 'sig' field which will not work with virt-builder 1.24.0\n"), - guestfs_int_program_name, input, sections->name); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + _("%s: section [%s] is missing a 'sig' field which will not work with virt-builder 1.24.0"), + input, sections->name); } } diff --git a/cat/cat.c b/cat/cat.c index c90fc06a0..0370fbde0 100644 --- a/cat/cat.c +++ b/cat/cat.c @@ -116,10 +116,8 @@ main (int argc, char *argv[]) int option_index; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -137,12 +135,10 @@ main (int argc, char *argv[]) echo_keys = 1; } else if (STREQ (long_options[option_index].name, "format")) { OPTION_format; - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, - long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': diff --git a/cat/filesystems.c b/cat/filesystems.c index 6f083d313..3f9d931fd 100644 --- a/cat/filesystems.c +++ b/cat/filesystems.c @@ -185,10 +185,8 @@ main (int argc, char *argv[]) int title; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -238,12 +236,10 @@ main (int argc, char *argv[]) STREQ (long_options[option_index].name, "volgroups") || STREQ (long_options[option_index].name, "volume-groups")) { output |= OUTPUT_VGS; - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, - long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': @@ -303,11 +299,9 @@ main (int argc, char *argv[]) /* -h and --csv doesn't make sense. Spreadsheets will corrupt these * fields. (RHBZ#600977). */ - if (human && csv) { - fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (human && csv) + error (EXIT_FAILURE, 0, + _("you cannot use -h and --csv options together.")); /* Nothing selected for output, means --filesystems is implied. */ if (output == 0) diff --git a/cat/log.c b/cat/log.c index 79761a0c8..daefda732 100644 --- a/cat/log.c +++ b/cat/log.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -117,10 +118,8 @@ main (int argc, char *argv[]) int option_index; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -138,12 +137,10 @@ main (int argc, char *argv[]) echo_keys = 1; } else if (STREQ (long_options[option_index].name, "format")) { OPTION_format; - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, - long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': diff --git a/cat/ls.c b/cat/ls.c index f92bda140..91f2125ac 100644 --- a/cat/ls.c +++ b/cat/ls.c @@ -178,10 +178,8 @@ main (int argc, char *argv[]) int mode = 0; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -227,11 +225,10 @@ main (int argc, char *argv[]) } else if (STREQ (long_options[option_index].name, "uid") || STREQ (long_options[option_index].name, "uids")) { enable_uids = 1; - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': @@ -327,20 +324,16 @@ main (int argc, char *argv[]) /* Many flags only apply to -lR mode. */ if (mode != MODE_LS_LR && (csv || human || enable_uids || enable_times || enable_extra_stats || - checksum)) { - fprintf (stderr, _("%s: used a flag which can only be combined with -lR mode\nFor more information, read the virt-ls(1) man page.\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + checksum)) + error (EXIT_FAILURE, 0, + _("used a flag which can only be combined with -lR mode\nFor more information, read the virt-ls(1) man page.")); /* CSV && human is unsafe because spreadsheets fail to parse these * fields correctly. (RHBZ#600977). */ - if (human && csv) { - fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (human && csv) + error (EXIT_FAILURE, 0, + _("you cannot use -h and --csv options together.")); /* User must specify at least one directory name on the command line. */ if (optind >= argc || argc - optind < 1) diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c index 00b4a0a81..b76b5a48a 100644 --- a/daemon/guestfsd.c +++ b/daemon/guestfsd.c @@ -221,9 +221,8 @@ main (int argc, char *argv[]) exit (EXIT_SUCCESS); default: - fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", - (unsigned) c); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + "unexpected command line option 0x%x\n", (unsigned) c); } } diff --git a/daemon/lvm-filter.c b/daemon/lvm-filter.c index 12c4ca76b..eadc4727c 100644 --- a/daemon/lvm-filter.c +++ b/daemon/lvm-filter.c @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include @@ -72,10 +74,8 @@ copy_lvm (void) return; } - if (mkdtemp (lvm_system_dir) == NULL) { - fprintf (stderr, "mkdtemp: %s: %m\n", lvm_system_dir); - exit (EXIT_FAILURE); - } + if (mkdtemp (lvm_system_dir) == NULL) + error (EXIT_FAILURE, errno, "mkdtemp: %s", lvm_system_dir); /* Copy the entire directory */ snprintf (cmd, sizeof cmd, "%s -a /etc/lvm/ %s", str_cp, lvm_system_dir); diff --git a/daemon/mount.c b/daemon/mount.c index 5dd7da282..ef51d487f 100644 --- a/daemon/mount.c +++ b/daemon/mount.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include #include #include @@ -49,10 +51,8 @@ is_root_mounted (void) * that requires custom parsing code. */ fp = setmntent ("/proc/mounts", "r"); - if (fp == NULL) { - fprintf (stderr, "setmntent: %s: %m\n", "/proc/mounts"); - exit (EXIT_FAILURE); - } + if (fp == NULL) + error (EXIT_FAILURE, errno, "setmntent: %s", "/proc/mounts"); while ((m = getmntent (fp)) != NULL) { /* Allow a mount directory like "/sysroot". */ @@ -91,10 +91,8 @@ is_device_mounted (const char *device) * that requires custom parsing code. */ fp = setmntent ("/proc/mounts", "r"); - if (fp == NULL) { - fprintf (stderr, "setmntent: %s: %m\n", "/proc/mounts"); - exit (EXIT_FAILURE); - } + if (fp == NULL) + error (EXIT_FAILURE, errno, "setmntent: %s", "/proc/mounts"); while ((m = getmntent (fp)) != NULL) { if ((sysroot_len > 0 && STREQ (m->mnt_dir, sysroot)) || @@ -274,10 +272,8 @@ mounts_or_mountpoints (int mp) * that requires custom parsing code. */ fp = setmntent ("/proc/mounts", "r"); - if (fp == NULL) { - fprintf (stderr, "setmntent: %s: %m\n", "/proc/mounts"); - exit (EXIT_FAILURE); - } + if (fp == NULL) + error (EXIT_FAILURE, errno, "setmntent: %s", "/proc/mounts"); while ((m = getmntent (fp)) != NULL) { /* Allow a mount directory like "/sysroot". */ @@ -383,10 +379,8 @@ do_umount_all (void) * that requires custom parsing code. */ fp = setmntent ("/proc/mounts", "r"); - if (fp == NULL) { - fprintf (stderr, "setmntent: %s: %m\n", "/proc/mounts"); - exit (EXIT_FAILURE); - } + if (fp == NULL) + error (EXIT_FAILURE, errno, "setmntent: %s", "/proc/mounts"); while ((m = getmntent (fp)) != NULL) { if (verbose) { diff --git a/daemon/proto.c b/daemon/proto.c index c3972f144..654dd3a0d 100644 --- a/daemon/proto.c +++ b/daemon/proto.c @@ -107,11 +107,8 @@ main_loop (int _sock) if (len == GUESTFS_CANCEL_FLAG) continue; - if (len > GUESTFS_MESSAGE_MAX) { - fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n", - len); - exit (EXIT_FAILURE); - } + if (len > GUESTFS_MESSAGE_MAX) + error (EXIT_FAILURE, 0, "incoming message is too long (%u bytes)", len); buf = malloc (len); if (!buf) { @@ -151,10 +148,8 @@ main_loop (int _sock) /* Decode the message header. */ xdrmem_create (&xdr, buf, len, XDR_DECODE); - if (!xdr_guestfs_message_header (&xdr, &hdr)) { - fprintf (stderr, "guestfsd: could not decode message header\n"); - exit (EXIT_FAILURE); - } + if (!xdr_guestfs_message_header (&xdr, &hdr)) + error (EXIT_FAILURE, 0, "could not decode message header"); /* Check the version etc. */ if (hdr.prog != GUESTFS_PROGRAM) { @@ -296,10 +291,8 @@ send_error (int errnum, char *msg) hdr.proc = proc_nr; hdr.serial = serial; - if (!xdr_guestfs_message_header (&xdr, &hdr)) { - fprintf (stderr, "guestfsd: failed to encode error message header\n"); - exit (EXIT_FAILURE); - } + if (!xdr_guestfs_message_header (&xdr, &hdr)) + error (EXIT_FAILURE, 0, "failed to encode error message header"); /* These strings are not going to be freed. We just cast them * to (char *) because they are defined that way in the XDR structs. @@ -308,10 +301,8 @@ send_error (int errnum, char *msg) (char *) (errnum > 0 ? guestfs_int_errno_to_string (errnum) : ""); err.error_message = (char *) msg; - if (!xdr_guestfs_message_error (&xdr, &err)) { - fprintf (stderr, "guestfsd: failed to encode error message body\n"); - exit (EXIT_FAILURE); - } + if (!xdr_guestfs_message_error (&xdr, &err)) + error (EXIT_FAILURE, 0, "failed to encode error message body"); len = xdr_getpos (&xdr); xdr_destroy (&xdr); @@ -320,14 +311,10 @@ send_error (int errnum, char *msg) xdr_u_int (&xdr, &len); xdr_destroy (&xdr); - if (xwrite (sock, lenbuf, 4) == -1) { - fprintf (stderr, "guestfsd: xwrite failed\n"); - exit (EXIT_FAILURE); - } - if (xwrite (sock, buf, len) == -1) { - fprintf (stderr, "guestfsd: xwrite failed\n"); - exit (EXIT_FAILURE); - } + if (xwrite (sock, lenbuf, 4) == -1) + error (EXIT_FAILURE, 0, "xwrite failed"); + if (xwrite (sock, buf, len) == -1) + error (EXIT_FAILURE, 0, "xwrite failed"); } void @@ -352,10 +339,8 @@ reply (xdrproc_t xdrp, char *ret) hdr.proc = proc_nr; hdr.serial = serial; - if (!xdr_guestfs_message_header (&xdr, &hdr)) { - fprintf (stderr, "guestfsd: failed to encode reply header\n"); - exit (EXIT_FAILURE); - } + if (!xdr_guestfs_message_header (&xdr, &hdr)) + error (EXIT_FAILURE, 0, "failed to encode reply header"); if (xdrp) { /* This can fail if the reply body is too large, for example @@ -376,14 +361,10 @@ reply (xdrproc_t xdrp, char *ret) xdr_u_int (&xdr, &len); xdr_destroy (&xdr); - if (xwrite (sock, lenbuf, 4) == -1) { - fprintf (stderr, "guestfsd: xwrite failed\n"); - exit (EXIT_FAILURE); - } - if (xwrite (sock, buf, (size_t) len) == -1) { - fprintf (stderr, "guestfsd: xwrite failed\n"); - exit (EXIT_FAILURE); - } + if (xwrite (sock, lenbuf, 4) == -1) + error (EXIT_FAILURE, 0, "xwrite failed"); + if (xwrite (sock, buf, (size_t) len) == -1) + error (EXIT_FAILURE, 0, "xwrite failed"); } /* Receive file chunks, repeatedly calling 'cb'. */ @@ -413,11 +394,8 @@ receive_file (receive_cb cb, void *opaque) if (len == GUESTFS_CANCEL_FLAG) continue; /* Just ignore it. */ - if (len > GUESTFS_MESSAGE_MAX) { - fprintf (stderr, "guestfsd: incoming message is too long (%u bytes)\n", - len); - exit (EXIT_FAILURE); - } + if (len > GUESTFS_MESSAGE_MAX) + error (EXIT_FAILURE, 0, "incoming message is too long (%u bytes)", len); buf = malloc (len); if (!buf) { @@ -617,10 +595,8 @@ send_chunk (const guestfs_chunk *chunk) int err = (xwrite (sock, lenbuf, 4) == 0 && xwrite (sock, buf, len) == 0 ? 0 : -1); - if (err) { - fprintf (stderr, "guestfsd: send_chunk: write failed\n"); - exit (EXIT_FAILURE); - } + if (err) + error (EXIT_FAILURE, 0, "send_chunk: write failed"); return err; } @@ -684,10 +660,8 @@ notify_progress_no_ratelimit (uint64_t position, uint64_t total, xdr_u_int (&xdr, &i); xdr_destroy (&xdr); - if (xwrite (sock, buf, 4) == -1) { - fprintf (stderr, "guestfsd: xwrite failed\n"); - exit (EXIT_FAILURE); - } + if (xwrite (sock, buf, 4) == -1) + error (EXIT_FAILURE, 0, "xwrite failed"); message.proc = proc_nr; message.serial = serial; @@ -703,10 +677,8 @@ notify_progress_no_ratelimit (uint64_t position, uint64_t total, len = xdr_getpos (&xdr); xdr_destroy (&xdr); - if (xwrite (sock, buf, len) == -1) { - fprintf (stderr, "guestfsd: xwrite failed\n"); - exit (EXIT_FAILURE); - } + if (xwrite (sock, buf, len) == -1) + error (EXIT_FAILURE, 0, "xwrite failed"); } /* "Pulse mode" progress messages. */ diff --git a/df/domains.c b/df/domains.c index 2407eece6..c65164584 100644 --- a/df/domains.c +++ b/df/domains.c @@ -84,19 +84,17 @@ get_all_libvirt_domains (const char *libvirt_uri) VIR_CONNECT_RO); if (!conn) { err = virGetLastError (); - fprintf (stderr, - _("%s: could not connect to libvirt (code %d, domain %d): %s\n"), - guestfs_int_program_name, err->code, err->domain, err->message); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + _("could not connect to libvirt (code %d, domain %d): %s"), + err->code, err->domain, err->message); } n = virConnectNumOfDomains (conn); if (n == -1) { err = virGetLastError (); - fprintf (stderr, - _("%s: could not get number of running domains (code %d, domain %d): %s\n"), - guestfs_int_program_name, err->code, err->domain, err->message); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + _("could not get number of running domains (code %d, domain %d): %s"), + err->code, err->domain, err->message); } ids = malloc (sizeof (int) * n); @@ -105,10 +103,9 @@ get_all_libvirt_domains (const char *libvirt_uri) n = virConnectListDomains (conn, ids, n); if (n == -1) { err = virGetLastError (); - fprintf (stderr, - _("%s: could not list running domains (code %d, domain %d): %s\n"), - guestfs_int_program_name, err->code, err->domain, err->message); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + _("could not list running domains (code %d, domain %d): %s"), + err->code, err->domain, err->message); } add_domains_by_id (conn, ids, n); @@ -116,10 +113,9 @@ get_all_libvirt_domains (const char *libvirt_uri) n = virConnectNumOfDefinedDomains (conn); if (n == -1) { err = virGetLastError (); - fprintf (stderr, - _("%s: could not get number of inactive domains (code %d, domain %d): %s\n"), - guestfs_int_program_name, err->code, err->domain, err->message); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + _("could not get number of inactive domains (code %d, domain %d): %s"), + err->code, err->domain, err->message); } names = malloc (sizeof (char *) * n); @@ -128,10 +124,9 @@ get_all_libvirt_domains (const char *libvirt_uri) n = virConnectListDefinedDomains (conn, names, n); if (n == -1) { err = virGetLastError (); - fprintf (stderr, - _("%s: could not list inactive domains (code %d, domain %d): %s\n"), - guestfs_int_program_name, err->code, err->domain, err->message); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + _("could not list inactive domains (code %d, domain %d): %s"), + err->code, err->domain, err->message); } add_domains_by_name (conn, names, n); diff --git a/df/main.c b/df/main.c index 88661514b..e04b14e49 100644 --- a/df/main.c +++ b/df/main.c @@ -131,10 +131,8 @@ main (int argc, char *argv[]) int err; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -154,11 +152,10 @@ main (int argc, char *argv[]) /* nothing - left for backwards compatibility */ } else if (STREQ (long_options[option_index].name, "uuid")) { uuid = 1; - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': @@ -182,11 +179,8 @@ main (int argc, char *argv[]) break; case 'P': - if (sscanf (optarg, "%zu", &max_threads) != 1) { - fprintf (stderr, _("%s: -P option is not numeric\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (sscanf (optarg, "%zu", &max_threads) != 1) + error (EXIT_FAILURE, 0, _("-P option is not numeric")); break; case 'v': @@ -256,11 +250,8 @@ main (int argc, char *argv[]) /* -h and --csv doesn't make sense. Spreadsheets will corrupt these * fields. (RHBZ#600977). */ - if (human && csv) { - fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (human && csv) + error (EXIT_FAILURE, 0, _("you cannot use -h and --csv options together.")); /* virt-df has two modes. If the user didn't specify any drives, * then we do the df on every libvirt guest. That's the if-clause @@ -274,9 +265,8 @@ main (int argc, char *argv[]) err = start_threads (max_threads, g, df_work); free_domains (); #else - fprintf (stderr, _("%s: compiled without support for libvirt.\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + _("compiled without support for libvirt.")); #endif } else { /* Single guest. */ diff --git a/diff/diff.c b/diff/diff.c index 2e099dbf7..d7542fc6f 100644 --- a/diff/diff.c +++ b/diff/diff.c @@ -190,16 +190,12 @@ main (int argc, char *argv[]) struct tree *tree1, *tree2; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); g2 = guestfs_create (); - if (g2 == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g2 == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -260,12 +256,10 @@ main (int argc, char *argv[]) } else if (STREQ (long_options[option_index].name, "xattr") || STREQ (long_options[option_index].name, "xattrs")) { enable_xattrs = 1; - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, - long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': @@ -327,17 +321,11 @@ main (int argc, char *argv[]) /* CSV && human is unsafe because spreadsheets fail to parse these * fields correctly. (RHBZ#600977). */ - if (human && csv) { - fprintf (stderr, _("%s: you cannot use -h and --csv options together.\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (human && csv) + error (EXIT_FAILURE, 0, _("you cannot use -h and --csv options together.")); - if (optind != argc) { - fprintf (stderr, _("%s: extra arguments on the command line\n"), - guestfs_int_program_name); - usage (EXIT_FAILURE); - } + if (optind != argc) + error (EXIT_FAILURE, 0, _("extra arguments on the command line")); /* These are really constants, but they have to be variables for the * options parsing code. Assert here that they have known-good diff --git a/edit/edit.c b/edit/edit.c index 38af8a500..6e7aee48a 100644 --- a/edit/edit.c +++ b/edit/edit.c @@ -133,10 +133,8 @@ main (int argc, char *argv[]) int option_index; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -154,12 +152,10 @@ main (int argc, char *argv[]) echo_keys = 1; } else if (STREQ (long_options[option_index].name, "format")) { OPTION_format; - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, - long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': @@ -167,11 +163,8 @@ main (int argc, char *argv[]) break; case 'b': - if (backup_extension) { - fprintf (stderr, _("%s: -b option given multiple times\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (backup_extension) + error (EXIT_FAILURE, 0, _("-b option given multiple times")); backup_extension = optarg; break; @@ -184,11 +177,8 @@ main (int argc, char *argv[]) break; case 'e': - if (perl_expr) { - fprintf (stderr, _("%s: -e option given multiple times\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (perl_expr) + error (EXIT_FAILURE, 0, _("-e option given multiple times")); perl_expr = optarg; break; diff --git a/fish/config.c b/fish/config.c index 4463d9fab..17c801cc3 100644 --- a/fish/config.c +++ b/fish/config.c @@ -61,13 +61,10 @@ read_config_from_file (const char *filename) guestfs_int_program_name, filename); */ - if (config_read (&conf, fp) == CONFIG_FALSE) { - fprintf (stderr, - _("%s: %s: line %d: error parsing configuration file: %s\n"), - guestfs_int_program_name, filename, config_error_line (&conf), - config_error_text (&conf)); - exit (EXIT_FAILURE); - } + if (config_read (&conf, fp) == CONFIG_FALSE) + error (EXIT_FAILURE, 0, + _("%s: line %d: error parsing configuration file: %s"), + filename, config_error_line (&conf), config_error_text (&conf)); if (fclose (fp) == -1) error (EXIT_FAILURE, errno, "fclose: %s", filename); diff --git a/fish/fish.c b/fish/fish.c index 6f9c78474..bfb4f8287 100644 --- a/fish/fish.c +++ b/fish/fish.c @@ -234,10 +234,8 @@ main (int argc, char *argv[]) * it's OK to do it early here. */ g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -253,19 +251,15 @@ main (int argc, char *argv[]) remote_control_listen = 1; else if (STREQ (long_options[option_index].name, "remote")) { if (optarg) { - if (sscanf (optarg, "%d", &remote_control) != 1) { - fprintf (stderr, _("%s: --listen=PID: PID was not a number: %s\n"), - guestfs_int_program_name, optarg); - exit (EXIT_FAILURE); - } + if (sscanf (optarg, "%d", &remote_control) != 1) + error (EXIT_FAILURE, 0, + _("--listen=PID: PID was not a number: %s"), optarg); } else { p = getenv ("GUESTFISH_PID"); - if (!p || sscanf (p, "%d", &remote_control) != 1) { - fprintf (stderr, _("%s: remote: $GUESTFISH_PID must be set" - " to the PID of the remote process\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (!p || sscanf (p, "%d", &remote_control) != 1) + error (EXIT_FAILURE, 0, + _("remote: $GUESTFISH_PID must be set" + " to the PID of the remote process")); } } else if (STREQ (long_options[option_index].name, "selinux")) { if (guestfs_set_selinux (g, 1) == -1) @@ -291,11 +285,10 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); } else if (STREQ (long_options[option_index].name, "no-dest-paths")) { complete_dest_paths = 0; - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': @@ -317,11 +310,8 @@ main (int argc, char *argv[]) break; case 'f': - if (file) { - fprintf (stderr, _("%s: only one -f parameter can be given\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (file) + error (EXIT_FAILURE, 0, _("only one -f parameter can be given")); file = optarg; break; @@ -501,26 +491,17 @@ main (int argc, char *argv[]) free_mps (mps); /* Remote control? */ - if (remote_control_listen && remote_control) { - fprintf (stderr, - _("%s: cannot use --listen and --remote options at the same time\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (remote_control_listen && remote_control) + error (EXIT_FAILURE, 0, + _("cannot use --listen and --remote options at the same time")); if (remote_control_listen) { - if (optind < argc) { - fprintf (stderr, - _("%s: extra parameters on the command line with --listen flag\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } - if (file) { - fprintf (stderr, - _("%s: cannot use --listen and --file options at the same time\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (optind < argc) + error (EXIT_FAILURE, 0, + _("extra parameters on the command line with --listen flag")); + if (file) + error (EXIT_FAILURE, 0, + _("cannot use --listen and --file options at the same time")); rc_listen (); goto out_after_handle_close; } @@ -1055,10 +1036,8 @@ cmdline (char *argv[], size_t optind, size_t argc) if (optind >= argc) return; cmd = argv[optind++]; - if (STREQ (cmd, ":")) { - fprintf (stderr, _("%s: empty command on command line\n"), guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (STREQ (cmd, ":")) + error (EXIT_FAILURE, 0, _("empty command on command line")); /* Allow -cmd on the command line to mean (temporarily) override * the normal exit on error (RHBZ#578407). diff --git a/fish/inspect.c b/fish/inspect.c index 97e46ae8b..0b93e8333 100644 --- a/fish/inspect.c +++ b/fish/inspect.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include "c-ctype.h" @@ -58,11 +59,8 @@ compare_keys (const void *p1, const void *p2) void inspect_mount_handle (guestfs_h *g) { - if (live) { - fprintf (stderr, _("%s: don't use --live and -i options together\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (live) + error (EXIT_FAILURE, 0, _("don't use --live and -i options together")); inspect_do_decrypt (g); diff --git a/fish/options.c b/fish/options.c index cc3d4c000..ab5131b0e 100644 --- a/fish/options.c +++ b/fish/options.c @@ -97,12 +97,8 @@ add_drives_handle (guestfs_h *g, struct drv *drv, char next_drive) int r; struct guestfs_add_drive_opts_argv ad_optargs; - if (next_drive > 'z') { - fprintf (stderr, - _("%s: too many drives added on the command line\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (next_drive > 'z') + error (EXIT_FAILURE, 0, _("too many drives added on the command line")); if (drv) { next_drive = add_drives (drv->next, next_drive); @@ -280,13 +276,10 @@ display_mountpoints_on_failure (const char *mp_device, guestfs_push_error_handler (g, NULL, NULL); subvolume = guestfs_mountable_subvolume (g, fses[i]); - if (subvolume == NULL && guestfs_last_errno (g) != EINVAL) { - fprintf (stderr, - _("%s: cannot determine the subvolume for %s: %s (%d)\n"), - guestfs_int_program_name, fses[i], - guestfs_last_error (g), guestfs_last_errno (g)); - exit (EXIT_FAILURE); - } + if (subvolume == NULL && guestfs_last_errno (g) != EINVAL) + error (EXIT_FAILURE, 0, + _("cannot determine the subvolume for %s: %s (%d)"), + fses[i], guestfs_last_error (g), guestfs_last_errno (g)); guestfs_pop_error_handler (g); diff --git a/fish/prep.c b/fish/prep.c index b8976e986..dfa550725 100644 --- a/fish/prep.c +++ b/fish/prep.c @@ -99,13 +99,11 @@ parse_type_string (const char *type_string) if (STRCASEEQLEN (type_string, preps[i].name, len)) break; - if (i == NR_PREPS) { - fprintf (stderr, _("\ -guestfish: -N parameter '%s': no such prepared disk image known.\n\ -Use 'guestfish -N help' to list possible values for the -N parameter.\n"), - type_string); - exit (EXIT_FAILURE); - } + if (i == NR_PREPS) + error (EXIT_FAILURE, 0, + _("-N parameter '%s': no such prepared disk image known.\n" + "Use 'guestfish -N help' to list possible values for the -N parameter."), + type_string); prep_data *data = malloc (sizeof *data); if (data == NULL) diff --git a/fish/rc.c b/fish/rc.c index ad673f8e5..bc8d31ca9 100644 --- a/fish/rc.c +++ b/fish/rc.c @@ -62,12 +62,10 @@ create_sockdir (void) goto error; if (!S_ISDIR (statbuf.st_mode) || (statbuf.st_mode & 0777) != 0700 || - statbuf.st_uid != euid) { - fprintf (stderr, - _("guestfish: '%s' is not a directory or has insecure owner or permissions\n"), - dir); - exit (EXIT_FAILURE); - } + statbuf.st_uid != euid) + error (EXIT_FAILURE, 0, + _("'%s' is not a directory or has insecure owner or permissions"), + dir); } static void diff --git a/fish/windows.c b/fish/windows.c index 063440639..cb775077d 100644 --- a/fish/windows.c +++ b/fish/windows.c @@ -97,11 +97,9 @@ mount_drive_letter (guestfs_h *g, char drive_letter, const char *root, /* Resolve the drive letter using the drive mappings table. */ CLEANUP_FREE_STRING_LIST char **drives = guestfs_inspect_get_drive_mappings (g, root); - if (drives == NULL || drives[0] == NULL) { - fprintf (stderr, _("%s: to use Windows drive letters, this must be a Windows guest\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (drives == NULL || drives[0] == NULL) + error (EXIT_FAILURE, 0, + _("to use Windows drive letters, this must be a Windows guest")); device = NULL; for (i = 0; drives[i] != NULL; i += 2) { @@ -111,11 +109,8 @@ mount_drive_letter (guestfs_h *g, char drive_letter, const char *root, } } - if (device == NULL) { - fprintf (stderr, _("%s: drive '%c:' not found.\n"), - guestfs_int_program_name, drive_letter); - exit (EXIT_FAILURE); - } + if (device == NULL) + error (EXIT_FAILURE, 0, _("drive '%c:' not found."), drive_letter); /* Unmount current disk and remount device. */ if (guestfs_umount_all (g) == -1) diff --git a/format/format.c b/format/format.c index 781423f37..4aa31deeb 100644 --- a/format/format.c +++ b/format/format.c @@ -128,10 +128,8 @@ main (int argc, char *argv[]) int retry, retries; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -148,19 +146,14 @@ main (int argc, char *argv[]) } else if (STREQ (long_options[option_index].name, "filesystem")) { if (STREQ (optarg, "none")) filesystem = NULL; - else if (optarg[0] == '-') { /* eg: --filesystem --lvm */ - fprintf (stderr, _("%s: no filesystem was specified\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } else + else if (optarg[0] == '-') /* eg: --filesystem --lvm */ + error (EXIT_FAILURE, 0, _("no filesystem was specified")); + else filesystem = optarg; } else if (STREQ (long_options[option_index].name, "lvm")) { - if (vg || lv) { - fprintf (stderr, - _("%s: --lvm option cannot be given multiple times\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (vg || lv) + error (EXIT_FAILURE, 0, + _("--lvm option cannot be given multiple times")); if (optarg == NULL) { vg = strdup ("VG"); lv = strdup ("LV"); @@ -182,12 +175,10 @@ main (int argc, char *argv[]) wipe = 1; } else if (STREQ (long_options[option_index].name, "label")) { label = optarg; - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, - long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': @@ -271,16 +262,13 @@ main (int argc, char *argv[]) guestfs_close (g); g = g2; } - else { + else /* Failed. */ - fprintf (stderr, - _("%s: failed to rescan the disks after two attempts. This\n" - "may mean there is some sort of partition table or disk\n" - "data which we are unable to remove. If you think this\n" - "is a bug, please file a bug at http://libguestfs.org/\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + error (EXIT_FAILURE, 0, + _("failed to rescan the disks after two attempts. This\n" + "may mean there is some sort of partition table or disk\n" + "data which we are unable to remove. If you think this\n" + "is a bug, please file a bug at http://libguestfs.org/\n")); } /* Free up data structures. */ @@ -311,12 +299,9 @@ parse_vg_lv (const char *lvm) if (!vg || !lv) error (EXIT_FAILURE, errno, "strdup"); - } else { + } else cannot_parse: - fprintf (stderr, _("%s: cannot parse --lvm option (%s)\n"), - guestfs_int_program_name, lvm); - exit (EXIT_FAILURE); - } + error (EXIT_FAILURE, 0, _("cannot parse --lvm option (%s)"), lvm); if (strchr (vg, '/') || strchr (lv, '/')) goto cannot_parse; diff --git a/fuse/guestmount.c b/fuse/guestmount.c index 6ab654a2d..fc03a9cc5 100644 --- a/fuse/guestmount.c +++ b/fuse/guestmount.c @@ -207,10 +207,8 @@ main (int argc, char *argv[]) sigaction (SIGPIPE, &sa, NULL); g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -242,17 +240,13 @@ main (int argc, char *argv[]) } else if (STREQ (long_options[option_index].name, "no-fork")) { do_fork = 0; } else if (STREQ (long_options[option_index].name, "fd")) { - if (sscanf (optarg, "%d", &pipe_fd) != 1 || pipe_fd < 0) { - fprintf (stderr, _("%s: unable to parse --fd option value: %s\n"), - guestfs_int_program_name, optarg); - exit (EXIT_FAILURE); - } - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, - long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + if (sscanf (optarg, "%d", &pipe_fd) != 1 || pipe_fd < 0) + error (EXIT_FAILURE, 0, + _("unable to parse --fd option value: %s"), optarg); + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': @@ -317,29 +311,18 @@ main (int argc, char *argv[]) /* Check we have the right options. */ if (!live) { - if (!drvs || !(mps || inspector)) { - fprintf (stderr, - _("%s: must have at least one -a/-d and at least one -m/-i option\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (!drvs || !(mps || inspector)) + error (EXIT_FAILURE, 0, + _("must have at least one -a/-d and at least one -m/-i option")); } else { size_t count_d = 0, count_other = 0; struct drv *drv; - if (read_only) { - fprintf (stderr, - _("%s: --live is not compatible with --ro option\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (read_only) + error (EXIT_FAILURE, 0, _("--live is not compatible with --ro option")); - if (inspector) { - fprintf (stderr, - _("%s: --live is not compatible with -i option\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (inspector) + error (EXIT_FAILURE, 0, _("--live is not compatible with -i option")); /* --live: make sure there was one -d option and no -a options */ for (drv = drvs; drv; drv = drv->next) { @@ -349,28 +332,18 @@ main (int argc, char *argv[]) count_other++; } - if (count_d != 1) { - fprintf (stderr, - _("%s: with --live, you must use exactly one -d option\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (count_d != 1) + error (EXIT_FAILURE, 0, + _("with --live, you must use exactly one -d option")); - if (count_other != 0) { - fprintf (stderr, - _("%s: --live is not compatible with -a option\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (count_other != 0) + error (EXIT_FAILURE, 0, _("--live is not compatible with -a option")); } /* We'd better have a mountpoint. */ - if (optind+1 != argc) { - fprintf (stderr, - _("%s: you must specify a mountpoint in the host filesystem\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (optind+1 != argc) + error (EXIT_FAILURE, 0, + _("you must specify a mountpoint in the host filesystem")); /* If we're forking, we can't use the recovery process. */ if (guestfs_set_recovery_proc (g, !do_fork) == -1) diff --git a/fuse/guestunmount.c b/fuse/guestunmount.c index 06db4ef69..dc9428cf4 100644 --- a/fuse/guestunmount.c +++ b/fuse/guestunmount.c @@ -107,24 +107,19 @@ main (int argc, char *argv[]) switch (c) { case 0: /* options which are long only */ if (STREQ (long_options[option_index].name, "fd")) { - if (sscanf (optarg, "%d", &fd) != 1 || fd < 0) { - fprintf (stderr, _("%s: cannot parse fd option '%s'\n"), - guestfs_int_program_name, optarg); - exit (EXIT_FAILURE); - } + if (sscanf (optarg, "%d", &fd) != 1 || fd < 0) + error (EXIT_FAILURE, 0, _("cannot parse fd option '%s'"), optarg); } else if (STREQ (long_options[option_index].name, "no-retry")) { retries = 0; } else if (STREQ (long_options[option_index].name, "retry")) { - if (sscanf (optarg, "%zu", &retries) != 1 || retries >= 64) { - fprintf (stderr, _("%s: cannot parse retries option or value is too large '%s'\n"), - guestfs_int_program_name, optarg); - exit (EXIT_FAILURE); - } - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + if (sscanf (optarg, "%zu", &retries) != 1 || retries >= 64) + error (EXIT_FAILURE, 0, + _("cannot parse retries option or value is too large '%s'"), + optarg); + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'q': @@ -148,12 +143,9 @@ main (int argc, char *argv[]) } /* We'd better have a mountpoint. */ - if (optind+1 != argc) { - fprintf (stderr, - _("%s: you must specify a mountpoint in the host filesystem\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (optind+1 != argc) + error (EXIT_FAILURE, 0, + _("you must specify a mountpoint in the host filesystem\n")); mountpoint = argv[optind]; diff --git a/fuse/test-guestmount-fd.c b/fuse/test-guestmount-fd.c index 5f358bb78..388909423 100644 --- a/fuse/test-guestmount-fd.c +++ b/fuse/test-guestmount-fd.c @@ -51,18 +51,12 @@ main (int argc, char *argv[]) /* Allow the test to be skipped. */ skip = getenv ("SKIP_TEST_FUSE_SH"); - if (skip && guestfs_int_is_true (skip) > 0) { - fprintf (stderr, "%s: test skipped because environment variable set.\n", - guestfs_int_program_name); - exit (77); - } + if (skip && guestfs_int_is_true (skip) > 0) + error (77, 0, "test skipped because environment variable set"); skip = getenv ("SKIP_TEST_GUESTMOUNT_FD"); - if (skip && guestfs_int_is_true (skip) > 0) { - fprintf (stderr, "%s: test skipped because environment variable set.\n", - guestfs_int_program_name); - exit (77); - } + if (skip && guestfs_int_is_true (skip) > 0) + error (77, 0, "test skipped because environment variable set"); /* Skip the test if the test image can't be found. */ if (access (TEST_IMAGE, R_OK) == -1) diff --git a/fuse/test-guestunmount-fd.c b/fuse/test-guestunmount-fd.c index a93789306..ad7ff74c8 100644 --- a/fuse/test-guestunmount-fd.c +++ b/fuse/test-guestunmount-fd.c @@ -47,18 +47,12 @@ main (int argc, char *argv[]) /* Allow the test to be skipped. */ skip = getenv ("SKIP_TEST_FUSE_SH"); - if (skip && guestfs_int_is_true (skip) > 0) { - fprintf (stderr, "%s: test skipped because environment variable set.\n", - guestfs_int_program_name); - exit (77); - } + if (skip && guestfs_int_is_true (skip) > 0) + error (77, 0, "test skipped because environment variable set"); skip = getenv ("SKIP_TEST_GUESTUNMOUNT_FD"); - if (skip && guestfs_int_is_true (skip) > 0) { - fprintf (stderr, "%s: test skipped because environment variable set.\n", - guestfs_int_program_name); - exit (77); - } + if (skip && guestfs_int_is_true (skip) > 0) + error (77, 0, "test skipped because environment variable set"); /* Create the pipe. */ if (pipe (pipefd) == -1) @@ -94,11 +88,11 @@ main (int argc, char *argv[]) if (r != 0) { char status_string[80]; - fprintf (stderr, "%s: test failed: %s\n", guestfs_int_program_name, - guestfs_int_exit_status_to_string (r, "guestunmount", - status_string, - sizeof status_string)); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + "test failed: %s", + guestfs_int_exit_status_to_string (r, "guestunmount", + status_string, + sizeof status_string)); } /* Close the write side of the pipe. This should cause guestunmount @@ -113,12 +107,11 @@ main (int argc, char *argv[]) if (!WIFEXITED (status) || WEXITSTATUS (status) != 3) { char status_string[80]; - fprintf (stderr, "%s: test failed: guestunmount didn't return status code 3; %s\n", - guestfs_int_program_name, - guestfs_int_exit_status_to_string (status, "guestunmount", - status_string, - sizeof status_string)); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + "test failed: guestunmount didn't return status code 3; %s", + guestfs_int_exit_status_to_string (status, "guestunmount", + status_string, + sizeof status_string)); } exit (EXIT_SUCCESS); diff --git a/inspector/inspector.c b/inspector/inspector.c index 2e54924f5..6d4694da4 100644 --- a/inspector/inspector.c +++ b/inspector/inspector.c @@ -131,10 +131,8 @@ main (int argc, char *argv[]) int option_index; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -158,12 +156,10 @@ main (int argc, char *argv[]) inspect_apps = 0; } else if (STREQ (long_options[option_index].name, "no-icon")) { inspect_icon = 0; - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, - long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': @@ -246,11 +242,9 @@ main (int argc, char *argv[]) * one extra parameter on the command line. */ if (xpath) { - if (drvs != NULL) { - fprintf (stderr, _("%s: cannot use --xpath together with other options.\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (drvs != NULL) + error (EXIT_FAILURE, 0, + _("cannot use --xpath together with other options.")); do_xpath (xpath); @@ -280,11 +274,9 @@ main (int argc, char *argv[]) { CLEANUP_FREE_STRING_LIST char **roots = guestfs_inspect_os (g); - if (roots == NULL) { - fprintf (stderr, _("%s: no operating system could be detected inside this disk image.\n\nThis may be because the file is not a disk image, or is not a virtual machine\nimage, or because the OS type is not understood by libguestfs.\n\nNOTE for Red Hat Enterprise Linux 6 users: for Windows guest support you must\ninstall the separate libguestfs-winsupport package.\n\nIf you feel this is an error, please file a bug report including as much\ninformation about the disk image as possible.\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (roots == NULL) + error (EXIT_FAILURE, 0, + _("no operating system could be detected inside this disk image.\n\nThis may be because the file is not a disk image, or is not a virtual machine\nimage, or because the OS type is not understood by libguestfs.\n\nNOTE for Red Hat Enterprise Linux 6 users: for Windows guest support you must\ninstall the separate libguestfs-winsupport package.\n\nIf you feel this is an error, please file a bug report including as much\ninformation about the disk image as possible.\n")); output (roots); } @@ -295,32 +287,23 @@ main (int argc, char *argv[]) } #define XMLERROR(code,e) do { \ - if ((e) == (code)) { \ - fprintf (stderr, _("%s: XML write error at \"%s\": %m\n"), \ - #e, guestfs_int_program_name); \ - exit (EXIT_FAILURE); \ - } \ + if ((e) == (code)) \ + error (EXIT_FAILURE, errno, _("XML write error at \"%s\""), #e); \ } while (0) static void output (char **roots) { xmlOutputBufferPtr ob = xmlOutputBufferCreateFd (1, NULL); - if (ob == NULL) { - fprintf (stderr, - _("%s: xmlOutputBufferCreateFd: failed to open stdout\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (ob == NULL) + error (EXIT_FAILURE, 0, + _("xmlOutputBufferCreateFd: failed to open stdout")); /* 'ob' is freed when 'xo' is freed.. */ CLEANUP_XMLFREETEXTWRITER xmlTextWriterPtr xo = xmlNewTextWriter (ob); - if (xo == NULL) { - fprintf (stderr, - _("%s: xmlNewTextWriter: failed to create libxml2 writer\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (xo == NULL) + error (EXIT_FAILURE, 0, + _("xmlNewTextWriter: failed to create libxml2 writer")); /* Pretty-print the output. */ XMLERROR (-1, xmlTextWriterSetIndent (xo, 1)); @@ -780,25 +763,16 @@ do_xpath (const char *query) xmlNodePtr wrnode; doc = xmlReadFd (STDIN_FILENO, NULL, "utf8", XML_PARSE_NOBLANKS); - if (doc == NULL) { - fprintf (stderr, _("%s: unable to parse XML from stdin\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (doc == NULL) + error (EXIT_FAILURE, 0, _("unable to parse XML from stdin")); xpathCtx = xmlXPathNewContext (doc); - if (xpathCtx == NULL) { - fprintf (stderr, _("%s: unable to create new XPath context\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (xpathCtx == NULL) + error (EXIT_FAILURE, 0, _("unable to create new XPath context")); xpathObj = xmlXPathEvalExpression (BAD_CAST query, xpathCtx); - if (xpathObj == NULL) { - fprintf (stderr, _("%s: unable to evaluate XPath expression\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (xpathObj == NULL) + error (EXIT_FAILURE, 0, _("unable to evaluate XPath expression")); switch (xpathObj->type) { case XPATH_NODESET: @@ -808,33 +782,21 @@ do_xpath (const char *query) saveCtx = xmlSaveToFd (STDOUT_FILENO, NULL, XML_SAVE_NO_DECL | XML_SAVE_FORMAT); - if (saveCtx == NULL) { - fprintf (stderr, _("%s: xmlSaveToFd failed\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (saveCtx == NULL) + error (EXIT_FAILURE, 0, _("xmlSaveToFd failed")); for (i = 0; i < (size_t) nodes->nodeNr; ++i) { CLEANUP_XMLFREEDOC xmlDocPtr wrdoc = xmlNewDoc (BAD_CAST "1.0"); - if (wrdoc == NULL) { - fprintf (stderr, _("%s: xmlNewDoc failed\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (wrdoc == NULL) + error (EXIT_FAILURE, 0, _("xmlNewDoc failed")); wrnode = xmlDocCopyNode (nodes->nodeTab[i], wrdoc, 1); - if (wrnode == NULL) { - fprintf (stderr, _("%s: xmlCopyNode failed\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (wrnode == NULL) + error (EXIT_FAILURE, 0, _("xmlCopyNode failed")); xmlDocSetRootElement (wrdoc, wrnode); - if (xmlSaveDoc (saveCtx, wrdoc) == -1) { - fprintf (stderr, _("%s: xmlSaveDoc failed\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (xmlSaveDoc (saveCtx, wrdoc) == -1) + error (EXIT_FAILURE, 0, _("xmlSaveDoc failed")); } xmlSaveClose (saveCtx); diff --git a/make-fs/make-fs.c b/make-fs/make-fs.c index 2778772b9..42e7e8273 100644 --- a/make-fs/make-fs.c +++ b/make-fs/make-fs.c @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -112,10 +113,8 @@ main (int argc, char *argv[]) textdomain (PACKAGE); g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -142,12 +141,10 @@ main (int argc, char *argv[]) partition = "mbr"; else partition = optarg; - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, - long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'F': diff --git a/p2v/gui.c b/p2v/gui.c index c5fbc9966..625c6eb55 100644 --- a/p2v/gui.c +++ b/p2v/gui.c @@ -322,10 +322,8 @@ test_connection_clicked (GtkWidget *w, gpointer data) pthread_attr_init (&attr); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); err = pthread_create (&tid, &attr, test_connection_thread, copy); - if (err != 0) { - fprintf (stderr, "pthread_create: %s\n", strerror (err)); - exit (EXIT_FAILURE); - } + if (err != 0) + error (EXIT_FAILURE, err, "pthread_create"); pthread_attr_destroy (&attr); } @@ -1557,10 +1555,8 @@ start_conversion_clicked (GtkWidget *w, gpointer data) pthread_attr_init (&attr); pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); err = pthread_create (&tid, &attr, start_conversion_thread, copy); - if (err != 0) { - fprintf (stderr, "pthread_create: %s\n", strerror (err)); - exit (EXIT_FAILURE); - } + if (err != 0) + error (EXIT_FAILURE, err, "pthread_create"); pthread_attr_destroy (&attr); } diff --git a/p2v/kernel.c b/p2v/kernel.c index 61fbecd23..fb5278525 100644 --- a/p2v/kernel.c +++ b/p2v/kernel.c @@ -54,11 +54,9 @@ kernel_configuration (struct config *config, char **cmdline, int cmdline_source) p = get_cmdline_key (cmdline, "p2v.port"); if (p) { - if (sscanf (p, "%d", &config->port) != 1) { - fprintf (stderr, "%s: cannot parse p2v.port from kernel command line", - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (sscanf (p, "%d", &config->port) != 1) + error (EXIT_FAILURE, 0, + "cannot parse p2v.port from kernel command line"); } p = get_cmdline_key (cmdline, "p2v.username"); @@ -93,9 +91,9 @@ kernel_configuration (struct config *config, char **cmdline, int cmdline_source) if (test_connection (config) == -1) { const char *err = get_ssh_error (); - fprintf (stderr, "%s: error opening control connection to %s:%d: %s\n", - guestfs_int_program_name, config->server, config->port, err); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + "error opening control connection to %s:%d: %s", + config->server, config->port, err); } } @@ -107,22 +105,18 @@ kernel_configuration (struct config *config, char **cmdline, int cmdline_source) p = get_cmdline_key (cmdline, "p2v.vcpus"); if (p) { - if (sscanf (p, "%d", &config->vcpus) != 1) { - fprintf (stderr, "%s: cannot parse p2v.vcpus from kernel command line\n", - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (sscanf (p, "%d", &config->vcpus) != 1) + error (EXIT_FAILURE, 0, + "cannot parse p2v.vcpus from kernel command line"); } p = get_cmdline_key (cmdline, "p2v.memory"); if (p) { char mem_code; - if (sscanf (p, "%" SCNu64 "%c", &config->memory, &mem_code) != 2) { - fprintf (stderr, "%s: cannot parse p2v.memory from kernel command line\n", - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (sscanf (p, "%" SCNu64 "%c", &config->memory, &mem_code) != 2) + error (EXIT_FAILURE, 0, + "cannot parse p2v.memory from kernel command line"); config->memory *= 1024; if (mem_code == 'M' || mem_code == 'm' || mem_code == 'G' || mem_code == 'g') @@ -130,11 +124,9 @@ kernel_configuration (struct config *config, char **cmdline, int cmdline_source) if (mem_code == 'G' || mem_code == 'g') config->memory *= 1024; if (mem_code != 'M' && mem_code != 'm' - && mem_code != 'G' && mem_code != 'g') { - fprintf (stderr, "%s: p2v.memory on kernel command line must be followed by 'G' or 'M'\n", - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + && mem_code != 'G' && mem_code != 'g') + error (EXIT_FAILURE, 0, + "p2v.memory on kernel command line must be followed by 'G' or 'M'"); } p = get_cmdline_key (cmdline, "p2v.disks"); @@ -216,13 +208,11 @@ kernel_configuration (struct config *config, char **cmdline, int cmdline_source) } /* Some disks must have been specified for conversion. */ - if (config->disks == NULL || guestfs_int_count_strings (config->disks) == 0) { - fprintf (stderr, "%s: error: no non-removable disks were discovered on this machine.\n", - guestfs_int_program_name); - fprintf (stderr, "virt-p2v looked in /sys/block and in p2v.disks on the kernel command line.\n"); - fprintf (stderr, "This is a fatal error and virt-p2v cannot continue.\n"); - exit (EXIT_FAILURE); - } + if (config->disks == NULL || guestfs_int_count_strings (config->disks) == 0) + error (EXIT_FAILURE, 0, + "no non-removable disks were discovered on this machine.\n" + "virt-p2v looked in /sys/block and in p2v.disks on the kernel command line.\n" + "This is a fatal error and virt-p2v cannot continue."); /* Perform the conversion in text mode. */ if (start_conversion (config, notify_ui_callback) == -1) { @@ -285,9 +275,7 @@ run_command (int verbose, const char *stage, const char *command) r = system (command); if (r == -1) error (EXIT_FAILURE, errno, "system: %s", command); - if ((WIFEXITED (r) && WEXITSTATUS (r) != 0) || !WIFEXITED (r)) { - fprintf (stderr, "%s: %s: unexpected failure of external command\n", - guestfs_int_program_name, stage); - exit (EXIT_FAILURE); - } + if ((WIFEXITED (r) && WEXITSTATUS (r) != 0) || !WIFEXITED (r)) + error (EXIT_FAILURE, 0, + "%s: unexpected failure of external command", stage); } diff --git a/p2v/main.c b/p2v/main.c index abec9bfcc..34cbd4d44 100644 --- a/p2v/main.c +++ b/p2v/main.c @@ -146,11 +146,10 @@ main (int argc, char *argv[]) cmdline = parse_cmdline_string (optarg); cmdline_source = CMDLINE_SOURCE_COMMAND_LINE; } - else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'v': @@ -198,13 +197,10 @@ main (int argc, char *argv[]) kernel_configuration (config, cmdline, cmdline_source); else { gui: - if (!gui_possible) { - fprintf (stderr, - _("%s: gtk_init_check returned false, indicating that\n" - "a GUI is not possible on this host. Check X11, $DISPLAY etc.\n"), - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (!gui_possible) + error (EXIT_FAILURE, 0, + _("gtk_init_check returned false, indicating that\n" + "a GUI is not possible on this host. Check X11, $DISPLAY etc.")); gui_application (config); } diff --git a/p2v/ssh.c b/p2v/ssh.c index 6ddfcb28f..7158e7011 100644 --- a/p2v/ssh.c +++ b/p2v/ssh.c @@ -415,10 +415,8 @@ start_ssh (struct config *config, char **extra_args, int wait_prompt) */ r = pcre_get_substring (h->buffer, ovector, mexp_get_pcre_error (h), 1, &matched); - if (r < 0) { - fprintf (stderr, "error: PCRE error reading substring (%d)\n", r); - exit (EXIT_FAILURE); - } + if (r < 0) + error (EXIT_FAILURE, 0, "PCRE error reading substring (%d)", r); r = STREQ (magic, matched); pcre_free_substring (matched); if (!r) diff --git a/rescue/rescue.c b/rescue/rescue.c index 37a0c3f92..c46c7757e 100644 --- a/rescue/rescue.c +++ b/rescue/rescue.c @@ -133,10 +133,8 @@ main (int argc, char *argv[]) int suggest = 0; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); for (;;) { c = getopt_long (argc, argv, options, long_options, &option_index); @@ -158,16 +156,12 @@ main (int argc, char *argv[]) } else if (STREQ (long_options[option_index].name, "format")) { OPTION_format; } else if (STREQ (long_options[option_index].name, "smp")) { - if (sscanf (optarg, "%d", &smp) != 1) { - fprintf (stderr, _("%s: could not parse --smp parameter '%s'\n"), - guestfs_int_program_name, optarg); - exit (EXIT_FAILURE); - } - if (smp < 1) { - fprintf (stderr, _("%s: --smp parameter '%s' should be >= 1\n"), - guestfs_int_program_name, optarg); - exit (EXIT_FAILURE); - } + if (sscanf (optarg, "%d", &smp) != 1) + error (EXIT_FAILURE, 0, + _("could not parse --smp parameter '%s'"), optarg); + if (smp < 1) + error (EXIT_FAILURE, 0, + _("--smp parameter '%s' should be >= 1"), optarg); } else if (STREQ (long_options[option_index].name, "suggest")) { suggest = 1; } else if (STREQ (long_options[option_index].name, "scratch")) { @@ -175,25 +169,18 @@ main (int argc, char *argv[]) add_scratch_disks (1, &drvs); else { int n; - if (sscanf (optarg, "%d", &n) != 1) { - fprintf (stderr, - _("%s: could not parse --scratch parameter '%s'\n"), - guestfs_int_program_name, optarg); - exit (EXIT_FAILURE); - } - if (n < 1) { - fprintf (stderr, - _("%s: --scratch parameter '%s' should be >= 1\n"), - guestfs_int_program_name, optarg); - exit (EXIT_FAILURE); - } + if (sscanf (optarg, "%d", &n) != 1) + error (EXIT_FAILURE, 0, + _("could not parse --scratch parameter '%s'"), optarg); + if (n < 1) + error (EXIT_FAILURE, 0, + _("--scratch parameter '%s' should be >= 1"), optarg); add_scratch_disks (n, &drvs); } - } else { - fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), - guestfs_int_program_name, long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 'a': @@ -209,11 +196,9 @@ main (int argc, char *argv[]) break; case 'm': - if (sscanf (optarg, "%d", &memsize) != 1) { - fprintf (stderr, _("%s: could not parse memory size '%s'\n"), - guestfs_int_program_name, optarg); - exit (EXIT_FAILURE); - } + if (sscanf (optarg, "%d", &memsize) != 1) + error (EXIT_FAILURE, 0, + _("could not parse memory size '%s'"), optarg); break; case 'r': diff --git a/src/libvirt-is-version.c b/src/libvirt-is-version.c index 212368a80..e350c4d8c 100644 --- a/src/libvirt-is-version.c +++ b/src/libvirt-is-version.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -51,10 +52,7 @@ main (int argc, char *argv[]) major = argtoint (argv[0], argv[1]); break; case 1: - fprintf (stderr, "%s: not enough arguments: MAJOR [MINOR [PATCH]]\n", - argv[0]); - exit (EXIT_FAILURE); - break; + error (EXIT_FAILURE, 0, "not enough arguments: MAJOR [MINOR [PATCH]]"); } virInitialize (); @@ -74,10 +72,8 @@ argtoint (const char *prog, const char *arg) errno = 0; res = strtol (arg, &endptr, 10); - if (errno || *endptr) { - fprintf (stderr, "%s: cannot parse integer argument '%s'.\n", prog, arg); - exit (EXIT_FAILURE); - } + if (errno || *endptr) + error (EXIT_FAILURE, 0, "cannot parse integer argument '%s'", arg); return (unsigned int) res; } diff --git a/test-tool/Makefile.am b/test-tool/Makefile.am index f7d1c7fa3..51779420e 100644 --- a/test-tool/Makefile.am +++ b/test-tool/Makefile.am @@ -33,10 +33,13 @@ libguestfs_test_tool_CPPFLAGS = \ -DLOCALEBASEDIR=\""$(datadir)/locale"\" libguestfs_test_tool_CFLAGS = \ - $(WARN_CFLAGS) $(WERROR_CFLAGS) + $(WARN_CFLAGS) $(WERROR_CFLAGS) \ + $(LIBXML2_CFLAGS) libguestfs_test_tool_LDADD = \ $(top_builddir)/src/libguestfs.la \ + $(top_builddir)/src/libutils.la \ + $(LIBXML2_LIBS) \ $(LTLIBINTL) \ $(top_builddir)/gnulib/lib/libgnu.la diff --git a/test-tool/test-tool.c b/test-tool/test-tool.c index 1aa9bc43f..c63244003 100644 --- a/test-tool/test-tool.c +++ b/test-tool/test-tool.c @@ -35,7 +35,7 @@ #include #include -#include +#include "guestfs.h" #include "guestfs-internal-frontend.h" #include "ignore-value.h" @@ -111,35 +111,24 @@ main (int argc, char *argv[]) qemu = optarg; qemu_use_wrapper = 1; } - else { - fprintf (stderr, - _("libguestfs-test-tool: unknown long option: %s (%d)\n"), - long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + else + error (EXIT_FAILURE, 0, + _("unknown long option: %s (%d)"), + long_options[option_index].name, option_index); break; case 't': - if (sscanf (optarg, "%d", &timeout) != 1 || timeout < 0) { - fprintf (stderr, - _("libguestfs-test-tool: invalid timeout: %s\n"), - optarg); - exit (EXIT_FAILURE); - } + if (sscanf (optarg, "%d", &timeout) != 1 || timeout < 0) + error (EXIT_FAILURE, 0, _("invalid timeout: %s"), optarg); break; case 'V': g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, - _("libguestfs-test-tool: failed to create libguestfs handle\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); vers = guestfs_version (g); - if (vers == NULL) { - fprintf (stderr, _("libguestfs-test-tool: guestfs_version failed\n")); + if (vers == NULL) exit (EXIT_FAILURE); - } printf ("%s %"PRIi64".%"PRIi64".%"PRIi64"%s\n", "libguestfs-test-tool", vers->major, vers->minor, vers->release, vers->extra); @@ -152,17 +141,12 @@ main (int argc, char *argv[]) exit (EXIT_SUCCESS); default: - fprintf (stderr, - _("libguestfs-test-tool: unexpected command line option %d\n"), - c); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, _("unexpected command line option %d"), c); } } - if (optind < argc) { - fprintf (stderr, _("libguestfs-test-tool: extra arguments on the command line\n")); - exit (EXIT_FAILURE); - } + if (optind < argc) + error (EXIT_FAILURE, 0, _("extra arguments on the command line")); /* Everyone ignores the documentation, so ... */ printf (" ************************************************************\n" @@ -177,17 +161,12 @@ main (int argc, char *argv[]) /* Create the handle. */ g = guestfs_create_flags (GUESTFS_CREATE_NO_ENVIRONMENT); - if (g == NULL) { - fprintf (stderr, - _("libguestfs-test-tool: failed to create libguestfs handle\n")); - exit (EXIT_FAILURE); - } - if (guestfs_parse_environment (g) == -1) { - fprintf (stderr, - _("libguestfs-test-tool: failed parsing environment variables.\n" - "Check earlier messages, and the output of the 'printenv' command.\n")); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create_flags"); + if (guestfs_parse_environment (g) == -1) + error (EXIT_FAILURE, 0, + _("failed parsing environment variables.\n" + "Check earlier messages, and the output of the 'printenv' command.")); guestfs_set_verbose (g, 1); if (qemu) @@ -224,11 +203,8 @@ main (int argc, char *argv[]) ignore_value (system ("getenforce")); /* Configure the handle. */ - if (guestfs_add_drive_scratch (g, 100*1024*1024, -1) == -1) { - fprintf (stderr, - _("libguestfs-test-tool: failed to add scratch drive\n")); + if (guestfs_add_drive_scratch (g, 100*1024*1024, -1) == -1) exit (EXIT_FAILURE); - } printf ("guestfs_get_append: %s\n", guestfs_get_append (g) ? : "(null)"); printf ("guestfs_get_autosync: %d\n", guestfs_get_autosync (g)); @@ -277,11 +253,8 @@ main (int argc, char *argv[]) alarm (timeout); - if (guestfs_launch (g) == -1) { - fprintf (stderr, - _("libguestfs-test-tool: failed to launch appliance\n")); + if (guestfs_launch (g) == -1) exit (EXIT_FAILURE); - } alarm (0); @@ -289,36 +262,22 @@ main (int argc, char *argv[]) fflush (stdout); /* Create the filesystem and mount everything. */ - if (guestfs_part_disk (g, "/dev/sda", "mbr") == -1) { - fprintf (stderr, - _("libguestfs-test-tool: failed to run part-disk\n")); + if (guestfs_part_disk (g, "/dev/sda", "mbr") == -1) exit (EXIT_FAILURE); - } - if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1) { - fprintf (stderr, - _("libguestfs-test-tool: failed to mkfs.ext2\n")); + if (guestfs_mkfs (g, "ext2", "/dev/sda1") == -1) exit (EXIT_FAILURE); - } - if (guestfs_mount (g, "/dev/sda1", "/") == -1) { - fprintf (stderr, - _("libguestfs-test-tool: failed to mount /dev/sda1 on /\n")); + if (guestfs_mount (g, "/dev/sda1", "/") == -1) exit (EXIT_FAILURE); - } /* Touch a file. */ - if (guestfs_touch (g, "/hello") == -1) { - fprintf (stderr, - _("libguestfs-test-tool: failed to touch file\n")); + if (guestfs_touch (g, "/hello") == -1) exit (EXIT_FAILURE); - } /* Close the handle. */ - if (guestfs_shutdown (g) == -1) { - fprintf (stderr, _("libguestfs-test-tool: shutdown failed\n")); + if (guestfs_shutdown (g) == -1) exit (EXIT_FAILURE); - } guestfs_close (g); @@ -342,26 +301,21 @@ cleanup_wrapper (void) static void set_qemu (guestfs_h *g, const char *path, int use_wrapper) { - char *buffer; + CLEANUP_FREE char *buffer = NULL; struct stat statbuf; int fd; FILE *fp; if (getenv ("LIBGUESTFS_QEMU") != NULL || - getenv ("LIBGUESTFS_HV") != NULL) { - fprintf (stderr, - _("LIBGUESTFS_HV/LIBGUESTFS_QEMU environment variable is already set, so\n" - "--qemu/--qemudir options cannot be used.\n")); - exit (EXIT_FAILURE); - } + getenv ("LIBGUESTFS_HV") != NULL) + error (EXIT_FAILURE, 0, + _("LIBGUESTFS_HV/LIBGUESTFS_QEMU environment variable is already set, so\n" + "--qemu/--qemudir options cannot be used.")); if (!use_wrapper) { - if (access (path, X_OK) == -1) { - fprintf (stderr, - _("Binary '%s' does not exist or is not executable\n"), - path); - exit (EXIT_FAILURE); - } + if (access (path, X_OK) == -1) + error (EXIT_FAILURE, errno, + _("binary '%s' does not exist or is not executable"), path); guestfs_set_hv (g, path); return; @@ -371,14 +325,9 @@ set_qemu (guestfs_h *g, const char *path, int use_wrapper) if (asprintf (&buffer, "%s/pc-bios", path) == -1) error (EXIT_FAILURE, errno, "asprintf"); if (stat (buffer, &statbuf) == -1 || - !S_ISDIR (statbuf.st_mode)) { - fprintf (stderr, - _("%s: does not look like a qemu source directory\n"), - path); - free (buffer); - exit (EXIT_FAILURE); - } - free (buffer); + !S_ISDIR (statbuf.st_mode)) + error (EXIT_FAILURE, errno, + _("path does not look like a qemu source directory: %s"), path); /* Make a wrapper script. */ fd = mkstemp (qemuwrapper); diff --git a/tests/c-api/test-add-drive-opts.c b/tests/c-api/test-add-drive-opts.c index 7f6bd4a89..4f39e2690 100644 --- a/tests/c-api/test-add-drive-opts.c +++ b/tests/c-api/test-add-drive-opts.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include "guestfs.h" #include "guestfs-internal-frontend.h" @@ -33,10 +35,8 @@ main (int argc, char *argv[]) int r; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, "failed to create handle\n"); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); r = guestfs_add_drive_opts (g, "/dev/null", -1); if (r == -1) diff --git a/tests/c-api/test-add-libvirt-dom.c b/tests/c-api/test-add-libvirt-dom.c index 566a3de3d..612b86e97 100644 --- a/tests/c-api/test-add-libvirt-dom.c +++ b/tests/c-api/test-add-libvirt-dom.c @@ -84,17 +84,13 @@ main (int argc, char *argv[]) /* Create the guestfs handle. */ g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, "failed to create handle\n"); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); backend = guestfs_get_backend (g); if (STREQ (backend, "uml")) { - printf ("%s: test skipped because UML backend does not support qcow2\n", - argv[0]); free (backend); - exit (77); + error (77, 0, "test skipped because UML backend does not support qcow2"); } free (backend); @@ -125,19 +121,16 @@ main (int argc, char *argv[]) conn = virConnectOpenReadOnly (libvirt_uri); if (!conn) { err = virGetLastError (); - fprintf (stderr, - "%s: could not connect to libvirt (code %d, domain %d): %s\n", - argv[0], err->code, err->domain, err->message); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + "could not connect to libvirt (code %d, domain %d): %s", + err->code, err->domain, err->message); } dom = virDomainLookupByName (conn, "guest"); if (!dom) { err = virGetLastError (); - fprintf (stderr, - "%s: no libvirt domain called '%s': %s\n", - argv[0], "guest", err->message); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + "no libvirt domain called '%s': %s", "guest", err->message); } r = guestfs_add_libvirt_dom (g, dom, @@ -146,12 +139,9 @@ main (int argc, char *argv[]) if (r == -1) exit (EXIT_FAILURE); - if (r != 3) { - fprintf (stderr, - "%s: incorrect number of disks added (%d, expected 3)\n", - argv[0], r); - exit (EXIT_FAILURE); - } + if (r != 3) + error (EXIT_FAILURE, 0, + "incorrect number of disks added (%d, expected 3)", r); guestfs_close (g); diff --git a/tests/c-api/test-backend-settings.c b/tests/c-api/test-backend-settings.c index ce1854ff4..371a24776 100644 --- a/tests/c-api/test-backend-settings.c +++ b/tests/c-api/test-backend-settings.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include "guestfs.h" @@ -44,10 +45,8 @@ main (int argc, char *argv[]) unsetenv ("LIBGUESTFS_BACKEND_SETTINGS"); g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, "failed to create handle\n"); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); /* There should be no backend settings initially. */ strs = guestfs_get_backend_settings (g); @@ -82,10 +81,8 @@ main (int argc, char *argv[]) setenv ("LIBGUESTFS_BACKEND_SETTINGS", initial_settings, 1); g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, "failed to create handle\n"); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); } /* Check the settings are correct. */ diff --git a/tests/c-api/test-command.c b/tests/c-api/test-command.c index c23b7d173..b14f0f1ba 100644 --- a/tests/c-api/test-command.c +++ b/tests/c-api/test-command.c @@ -25,6 +25,7 @@ #include #include #include +#include #define STREQ(a,b) (strcmp((a),(b)) == 0) @@ -54,14 +55,10 @@ main (int argc, char *argv[]) printf ("Result10-1\nResult10-2\n"); } else if (STREQ (argv[1], "11")) { printf ("Result11-1\nResult11-2"); - } else { - fprintf (stderr, "unknown parameter: %s\n", argv[1]); - exit (EXIT_FAILURE); - } - } else { - fprintf (stderr, "missing parameter\n"); - exit (EXIT_FAILURE); - } + } else + error (EXIT_FAILURE, 0, "unknown parameter: %s", argv[1]); + } else + error (EXIT_FAILURE, 0, "missing parameter"); exit (EXIT_SUCCESS); } diff --git a/tests/c-api/test-config.c b/tests/c-api/test-config.c index feaeadc3b..fef6c64e5 100644 --- a/tests/c-api/test-config.c +++ b/tests/c-api/test-config.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include "guestfs.h" #include "guestfs-internal-frontend.h" @@ -33,10 +35,8 @@ main (int argc, char *argv[]) int r; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, "failed to create handle\n"); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); /* If these fail, the default error handler will print an error * message to stderr, so we don't need to print anything. This code @@ -49,19 +49,15 @@ main (int argc, char *argv[]) r = guestfs_get_verbose (g); if (r == -1) exit (EXIT_FAILURE); - if (!r) { - fprintf (stderr, "set_verbose not true\n"); - exit (EXIT_FAILURE); - } + if (!r) + error (EXIT_FAILURE, 0, "set_verbose not true"); if (guestfs_set_verbose (g, 0) == -1) exit (EXIT_FAILURE); r = guestfs_get_verbose (g); if (r == -1) exit (EXIT_FAILURE); - if (r) { - fprintf (stderr, "set_verbose not false\n"); - exit (EXIT_FAILURE); - } + if (r) + error (EXIT_FAILURE, 0, "set_verbose not false"); guestfs_close (g); diff --git a/tests/c-api/test-create-handle.c b/tests/c-api/test-create-handle.c index d2bfbbf62..170649926 100644 --- a/tests/c-api/test-create-handle.c +++ b/tests/c-api/test-create-handle.c @@ -22,6 +22,8 @@ #include #include #include +#include +#include #include "guestfs.h" #include "guestfs-internal-frontend.h" @@ -32,10 +34,8 @@ main (int argc, char *argv[]) guestfs_h *g; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, "failed to create handle\n"); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); guestfs_close (g); diff --git a/tests/c-api/test-debug-to-file.c b/tests/c-api/test-debug-to-file.c index c36ae14db..10e36c63b 100644 --- a/tests/c-api/test-debug-to-file.c +++ b/tests/c-api/test-debug-to-file.c @@ -60,10 +60,8 @@ main (int argc, char *argv[]) error (EXIT_FAILURE, errno, "fopen: %s", filename); g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, "failed to create handle\n"); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); if (guestfs_set_event_callback (g, debug_to_file, diff --git a/tests/c-api/test-dlopen.c b/tests/c-api/test-dlopen.c index 3efd847b0..8a4174c74 100644 --- a/tests/c-api/test-dlopen.c +++ b/tests/c-api/test-dlopen.c @@ -24,6 +24,8 @@ #include #include #include +#include +#include /* We don't need the header file here. */ typedef struct guestfs_h guestfs_h; @@ -44,10 +46,9 @@ read_symbol (void *lib, const char *symbol) dlerror (); /* Clear error indicator. */ symval = dlsym (lib, symbol); - if ((err = dlerror ()) != NULL) { - fprintf (stderr, "could not read symbol: %s: %s\n", symbol, err); - exit (EXIT_FAILURE); - } + if ((err = dlerror ()) != NULL) + error (EXIT_FAILURE, 0, + "could not read symbol: %s: %s", symbol, err); return symval; } @@ -60,35 +61,26 @@ main (int argc, char *argv[]) guestfs_close_t guestfs_close; guestfs_h *g; - if (access (LIBRARY, X_OK) == -1) { - fprintf (stderr, "test skipped because %s cannot be accessed: %m\n", - LIBRARY); - exit (77); - } + if (access (LIBRARY, X_OK) == -1) + error (77, errno, "test skipped because %s cannot be accessed", LIBRARY); lib = dlopen (LIBRARY, RTLD_LAZY); - if (lib == NULL) { - fprintf (stderr, "could not open %s: %s\n", LIBRARY, dlerror ()); - exit (EXIT_FAILURE); - } + if (lib == NULL) + error (EXIT_FAILURE, 0, "could not open %s: %s", LIBRARY, dlerror ()); guestfs_create = read_symbol (lib, "guestfs_create"); guestfs_get_program = read_symbol (lib, "guestfs_get_program"); guestfs_close = read_symbol (lib, "guestfs_close"); g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, "failed to create handle\n"); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); printf ("program = %s\n", guestfs_get_program (g)); guestfs_close (g); - if (dlclose (lib) != 0) { - fprintf (stderr, "could not close %s: %s\n", LIBRARY, dlerror ()); - exit (EXIT_FAILURE); - } + if (dlclose (lib) != 0) + error (EXIT_FAILURE, 0, "could not close %s: %s", LIBRARY, dlerror ()); exit (EXIT_SUCCESS); } diff --git a/tests/c-api/test-last-errno.c b/tests/c-api/test-last-errno.c index 135d67208..acd5cd97d 100644 --- a/tests/c-api/test-last-errno.c +++ b/tests/c-api/test-last-errno.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "guestfs.h" #include "guestfs-internal-frontend.h" @@ -40,10 +41,8 @@ main (int argc, char *argv[]) struct guestfs_stat *stat; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, "failed to create handle\n"); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); if (guestfs_add_drive_scratch (g, 524288000, -1) == -1) exit (EXIT_FAILURE); @@ -64,18 +63,14 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); r = guestfs_touch (g, "/test"); - if (r != -1) { - fprintf (stderr, - "guestfs_touch: expected error for read-only filesystem\n"); - exit (EXIT_FAILURE); - } + if (r != -1) + error (EXIT_FAILURE, 0, + "guestfs_touch: expected error for read-only filesystem"); err = guestfs_last_errno (g); - if (err != EROFS) { - fprintf (stderr, - "guestfs_touch: expected errno == EROFS, but got %d\n", err); - exit (EXIT_FAILURE); - } + if (err != EROFS) + error (EXIT_FAILURE, 0, + "guestfs_touch: expected errno == EROFS, but got %d", err); if (guestfs_umount (g, "/") == -1) exit (EXIT_FAILURE); @@ -85,35 +80,27 @@ main (int argc, char *argv[]) exit (EXIT_FAILURE); stat = guestfs_lstat (g, "/nosuchfile"); - if (stat != NULL) { - fprintf (stderr, - "guestfs_lstat: expected error for missing file\n"); - exit (EXIT_FAILURE); - } + if (stat != NULL) + error (EXIT_FAILURE, 0, + "guestfs_lstat: expected error for missing file"); err = guestfs_last_errno (g); - if (err != ENOENT) { - fprintf (stderr, - "guestfs_lstat: expected errno == ENOENT, but got %d\n", err); - exit (EXIT_FAILURE); - } + if (err != ENOENT) + error (EXIT_FAILURE, 0, + "guestfs_lstat: expected errno == ENOENT, but got %d", err); if (guestfs_touch (g, "/test") == -1) exit (EXIT_FAILURE); r = guestfs_mkdir (g, "/test"); - if (r != -1) { - fprintf (stderr, - "guestfs_mkdir: expected error for file which exists\n"); - exit (EXIT_FAILURE); - } + if (r != -1) + error (EXIT_FAILURE, 0, + "guestfs_mkdir: expected error for file which exists"); err = guestfs_last_errno (g); - if (err != EEXIST) { - fprintf (stderr, - "guestfs_mkdir: expected errno == EEXIST, but got %d\n", err); - exit (EXIT_FAILURE); - } + if (err != EEXIST) + error (EXIT_FAILURE, 0, + "guestfs_mkdir: expected errno == EEXIST, but got %d", err); guestfs_close (g); diff --git a/tests/c-api/test-private-data.c b/tests/c-api/test-private-data.c index 349d95927..9ebe7a892 100644 --- a/tests/c-api/test-private-data.c +++ b/tests/c-api/test-private-data.c @@ -25,6 +25,8 @@ #include #include #include +#include +#include #include "guestfs.h" #include "guestfs-internal-frontend.h" @@ -68,10 +70,8 @@ main (int argc, char *argv[]) size_t count; g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, "failed to create handle\n"); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); if (guestfs_set_event_callback (g, close_callback, GUESTFS_EVENT_CLOSE, 0, NULL) == -1) diff --git a/tests/c-api/test-user-cancel.c b/tests/c-api/test-user-cancel.c index 029c405e6..382368209 100644 --- a/tests/c-api/test-user-cancel.c +++ b/tests/c-api/test-user-cancel.c @@ -75,10 +75,8 @@ main (int argc, char *argv[]) srand48 (time (NULL)); g = guestfs_create (); - if (g == NULL) { - fprintf (stderr, "failed to create handle\n"); - exit (EXIT_FAILURE); - } + if (g == NULL) + error (EXIT_FAILURE, errno, "guestfs_create"); if (guestfs_add_drive_scratch (g, filesize, -1) == -1) exit (EXIT_FAILURE); @@ -115,10 +113,8 @@ main (int argc, char *argv[]) /* Create the test thread. */ r = pthread_create (&test_thread, NULL, start_test_thread, &data); - if (r != 0) { - fprintf (stderr, "pthread_create: %s\n", strerror (r)); - exit (EXIT_FAILURE); - } + if (r != 0) + error (EXIT_FAILURE, r, "pthread_create"); /* Do the upload. */ op_error = guestfs_upload (g, dev_fd, "/upload"); @@ -126,15 +122,11 @@ main (int argc, char *argv[]) /* Kill the test thread and clean up. */ r = pthread_cancel (test_thread); - if (r != 0) { - fprintf (stderr, "pthread_cancel: %s\n", strerror (r)); - exit (EXIT_FAILURE); - } + if (r != 0) + error (EXIT_FAILURE, r, "pthread_cancel"); r = pthread_join (test_thread, NULL); - if (r != 0) { - fprintf (stderr, "pthread_join: %s\n", strerror (r)); - exit (EXIT_FAILURE); - } + if (r != 0) + error (EXIT_FAILURE, r, "pthread_join"); close (fds[0]); close (fds[1]); @@ -179,10 +171,8 @@ main (int argc, char *argv[]) /* Create the test thread. */ r = pthread_create (&test_thread, NULL, start_test_thread, &data); - if (r != 0) { - fprintf (stderr, "pthread_create: %s\n", strerror (r)); - exit (EXIT_FAILURE); - } + if (r != 0) + error (EXIT_FAILURE, r, "pthread_create"); /* Do the download. */ op_error = guestfs_download (g, "/download", dev_fd); @@ -190,15 +180,11 @@ main (int argc, char *argv[]) /* Kill the test thread and clean up. */ r = pthread_cancel (test_thread); - if (r != 0) { - fprintf (stderr, "pthread_cancel: %s\n", strerror (r)); - exit (EXIT_FAILURE); - } + if (r != 0) + error (EXIT_FAILURE, r, "pthread_cancel"); r = pthread_join (test_thread, NULL); - if (r != 0) { - fprintf (stderr, "pthread_join: %s\n", strerror (r)); - exit (EXIT_FAILURE); - } + if (r != 0) + error (EXIT_FAILURE, r, "pthread_join"); close (fds[0]); close (fds[1]); diff --git a/tests/c-api/tests-main.c b/tests/c-api/tests-main.c index dd5c2b6dc..e3af651dd 100644 --- a/tests/c-api/tests-main.c +++ b/tests/c-api/tests-main.c @@ -345,11 +345,9 @@ match_re (const char *str, const char *pattern) int vec[30], r; re = pcre_compile (pattern, 0, &err, &offset, NULL); - if (re == NULL) { - fprintf (stderr, "tests: cannot compile regular expression '%s': %s\n", - pattern, err); - exit (EXIT_FAILURE); - } + if (re == NULL) + error (EXIT_FAILURE, 0, + "cannot compile regular expression '%s': %s", pattern, err); r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]); pcre_free (re); @@ -370,13 +368,12 @@ substitute_srcdir (const char *path) const char *srcdir; srcdir = getenv ("srcdir"); - if (!srcdir) { - fprintf (stderr, "tests: environment variable $srcdir is not defined.\n" - "Normally it is defined by automake. If you are running the\n" - "tests directly, set $srcdir to point to the source tests/c-api\n" - "directory.\n"); - exit (EXIT_FAILURE); - } + if (!srcdir) + error (EXIT_FAILURE, 0, + "environment variable $srcdir is not defined.\n" + "Normally it is defined by automake. If you are running the\n" + "tests directly, set $srcdir to point to the source tests/c-api\n" + "directory."); if (asprintf (&ret, "%s%s", srcdir, path + 7) == -1) error (EXIT_FAILURE, errno, "asprintf"); @@ -485,17 +482,12 @@ check_cross_appliance (guestfs_h *g) struct guestfs_utsname host_utsname; r = uname (&host); - if (r == -1) { - fprintf (stderr, "call to uname failed\n"); - exit (EXIT_FAILURE); - } + if (r == -1) + error (EXIT_FAILURE, errno, "uname"); appliance = guestfs_utsname (g); - if (appliance == NULL) { - fprintf (stderr, "call to guestfs_utsname failed: %d, %s\n", - guestfs_last_errno (g), guestfs_last_error (g)); + if (appliance == NULL) exit (EXIT_FAILURE); - } host_utsname.uts_sysname = host.sysname; host_utsname.uts_release = host.release; diff --git a/tests/events/test-libvirt-auth-callbacks.c b/tests/events/test-libvirt-auth-callbacks.c index 86e26f8f6..a18ab2429 100644 --- a/tests/events/test-libvirt-auth-callbacks.c +++ b/tests/events/test-libvirt-auth-callbacks.c @@ -55,22 +55,17 @@ main (int argc, char *argv[]) * supports the new test-driver auth feature. */ virGetVersion (&ver, NULL, NULL); - if (ver < 1002001) { - fprintf (stderr, "%s: test skipped because libvirt is too old (%lu)\n", - argv[0], ver); - exit (77); - } + if (ver < 1002001) + error (77, 0, "test skipped because libvirt is too old (%lu)", ver); /* $srcdir must have been passed (by automake). */ srcdir = getenv ("srcdir"); - if (!srcdir) { - fprintf (stderr, - "%s: environment variable $srcdir is not defined.\n" - "Normally it is defined by automake. If you are running the\n" - "tests directly, set $srcdir to point to the source tests/events\n" - "directory.\n", argv[0]); - exit (EXIT_FAILURE); - } + if (!srcdir) + error (EXIT_FAILURE, 0, + "environment variable $srcdir is not defined.\n" + "Normally it is defined by automake. If you are running the\n" + "tests directly, set $srcdir to point to the source tests/events\n" + "directory."); cwd = getcwd (NULL, 0); if (cwd == NULL) @@ -107,7 +102,7 @@ do_test (const char *prog, const char *libvirt_uri, g = guestfs_create (); if (!g) - exit (EXIT_FAILURE); + error (EXIT_FAILURE, errno, "guestfs_create"); r = guestfs_set_libvirt_supported_credentials (g, (char **) creds); if (r == -1) @@ -123,13 +118,11 @@ do_test (const char *prog, const char *libvirt_uri, GUESTFS_ADD_DOMAIN_LIBVIRTURI, libvirt_uri, GUESTFS_ADD_DOMAIN_READONLY, 1, -1); - if (r != expected) { - fprintf (stderr, - "%s: test failed: u=%s p=%s: got %d expected %d\n", - prog, auth_data->username, auth_data->password ? : "(none)", - r, expected); - exit (EXIT_FAILURE); - } + if (r != expected) + error (EXIT_FAILURE, 0, + "test failed: u=%s p=%s: got %d expected %d", + auth_data->username, auth_data->password ? : "(none)", + r, expected); guestfs_close (g); } @@ -161,18 +154,18 @@ auth_callback (guestfs_h *g, void *opaque, } else if (STREQ (creds[i], "passphrase") || STREQ (creds[i], "noechoprompt")) { - if (!auth_data->password) { - fprintf (stderr, "test failed: libvirt asked for a password, but auth_data->password == NULL\n"); - exit (EXIT_FAILURE); - } + if (!auth_data->password) + error (EXIT_FAILURE, 0, + "test failed: libvirt asked for a password, but auth_data->password == NULL"); reply = auth_data->password; len = strlen (reply); } else { - fprintf (stderr, "test failed: libvirt asked for '%s' which is not in creds list\n(This is probably a libvirt bug)\n", - creds[i]); - exit (EXIT_FAILURE); + error (EXIT_FAILURE, 0, + "test failed: libvirt asked for '%s' which is not in creds list\n(This is probably a libvirt bug)", + creds[i]); + abort (); /* keeps GCC happy since error(3) is not marked noreturn */ } r = guestfs_set_libvirt_requested_credential (g, i, diff --git a/tests/qemu/qemu-boot.c b/tests/qemu/qemu-boot.c index fe7ce9592..61d2ff02e 100644 --- a/tests/qemu/qemu-boot.c +++ b/tests/qemu/qemu-boot.c @@ -122,11 +122,10 @@ main (int argc, char *argv[]) log_file_size += 64; } } - else { - fprintf (stderr, "%s: unknown long option: %s (%d)\n", - guestfs_int_program_name, long_options[option_index].name, option_index); - exit (EXIT_FAILURE); - } + else + error (EXIT_FAILURE, 0, + "unknown long option: %s (%d)", + long_options[option_index].name, option_index); break; case 'i': @@ -134,18 +133,13 @@ main (int argc, char *argv[]) break; case 'n': - if (sscanf (optarg, "%zu", &n) != 1 || n == 0) { - fprintf (stderr, "%s: -n option not numeric and greater than 0\n", - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (sscanf (optarg, "%zu", &n) != 1 || n == 0) + error (EXIT_FAILURE, 0, "-n option not numeric and greater than 0"); break; case 'P': - if (sscanf (optarg, "%zu", &P) != 1) { - fprintf (stderr, "%s: -P option not numeric\n", guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (sscanf (optarg, "%zu", &P) != 1) + error (EXIT_FAILURE, 0, "-P option not numeric"); break; case 'v': @@ -164,18 +158,13 @@ main (int argc, char *argv[]) } } - if (n == 0) { - fprintf (stderr, - "%s: must specify number of processes to run (-n option)\n", - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (n == 0) + error (EXIT_FAILURE, 0, + "must specify number of processes to run (-n option)"); - if (optind != argc) { - fprintf (stderr, "%s: extra arguments found on the command line\n", - guestfs_int_program_name); - exit (EXIT_FAILURE); - } + if (optind != argc) + error (EXIT_FAILURE, 0, + "extra arguments found on the command line"); /* Calculate the number of threads to use. */ if (P > 0) @@ -205,11 +194,8 @@ run_test (size_t P) for (i = 0; i < P; ++i) { thread_data[i].thread_num = i; err = pthread_create (&threads[i], NULL, start_thread, &thread_data[i]); - if (err != 0) { - fprintf (stderr, "%s: pthread_create[%zu]: %s\n", - guestfs_int_program_name, i, strerror (err)); - exit (EXIT_FAILURE); - } + if (err != 0) + error (EXIT_FAILURE, err, "pthread_create[%zu]\n", i); } /* Wait for the threads to exit. */ diff --git a/tests/regressions/rhbz790721.c b/tests/regressions/rhbz790721.c index ae44e68b2..e2178e5e6 100644 --- a/tests/regressions/rhbz790721.c +++ b/tests/regressions/rhbz790721.c @@ -90,10 +90,8 @@ main (int argc, char *argv[]) for (i = 0; i < NR_THREADS; ++i) { data[i] = i; r = pthread_create (&thread[i], NULL, start_thread, &data[i]); - if (r != 0) { - fprintf (stderr, "pthread_create: %s\n", strerror (r)); - exit (EXIT_FAILURE); - } + if (r != 0) + error (EXIT_FAILURE, r, "pthread_create"); } /* Wait for the threads to exit. */ @@ -103,10 +101,8 @@ main (int argc, char *argv[]) int *ret; r = pthread_join (thread[i], (void **) &ret); - if (r != 0) { - fprintf (stderr, "pthread_join: %s\n", strerror (r)); - exit (EXIT_FAILURE); - } + if (r != 0) + error (EXIT_FAILURE, r, "pthread_join"); if (*ret == -1) errors++; }