From patchwork Tue Jul 16 17:06:22 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petre Ovidiu PIRCALABU X-Patchwork-Id: 11046533 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7913914DB for ; Tue, 16 Jul 2019 17:08:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 60082283A6 for ; Tue, 16 Jul 2019 17:08:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 53B5E2867F; Tue, 16 Jul 2019 17:08:27 +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=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, 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 AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id EF5F628681 for ; Tue, 16 Jul 2019 17:08:25 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hnQuP-0005og-Dk; Tue, 16 Jul 2019 17:06:33 +0000 Received: from us1-rack-dfw2.inumbo.com ([104.130.134.6]) by lists.xenproject.org with esmtp (Exim 4.89) (envelope-from ) id 1hnQuN-0005nq-HI for xen-devel@lists.xenproject.org; Tue, 16 Jul 2019 17:06:31 +0000 X-Inumbo-ID: 0d7bb84c-a7ec-11e9-8980-bc764e045a96 Received: from mx01.bbu.dsd.mx.bitdefender.com (unknown [91.199.104.161]) by us1-rack-dfw2.inumbo.com (Halon) with ESMTPS id 0d7bb84c-a7ec-11e9-8980-bc764e045a96; Tue, 16 Jul 2019 17:06:30 +0000 (UTC) Received: from smtp.bitdefender.com (smtp02.buh.bitdefender.net [10.17.80.76]) by mx01.bbu.dsd.mx.bitdefender.com (Postfix) with ESMTPS id 27655305FFA6; Tue, 16 Jul 2019 20:06:26 +0300 (EEST) Received: from bitdefender.com (unknown [195.189.155.70]) by smtp.bitdefender.com (Postfix) with ESMTPSA id 0E30D304F607; Tue, 16 Jul 2019 20:06:26 +0300 (EEST) From: Petre Pircalabu To: xen-devel@lists.xenproject.org Date: Tue, 16 Jul 2019 20:06:22 +0300 Message-Id: <2f0d996d9fde04c1a12cee7a1cb58486cf7788d6.1563293545.git.ppircalabu@bitdefender.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: References: In-Reply-To: References: Subject: [Xen-devel] [PATCH v2 08/10] xen-access: Use getopt_long for cmdline parsing X-BeenThere: xen-devel@lists.xenproject.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Cc: Petre Pircalabu , Tamas K Lengyel , Razvan Cojocaru , Wei Liu , Ian Jackson , Alexandru Isaila MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xenproject.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP This simplifies the command line parsing logic and makes it easier to add new test parameters. Signed-off-by: Petre Pircalabu Acked-by: Tamas K Lengyel Reviewed-by: Alexandru Isaila --- tools/tests/xen-access/xen-access.c | 60 +++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/tools/tests/xen-access/xen-access.c b/tools/tests/xen-access/xen-access.c index 6aaee16..8a3eea5 100644 --- a/tools/tests/xen-access/xen-access.c +++ b/tools/tests/xen-access/xen-access.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include @@ -397,93 +398,102 @@ int main(int argc, char *argv[]) uint16_t altp2m_view_id = 0; char* progname = argv[0]; - argv++; - argc--; + char* command; + int c; + int option_index; + struct option long_options[] = + { + { "mem-access-listener", no_argument, 0, 'm' }, + }; - if ( argc == 3 && argv[0][0] == '-' ) + while ( 1 ) { - if ( !strcmp(argv[0], "-m") ) - required = 1; - else + c = getopt_long(argc, argv, "m", long_options, &option_index); + if ( c == -1 ) + break; + + switch ( c ) { + case 'm': + required = 1; + break; + + default: usage(progname); return -1; } - argv++; - argc--; } - if ( argc != 2 ) + if ( argc - optind != 2 ) { usage(progname); return -1; } - domain_id = atoi(argv[0]); - argv++; - argc--; + domain_id = atoi(argv[optind++]); + command = argv[optind]; - if ( !strcmp(argv[0], "write") ) + if ( !strcmp(command, "write") ) { default_access = XENMEM_access_rx; after_first_access = XENMEM_access_rwx; memaccess = 1; } - else if ( !strcmp(argv[0], "exec") ) + else if ( !strcmp(command, "exec") ) { default_access = XENMEM_access_rw; after_first_access = XENMEM_access_rwx; memaccess = 1; } #if defined(__i386__) || defined(__x86_64__) - else if ( !strcmp(argv[0], "breakpoint") ) + else if ( !strcmp(command, "breakpoint") ) { breakpoint = 1; } - else if ( !strcmp(argv[0], "altp2m_write") ) + else if ( !strcmp(command, "altp2m_write") ) { default_access = XENMEM_access_rx; altp2m = 1; memaccess = 1; } - else if ( !strcmp(argv[0], "altp2m_exec") ) + else if ( !strcmp(command, "altp2m_exec") ) { default_access = XENMEM_access_rw; altp2m = 1; memaccess = 1; } - else if ( !strcmp(argv[0], "altp2m_write_no_gpt") ) + else if ( !strcmp(command, "altp2m_write_no_gpt") ) { default_access = XENMEM_access_rw; altp2m_write_no_gpt = 1; memaccess = 1; altp2m = 1; } - else if ( !strcmp(argv[0], "debug") ) + else if ( !strcmp(command, "debug") ) { debug = 1; } - else if ( !strcmp(argv[0], "cpuid") ) + else if ( !strcmp(command, "cpuid") ) { cpuid = 1; } - else if ( !strcmp(argv[0], "desc_access") ) + else if ( !strcmp(command, "desc_access") ) { desc_access = 1; } - else if ( !strcmp(argv[0], "write_ctrlreg_cr4") ) + else if ( !strcmp(command, "write_ctrlreg_cr4") ) { write_ctrlreg_cr4 = 1; } #elif defined(__arm__) || defined(__aarch64__) - else if ( !strcmp(argv[0], "privcall") ) + else if ( !strcmp(command, "privcall") ) { privcall = 1; } #endif else { - usage(argv[0]); + usage(command); return -1; } @@ -494,7 +504,7 @@ int main(int argc, char *argv[]) return 1; } - DPRINTF("starting %s %u\n", argv[0], domain_id); + DPRINTF("starting %s %u\n", command, domain_id); /* ensure that if we get a signal, we'll do cleanup, then exit */ act.sa_handler = close_handler;