]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Add virGettextInitialize, convert the code
authorCole Robinson <crobinso@redhat.com>
Tue, 12 Apr 2016 22:29:52 +0000 (18:29 -0400)
committerCole Robinson <crobinso@redhat.com>
Thu, 14 Apr 2016 17:22:40 +0000 (13:22 -0400)
Take setlocale/gettext error handling pattern from tools/virsh-*
and use it for all standalone binaries via a new shared
virGettextInitialize routine. The virsh* pattern differed slightly
from other callers. All users now consistently:

* Ignore setlocale errors. virsh has done this forever, presumably for
  good reason. This has been partially responsible for some bug reports:

  https://bugzilla.redhat.com/show_bug.cgi?id=1312688
  https://bugzilla.redhat.com/show_bug.cgi?id=1026514
  https://bugzilla.redhat.com/show_bug.cgi?id=1016158

* Report the failed function name
* Report strerror

19 files changed:
cfg.mk
daemon/libvirtd.c
src/Makefile.am
src/libvirt_private.syms
src/locking/lock_daemon.c
src/locking/sanlock_helper.c
src/logging/log_daemon.c
src/lxc/lxc_controller.c
src/network/leaseshelper.c
src/security/virt-aa-helper.c
src/storage/parthelper.c
src/util/iohelper.c
src/util/virgettext.c [new file with mode: 0644]
src/util/virgettext.h [new file with mode: 0644]
tools/virsh.c
tools/virt-admin.c
tools/virt-host-validate.c
tools/virt-login-shell.c
tools/vsh.c

diff --git a/cfg.mk b/cfg.mk
index cd3b515ce0d34f611d72dc1770ae3a6af6c90f68..5e27726821719ddfe69e676af9227e00c3d5d665 100644 (file)
--- a/cfg.mk
+++ b/cfg.mk
@@ -1037,6 +1037,15 @@ sc_prohibit_verbose_strcat:
        halt='Use strcat(a, b) instead of strncat(a, b, strlen(b))' \
          $(_sc_search_regexp)
 
+# Ensure that each .c file containing a "main" function also
+# calls virGettextInitialize
+sc_gettext_init:
+       @require='virGettextInitialize *\('                                     \
+       in_vc_files='\.c$$'                                             \
+       containing='\<main *('                                          \
+       halt='the above files do not call virGettextInitialize'         \
+         $(_sc_search_regexp)
+
 # We don't use this feature of maint.mk.
 prev_version_file = /dev/null
 
@@ -1133,7 +1142,9 @@ _test1=shunloadtest|virnettlscontexttest|virnettlssessiontest|vircgroupmock
 exclude_file_name_regexp--sc_avoid_write = \
   ^(src/($(_src1))|daemon/libvirtd|tools/virsh-console|tests/($(_test1)))\.c$$
 
-exclude_file_name_regexp--sc_bindtextdomain = ^(tests|examples)/
+exclude_file_name_regexp--sc_bindtextdomain = .*
+
+exclude_file_name_regexp--sc_gettext_init = ^(tests|examples)/
 
 exclude_file_name_regexp--sc_copyright_usage = \
   ^COPYING(|\.LESSER)$$
index 92b40801cd68d25c068eb4f973de486dec4e8749..7ec02ad8ca209a68ac3399cb19f4689b03e625b8 100644 (file)
@@ -30,7 +30,6 @@
 #include <getopt.h>
 #include <stdlib.h>
 #include <grp.h>
-#include <locale.h>
 
 #include "libvirt_internal.h"
 #include "virerror.h"
@@ -58,6 +57,7 @@
 #include "locking/lock_manager.h"
 #include "viraccessmanager.h"
 #include "virutil.h"
+#include "virgettext.h"
 
 #ifdef WITH_DRIVER_MODULES
 # include "driver.h"
@@ -1181,9 +1181,7 @@ int main(int argc, char **argv) {
         {0, 0, 0, 0}
     };
 
-    if (setlocale(LC_ALL, "") == NULL ||
-        bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
-        textdomain(PACKAGE) == NULL ||
+    if (virGettextInitialize() < 0 ||
         virInitialize() < 0) {
         fprintf(stderr, _("%s: initialization failed\n"), argv[0]);
         exit(EXIT_FAILURE);
index f5e57c03c03e948ee934a498af1db65151411024..ad1c0c3466377a66cba3996eb5a39b59bfd06067 100644 (file)
@@ -114,6 +114,7 @@ UTIL_SOURCES =                                                      \
                util/virfile.c util/virfile.h                   \
                util/virfirewall.c util/virfirewall.h           \
                util/virfirewallpriv.h                          \
+               util/virgettext.c util/virgettext.h             \
                util/virgic.c util/virgic.h                     \
                util/virhash.c util/virhash.h                   \
                util/virhashcode.c util/virhashcode.h           \
@@ -2329,6 +2330,7 @@ libvirt_setuid_rpc_client_la_SOURCES =            \
                util/virevent.c                 \
                util/vireventpoll.c             \
                util/virfile.c                  \
+               util/virgettext.c               \
                util/virhash.c                  \
                util/virhashcode.c              \
                util/virjson.c                  \
index a9025f50c4ee9cdc212e6581d45878d8594dd1c4..abeba6c0fd22d58b2d306f6235a03ff33578dfaa 100644 (file)
@@ -1522,6 +1522,10 @@ virFirewallStartRollback;
 virFirewallStartTransaction;
 
 
+# util/virgettext.h
+virGettextInitialize;
+
+
 # util/virgic.h
 virGICVersionTypeFromString;
 virGICVersionTypeToString;
index 973e6918c9fe16b04f8b88276df6612e5d95f362..bfdcfc6e1011e336a931492f91b7db694dd70173 100644 (file)
@@ -28,7 +28,6 @@
 #include <sys/stat.h>
 #include <getopt.h>
 #include <stdlib.h>
-#include <locale.h>
 
 
 #include "lock_daemon.h"
@@ -47,6 +46,7 @@
 #include "virhash.h"
 #include "viruuid.h"
 #include "virstring.h"
+#include "virgettext.h"
 
 #include "locking/lock_daemon_dispatch.h"
 #include "locking/lock_protocol.h"
@@ -1179,9 +1179,7 @@ int main(int argc, char **argv) {
 
     privileged = geteuid() == 0;
 
-    if (setlocale(LC_ALL, "") == NULL ||
-        bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
-        textdomain(PACKAGE) == NULL ||
+    if (virGettextInitialize() < 0 ||
         virThreadInitialize() < 0 ||
         virErrorInitialize() < 0) {
         fprintf(stderr, _("%s: initialization failed\n"), argv[0]);
index d8d294f3cc4a6fdb1201fc03f6bdd518712afd5e..57e1cfb0317c955a80fbaae791aa943dc44c4557 100644 (file)
@@ -1,13 +1,12 @@
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
-#include <locale.h>
 
-#include "configmake.h"
 #include "internal.h"
 #include "virconf.h"
 #include "viralloc.h"
 #include "domain_conf.h"
+#include "virgettext.h"
 
 
 static int
@@ -70,12 +69,8 @@ main(int argc, char **argv)
         .cb = authCallback,
     };
 
-    if (setlocale(LC_ALL, "") == NULL ||
-        bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
-        textdomain(PACKAGE) == NULL) {
-        fprintf(stderr, _("%s: initialization failed\n"), argv[0]);
+    if (virGettextInitialize() < 0)
         exit(EXIT_FAILURE);
-    }
 
     if (getArgs(argc, argv, &uri, &uuid, &action) < 0)
         goto cleanup;
index f674cbd51402f1fd0a9fca5503deb76a9014cc9a..70339afa206f6e4aef2f4825d2f424169055a9f9 100644 (file)
@@ -28,7 +28,6 @@
 #include <sys/stat.h>
 #include <getopt.h>
 #include <stdlib.h>
-#include <locale.h>
 
 
 #include "log_daemon.h"
@@ -46,6 +45,7 @@
 #include "virhash.h"
 #include "viruuid.h"
 #include "virstring.h"
+#include "virgettext.h"
 
 #include "log_daemon_dispatch.h"
 #include "log_protocol.h"
@@ -936,9 +936,7 @@ int main(int argc, char **argv) {
 
     privileged = geteuid() == 0;
 
-    if (setlocale(LC_ALL, "") == NULL ||
-        bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
-        textdomain(PACKAGE) == NULL ||
+    if (virGettextInitialize() < 0 ||
         virThreadInitialize() < 0 ||
         virErrorInitialize() < 0) {
         fprintf(stderr, _("%s: initialization failed\n"), argv[0]);
index 8b5ec4c840c6a9ab42c9abf459b0612f1e427ac2..73e57e30ad48a8a87f14a0286c1c7ab647f8262e 100644 (file)
@@ -36,7 +36,6 @@
 #include <signal.h>
 #include <getopt.h>
 #include <sys/mount.h>
-#include <locale.h>
 #include <grp.h>
 #include <sys/stat.h>
 #include <time.h>
@@ -66,6 +65,7 @@
 #include "virdbus.h"
 #include "rpc/virnetdaemon.h"
 #include "virstring.h"
+#include "virgettext.h"
 
 #define VIR_FROM_THIS VIR_FROM_LXC
 
@@ -2505,9 +2505,7 @@ int main(int argc, char *argv[])
     for (i = 0; i < VIR_LXC_DOMAIN_NAMESPACE_LAST; i++)
         ns_fd[i] = -1;
 
-    if (setlocale(LC_ALL, "") == NULL ||
-        bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
-        textdomain(PACKAGE) == NULL ||
+    if (virGettextInitialize() < 0 ||
         virThreadInitialize() < 0 ||
         virErrorInitialize() < 0) {
         fprintf(stderr, _("%s: initialization failed\n"), argv[0]);
index 097cd11562da655823b0eb1a34ac23a87b16a913..16f6eb87b53ca30887eccb52a296103e98b1d1b3 100644 (file)
@@ -25,7 +25,6 @@
 
 #include <config.h>
 
-#include <locale.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -38,6 +37,7 @@
 #include "virjson.h"
 #include "virlease.h"
 #include "configmake.h"
+#include "virgettext.h"
 
 #define VIR_FROM_THIS VIR_FROM_NETWORK
 
@@ -115,14 +115,8 @@ main(int argc, char **argv)
 
     program_name = argv[0];
 
-    if (setlocale(LC_ALL, "") == NULL ||
-        bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
-        textdomain(PACKAGE) == NULL) {
-        fprintf(stderr, _("%s: initialization failed\n"), program_name);
-        exit(EXIT_FAILURE);
-    }
-
-    if (virThreadInitialize() < 0 ||
+    if (virGettextInitialize() < 0 ||
+        virThreadInitialize() < 0 ||
         virErrorInitialize() < 0) {
         fprintf(stderr, _("%s: initialization failed\n"), program_name);
         exit(EXIT_FAILURE);
index 50d2a081660669470864858551451b21f4357c9d..5db9c02abb03e67df2bfc4f2e0d00057ac0467fc 100644 (file)
@@ -35,7 +35,6 @@
 #include <fcntl.h>
 #include <getopt.h>
 #include <sys/utsname.h>
-#include <locale.h>
 
 #include "internal.h"
 #include "virbuffer.h"
@@ -54,6 +53,7 @@
 #include "configmake.h"
 #include "virrandom.h"
 #include "virstring.h"
+#include "virgettext.h"
 
 #include "storage/storage_driver.h"
 
@@ -1298,14 +1298,8 @@ main(int argc, char **argv)
     char *profile = NULL;
     char *include_file = NULL;
 
-    if (setlocale(LC_ALL, "") == NULL ||
-        bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
-        textdomain(PACKAGE) == NULL) {
-        fprintf(stderr, _("%s: initialization failed\n"), argv[0]);
-        exit(EXIT_FAILURE);
-    }
-
-    if (virThreadInitialize() < 0 ||
+    if (virGettextInitialize() < 0 ||
+        virThreadInitialize() < 0 ||
         virErrorInitialize() < 0) {
         fprintf(stderr, _("%s: initialization failed\n"), argv[0]);
         exit(EXIT_FAILURE);
index d1df068f233f3e2f84a8c69a436558bf01c0ec3a..6695f23896c9fca65c28ce71e2654368be26ec84 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
-#include <locale.h>
 
 #include "virutil.h"
 #include "virfile.h"
 #include "c-ctype.h"
-#include "configmake.h"
 #include "virstring.h"
+#include "virgettext.h"
 
 /* we don't need to include the full internal.h just for this */
 #define STREQ(a, b) (strcmp(a, b) == 0)
@@ -72,12 +71,8 @@ int main(int argc, char **argv)
     const char *partsep;
     bool devmap_nopartsep = false;
 
-    if (setlocale(LC_ALL, "") == NULL ||
-        bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
-        textdomain(PACKAGE) == NULL) {
-        fprintf(stderr, _("%s: initialization failed\n"), argv[0]);
+    if (virGettextInitialize() < 0)
         exit(EXIT_FAILURE);
-    }
 
     if (argc == 3 && STREQ(argv[2], "-g")) {
         cmd = DISK_GEOMETRY;
index 8a3c3778a74262bca0c27801f242ff7d3ac42c9a..275f993abd4cf2bf57424a7b915a180713493374 100644 (file)
@@ -27,7 +27,6 @@
 
 #include <config.h>
 
-#include <locale.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdio.h>
@@ -38,9 +37,9 @@
 #include "virfile.h"
 #include "viralloc.h"
 #include "virerror.h"
-#include "configmake.h"
 #include "virrandom.h"
 #include "virstring.h"
+#include "virgettext.h"
 
 #define VIR_FROM_THIS VIR_FROM_STORAGE
 
@@ -230,14 +229,8 @@ main(int argc, char **argv)
 
     program_name = argv[0];
 
-    if (setlocale(LC_ALL, "") == NULL ||
-        bindtextdomain(PACKAGE, LOCALEDIR) == NULL ||
-        textdomain(PACKAGE) == NULL) {
-        fprintf(stderr, _("%s: initialization failed\n"), program_name);
-        exit(EXIT_FAILURE);
-    }
-
-    if (virThreadInitialize() < 0 ||
+    if (virGettextInitialize() < 0 ||
+        virThreadInitialize() < 0 ||
         virErrorInitialize() < 0) {
         fprintf(stderr, _("%s: initialization failed\n"), program_name);
         exit(EXIT_FAILURE);
diff --git a/src/util/virgettext.c b/src/util/virgettext.c
new file mode 100644 (file)
index 0000000..c0135b4
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * virgettext.c: gettext helper routines
+ *
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+
+#include <locale.h>
+#include <stdio.h>
+
+#include "configmake.h"
+#include "internal.h"
+#include "virgettext.h"
+
+
+/**
+ * virGettextInitialize:
+ *
+ * Initialize standard gettext setup
+ * Returns -1 on fatal error
+ */
+int
+virGettextInitialize(void)
+{
+    if (!setlocale(LC_ALL, "")) {
+        perror("setlocale");
+        /* failure to setup locale is not fatal */
+    }
+
+    if (!bindtextdomain(PACKAGE, LOCALEDIR)) {
+        perror("bindtextdomain");
+        return -1;
+    }
+
+    if (!textdomain(PACKAGE)) {
+        perror("textdomain");
+        return -1;
+    }
+
+    return 0;
+}
diff --git a/src/util/virgettext.h b/src/util/virgettext.h
new file mode 100644 (file)
index 0000000..4584d83
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * virgettext.h: gettext helper routines
+ *
+ * Copyright (C) 2016 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ */
+#ifndef __VIR_GETTEXT_H__
+# define __VIR_GETTEXT_H__
+
+int virGettextInitialize(void);
+
+#endif /* __VIR_GETTEXT_H__ */
index 5a61189bc2958a1dc06698a61397670db6c5f9d9..0d8ec5c80c7d6938005b56ee9bd8e043d60fc2cb 100644 (file)
@@ -34,7 +34,6 @@
 #include <getopt.h>
 #include <sys/time.h>
 #include <fcntl.h>
-#include <locale.h>
 #include <time.h>
 #include <limits.h>
 #include <sys/stat.h>
 #include <libvirt/libvirt-qemu.h>
 #include <libvirt/libvirt-lxc.h>
 #include "virfile.h"
-#include "configmake.h"
 #include "virthread.h"
 #include "vircommand.h"
 #include "conf/domain_conf.h"
 #include "virtypedparam.h"
 #include "virstring.h"
+#include "virgettext.h"
 
 #include "virsh-console.h"
 #include "virsh-domain.h"
@@ -936,18 +935,8 @@ main(int argc, char **argv)
         progname++;
     ctl->progname = progname;
 
-    if (!setlocale(LC_ALL, "")) {
-        perror("setlocale");
-        /* failure to setup locale is not fatal */
-    }
-    if (!bindtextdomain(PACKAGE, LOCALEDIR)) {
-        perror("bindtextdomain");
-        return EXIT_FAILURE;
-    }
-    if (!textdomain(PACKAGE)) {
-        perror("textdomain");
+    if (virGettextInitialize() < 0)
         return EXIT_FAILURE;
-    }
 
     if (isatty(STDIN_FILENO)) {
         ctl->istty = true;
index f0a49a3895bded450c071d29d98198892e226c9a..195088bdd885ab4e12c49b5e81f4242dcf37ab5c 100644 (file)
 
 #include <errno.h>
 #include <getopt.h>
-#include <locale.h>
 
 #if WITH_READLINE
 # include <readline/readline.h>
 # include <readline/history.h>
 #endif
 
-#include "configmake.h"
 #include "internal.h"
 #include "viralloc.h"
 #include "virerror.h"
 #include "virfile.h"
 #include "virstring.h"
 #include "virthread.h"
+#include "virgettext.h"
 
 /* Gnulib doesn't guarantee SA_SIGINFO support.  */
 #ifndef SA_SIGINFO
@@ -689,18 +688,8 @@ main(int argc, char **argv)
         progname++;
     ctl->progname = progname;
 
-    if (!setlocale(LC_ALL, "")) {
-        perror("setlocale");
-        /* failure to setup locale is not fatal */
-    }
-    if (!bindtextdomain(PACKAGE, LOCALEDIR)) {
-        perror("bindtextdomain");
-        return EXIT_FAILURE;
-    }
-    if (!textdomain(PACKAGE)) {
-        perror("textdomain");
+    if (virGettextInitialize() < 0)
         return EXIT_FAILURE;
-    }
 
     if (isatty(STDIN_FILENO)) {
         ctl->istty = true;
index a8c2075fde96707d04c0cb368e37f434791ddd48..5b7fe9b4a15149cb6f118c82b6afebf0554b7f3a 100644 (file)
 #include <stdlib.h>
 #include <gettext.h>
 #include <getopt.h>
-#include <locale.h>
 
 #include "internal.h"
-#include "configmake.h"
+#include "virgettext.h"
 
 #include "virt-host-validate-common.h"
 #if WITH_QEMU
@@ -80,18 +79,8 @@ main(int argc, char **argv)
     bool quiet = false;
     bool usedHvname = false;
 
-    if (!setlocale(LC_ALL, "")) {
-        perror("setlocale");
-        /* failure to setup locale is not fatal */
-    }
-    if (!bindtextdomain(PACKAGE, LOCALEDIR)) {
-        perror("bindtextdomain");
-        return EXIT_FAILURE;
-    }
-    if (!textdomain(PACKAGE)) {
-        perror("textdomain");
+    if (virGettextInitialize() < 0)
         return EXIT_FAILURE;
-    }
 
     while ((c = getopt_long(argc, argv, "hvq", argOptions, NULL)) != -1) {
         switch (c) {
index ec759dcb0d901565f2af8f8e8b3b9e1fd6492862..8f872271c0b495fc127bf2e02c6f3f349c10e3fd 100644 (file)
@@ -24,7 +24,6 @@
 #include <errno.h>
 #include <fnmatch.h>
 #include <getopt.h>
-#include <locale.h>
 #include <signal.h>
 #include <stdarg.h>
 #include <stdio.h>
@@ -41,6 +40,7 @@
 #include "virstring.h"
 #include "viralloc.h"
 #include "vircommand.h"
+#include "virgettext.h"
 #define VIR_FROM_THIS VIR_FROM_NONE
 
 static const char *conf_file = SYSCONFDIR "/libvirt/virt-login-shell.conf";
@@ -207,18 +207,8 @@ main(int argc, char **argv)
     virSetErrorLogPriorityFunc(NULL);
 
     progname = argv[0];
-    if (!setlocale(LC_ALL, "")) {
-        perror("setlocale");
-        /* failure to setup locale is not fatal */
-    }
-    if (!bindtextdomain(PACKAGE, LOCALEDIR)) {
-        perror("bindtextdomain");
-        return ret;
-    }
-    if (!textdomain(PACKAGE)) {
-        perror("textdomain");
+    if (virGettextInitialize() < 0)
         return ret;
-    }
 
     while ((arg = getopt_long(argc, argv, "hV", opt, &longindex)) != -1) {
         switch (arg) {
index 56591107604965f934d3050de919ec04d644cf0a..a80e85103a84d4b5af13c4e63dee004666924ccf 100644 (file)
@@ -35,7 +35,6 @@
 #include <sys/time.h>
 #include "c-ctype.h"
 #include <fcntl.h>
-#include <locale.h>
 #include <time.h>
 #include <limits.h>
 #include <sys/stat.h>
@@ -54,7 +53,6 @@
 #include <libvirt/libvirt-qemu.h>
 #include <libvirt/libvirt-lxc.h>
 #include "virfile.h"
-#include "configmake.h"
 #include "virthread.h"
 #include "vircommand.h"
 #include "conf/domain_conf.h"