From patchwork Fri Feb 24 16:12:52 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wei Liu X-Patchwork-Id: 9590685 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 476E0604A2 for ; Fri, 24 Feb 2017 16:15:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 546AA286E0 for ; Fri, 24 Feb 2017 16:15:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 495C22871D; Fri, 24 Feb 2017 16:15:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9DE39286E0 for ; Fri, 24 Feb 2017 16:15:47 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1chIUk-0004Yv-ME; Fri, 24 Feb 2017 16:13:22 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1chIUi-0004X7-PV for xen-devel@lists.xenproject.org; Fri, 24 Feb 2017 16:13:20 +0000 Received: from [193.109.254.147] by server-2.bemta-6.messagelabs.com id EB/D5-19731-0AB50B85; Fri, 24 Feb 2017 16:13:20 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFprAIsWRWlGSWpSXmKPExsXitHRDpO786A0 RBpsnKVp83zKZyYHR4/CHKywBjFGsmXlJ+RUJrBkH/19jL5gfUzHl7z3WBsbzll2MHBwSAv4S h45qdzFycrAJKEv87OxlA7FFBPQkmg48ZwSxmQW8JH6s6GEHsYUF7CWab54Bq2ERUJVYvPgDm M0rYCFx9uB3FhBbQkBeYlfbRVYQm1PAUmLTg69gvUJANS++b2aEsBUkOqYfY4LoFZQ4OfMJC8 QuCYmDL14wT2DknYUkNQtJagEj0ypGjeLUorLUIl1DI72kosz0jJLcxMwcXUMDM73c1OLixPT UnMSkYr3k/NxNjMDQYQCCHYyXNwYcYpTkYFIS5Q0N3hAhxJeUn1KZkVicEV9UmpNafIhRhoND SYLXMwooJ1iUmp5akZaZAwximLQEB4+SCG9+JFCat7ggMbc4Mx0idYpRl2PO7N1vmIRY8vLzU qXEeQNBZgiAFGWU5sGNgEXUJUZZKWFeRqCjhHgKUotyM0tQ5V8xinMwKgnzdoJM4cnMK4Hb9A roCCagIyyd14IcUZKIkJJqYBSUCNr4wDOwtTHcfRfX1mWx3S+M3ofeXMW77naOncWKsndWLj5 nmCyEBevseOV3hvK9mbS5oXZTWW/dSXZfjQ6+9OKG4KbXRcvK/sxYEb86JP9nNX/KsiQ7Ac7G PxfbEmNPhjl8VW1uVXXOTgm9cPykRMOypZv/hn8zCbzeu+v/2zs3Lr0yVmIpzkg01GIuKk4EA GM84RSjAgAA X-Env-Sender: prvs=22177af8c=wei.liu2@citrix.com X-Msg-Ref: server-2.tower-27.messagelabs.com!1487952797!36309652!1 X-Originating-IP: [66.165.176.89] X-SpamReason: No, hits=0.0 required=7.0 tests=sa_preprocessor: VHJ1c3RlZCBJUDogNjYuMTY1LjE3Ni44OSA9PiAyMDMwMDc=\n, received_headers: No Received headers X-StarScan-Received: X-StarScan-Version: 9.2.3; banners=-,-,- X-VirusChecked: Checked Received: (qmail 37384 invoked from network); 24 Feb 2017 16:13:19 -0000 Received: from smtp.citrix.com (HELO SMTP.CITRIX.COM) (66.165.176.89) by server-2.tower-27.messagelabs.com with RC4-SHA encrypted SMTP; 24 Feb 2017 16:13:19 -0000 X-IronPort-AV: E=Sophos;i="5.35,201,1484006400"; d="scan'208";a="409524325" From: Wei Liu To: Xen-devel Date: Fri, 24 Feb 2017 16:12:52 +0000 Message-ID: <20170224161314.22154-8-wei.liu2@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20170224161314.22154-1-wei.liu2@citrix.com> References: <20170224161314.22154-1-wei.liu2@citrix.com> MIME-Version: 1.0 Cc: Wei Liu , Ian Jackson Subject: [Xen-devel] [PATCH 07/29] xl: lift a bunch of macros to xl_utils.h X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP We're going to split xl_cmdimpl.c into multiple files. Lift the commonly used macros to xl_utils.h. Signed-off-by: Wei Liu Acked-by: Ian Jackson --- tools/xl/xl_cmdimpl.c | 104 +--------------------------------------- tools/xl/xl_utils.h | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 103 deletions(-) create mode 100644 tools/xl/xl_utils.h diff --git a/tools/xl/xl_cmdimpl.c b/tools/xl/xl_cmdimpl.c index 1d7cf8ffa8..d9d947827d 100644 --- a/tools/xl/xl_cmdimpl.c +++ b/tools/xl/xl_cmdimpl.c @@ -39,42 +39,7 @@ #include #include #include "xl.h" - -/* For calls which return an errno on failure */ -#define CHK_ERRNOVAL( call ) ({ \ - int chk_errnoval = (call); \ - if (chk_errnoval < 0) \ - abort(); \ - else if (chk_errnoval > 0) { \ - fprintf(stderr,"xl: fatal error: %s:%d: %s: %s\n", \ - __FILE__,__LINE__, strerror(chk_errnoval), #call); \ - exit(EXIT_FAILURE); \ - } \ - }) - -/* For calls which return -1 and set errno on failure */ -#define CHK_SYSCALL( call ) ({ \ - if ((call) == -1) { \ - fprintf(stderr,"xl: fatal error: %s:%d: %s: %s\n", \ - __FILE__,__LINE__, strerror(errno), #call); \ - exit(EXIT_FAILURE); \ - } \ - }) - -#define MUST( call ) ({ \ - int must_rc = (call); \ - if (must_rc < 0) { \ - fprintf(stderr,"xl: fatal error: %s:%d, rc=%d: %s\n", \ - __FILE__,__LINE__, must_rc, #call); \ - exit(EXIT_FAILURE); \ - } \ - }) - -#define STR_HAS_PREFIX( a, b ) \ - ( strncmp(a, b, strlen(b)) == 0 ) -#define STR_SKIP_PREFIX( a, b ) \ - ( STR_HAS_PREFIX(a, b) ? ((a) += strlen(b), 1) : 0 ) - +#include "xl_utils.h" int logfile = 2; @@ -83,7 +48,6 @@ libxl_ctx *ctx; xlchild children[child_max]; -#define INVALID_DOMID ~0 static const char *common_domname; static int fd_lock = -1; @@ -349,8 +313,6 @@ static char *xstrdup(const char *x) #define ARRAY_EXTEND_INIT_NODEVID(array,count,initfn) \ ARRAY_EXTEND_INIT__CORE((array),(count),(initfn), /* nothing */ ) -#define LOG(_f, _a...) dolog(__FILE__, __LINE__, __func__, _f "\n", ##_a) - static void dolog(const char *file, int line, const char *func, char *fmt, ...) __attribute__((format(printf,4,5))); @@ -3332,10 +3294,6 @@ static int64_t parse_mem_size_kb(const char *mem) return kbytes; } -/* Must be last in list */ -#define COMMON_LONG_OPTS {"help", 0, 0, 'h'}, \ - {0, 0, 0, 0} - /* * Callers should use SWITCH_FOREACH_OPT in preference to calling this * directly. @@ -3378,66 +3336,6 @@ static int def_getopt(int argc, char * const argv[], return -1; } -/* - * Wraps def_getopt into a convenient loop+switch to process all - * arguments. This macro is intended to be called from main_XXX(). - * - * SWITCH_FOREACH_OPT(int *opt, "OPTS", - * const struct option *longopts, - * const char *commandname, - * int num_opts_req) { ... - * - * opt: pointer to an int variable, holds the current option - * during processing. - * OPTS: short options, as per getopt_long(3)'s optstring argument. - * do not include "h"; will be provided automatically - * longopts: long options, as per getopt_long(3)'s longopts argument. - * May be null. - * commandname: name of this command, for usage string. - * num_required_opts: number of non-option command line parameters - * which are required. - * - * In addition the calling context is expected to contain variables - * "argc" and "argv" in the conventional C-style: - * main(int argc, char **argv) - * manner. - * - * Callers should treat SWITCH_FOREACH_OPT as they would a switch - * statement over the value of `opt`. Each option given in `opts` (or - * `lopts`) should be handled by a case statement as if it were inside - * a switch statement. - * - * In addition to the options provided in opts the macro will handle - * the "help" option and enforce a minimum number of non-option - * command line pearameters as follows: - * -- if the user passes a -h or --help option. help will be printed, - * and the macro will cause the process to exit with code 0. - * -- if the user does not provided `num_required_opts` non-option - * arguments, the macro will cause the process to exit with code 2. - * - * Example: - * - * int main_foo(int argc, char **argv) { - * int opt; - * - * SWITCH_FOREACH_OPT(opt, "blah", NULL, "foo", 0) { - * case 'b': - * ... handle b option... - * break; - * case 'l': - * ... handle l option ... - * break; - * case etc etc... - * } - * ... do something useful with the options ... - * } - */ -#define SWITCH_FOREACH_OPT(opt, opts, longopts, \ - commandname, num_required_opts) \ - while (((opt) = def_getopt(argc, argv, "h" opts, (longopts), \ - (commandname), (num_required_opts))) != -1) \ - switch (opt) - static int set_memory_max(uint32_t domid, const char *mem) { int64_t memorykb; diff --git a/tools/xl/xl_utils.h b/tools/xl/xl_utils.h new file mode 100644 index 0000000000..ebb9305b7c --- /dev/null +++ b/tools/xl/xl_utils.h @@ -0,0 +1,129 @@ +/* + * Copyright 2009-2017 Citrix Ltd and other contributors + * + * This program 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; version 2.1 only. with the special + * exception on linking described in file LICENSE. + * + * This program 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. + */ + +#ifndef XL_UTILS_H +#define XL_UTILS_H + +/* For calls which return an errno on failure */ +#define CHK_ERRNOVAL( call ) ({ \ + int chk_errnoval = (call); \ + if (chk_errnoval < 0) \ + abort(); \ + else if (chk_errnoval > 0) { \ + fprintf(stderr,"xl: fatal error: %s:%d: %s: %s\n", \ + __FILE__,__LINE__, strerror(chk_errnoval), #call); \ + exit(EXIT_FAILURE); \ + } \ + }) + +/* For calls which return -1 and set errno on failure */ +#define CHK_SYSCALL( call ) ({ \ + if ((call) == -1) { \ + fprintf(stderr,"xl: fatal error: %s:%d: %s: %s\n", \ + __FILE__,__LINE__, strerror(errno), #call); \ + exit(EXIT_FAILURE); \ + } \ + }) + +#define MUST( call ) ({ \ + int must_rc = (call); \ + if (must_rc < 0) { \ + fprintf(stderr,"xl: fatal error: %s:%d, rc=%d: %s\n", \ + __FILE__,__LINE__, must_rc, #call); \ + exit(EXIT_FAILURE); \ + } \ + }) + +#define STR_HAS_PREFIX( a, b ) \ + ( strncmp(a, b, strlen(b)) == 0 ) +#define STR_SKIP_PREFIX( a, b ) \ + ( STR_HAS_PREFIX(a, b) ? ((a) += strlen(b), 1) : 0 ) + +#define INVALID_DOMID ~0 + +#define LOG(_f, _a...) dolog(__FILE__, __LINE__, __func__, _f "\n", ##_a) + +/* + * Wraps def_getopt into a convenient loop+switch to process all + * arguments. This macro is intended to be called from main_XXX(). + * + * SWITCH_FOREACH_OPT(int *opt, "OPTS", + * const struct option *longopts, + * const char *commandname, + * int num_opts_req) { ... + * + * opt: pointer to an int variable, holds the current option + * during processing. + * OPTS: short options, as per getopt_long(3)'s optstring argument. + * do not include "h"; will be provided automatically + * longopts: long options, as per getopt_long(3)'s longopts argument. + * May be null. + * commandname: name of this command, for usage string. + * num_required_opts: number of non-option command line parameters + * which are required. + * + * In addition the calling context is expected to contain variables + * "argc" and "argv" in the conventional C-style: + * main(int argc, char **argv) + * manner. + * + * Callers should treat SWITCH_FOREACH_OPT as they would a switch + * statement over the value of `opt`. Each option given in `opts` (or + * `lopts`) should be handled by a case statement as if it were inside + * a switch statement. + * + * In addition to the options provided in opts the macro will handle + * the "help" option and enforce a minimum number of non-option + * command line pearameters as follows: + * -- if the user passes a -h or --help option. help will be printed, + * and the macro will cause the process to exit with code 0. + * -- if the user does not provided `num_required_opts` non-option + * arguments, the macro will cause the process to exit with code 2. + * + * Example: + * + * int main_foo(int argc, char **argv) { + * int opt; + * + * SWITCH_FOREACH_OPT(opt, "blah", NULL, "foo", 0) { + * case 'b': + * ... handle b option... + * break; + * case 'l': + * ... handle l option ... + * break; + * case etc etc... + * } + * ... do something useful with the options ... + * } + */ +#define SWITCH_FOREACH_OPT(opt, opts, longopts, \ + commandname, num_required_opts) \ + while (((opt) = def_getopt(argc, argv, "h" opts, (longopts), \ + (commandname), (num_required_opts))) != -1) \ + switch (opt) + +/* Must be last in list */ +#define COMMON_LONG_OPTS {"help", 0, 0, 'h'}, \ + {0, 0, 0, 0} + +#endif /* XL_UTILS_H */ + +/* + * Local variables: + * mode: C + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */