From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from na01-by2-obe.outbound.protection.outlook.com (mail-by2on0107.outbound.protection.outlook.com [207.46.100.107]) by dpdk.org (Postfix) with ESMTP id C5A7B91D6 for ; Thu, 15 Oct 2015 18:22:58 +0200 (CEST) Received: from CY1PR0101MB0987.prod.exchangelabs.com (10.160.224.149) by CY1PR0101MB0986.prod.exchangelabs.com (10.160.224.148) with Microsoft SMTP Server (TLS) id 15.1.293.16; Thu, 15 Oct 2015 16:22:54 +0000 Received: from CY1PR0101MB0987.prod.exchangelabs.com ([10.160.224.149]) by CY1PR0101MB0987.prod.exchangelabs.com ([10.160.224.149]) with mapi id 15.01.0293.007; Thu, 15 Oct 2015 16:22:54 +0000 From: Don Provan To: Tiwei Bie , "bruce.richardson@intel.com" , "dev@dpdk.org" Thread-Topic: [PATCH] eal: don't reset getopt lib Thread-Index: AQHRBz62v0fVSuBcPU2QhiPDLiAaG55su2Lg Date: Thu, 15 Oct 2015 16:22:53 +0000 Message-ID: References: <1444909564-53691-1-git-send-email-btw@mail.ustc.edu.cn> In-Reply-To: <1444909564-53691-1-git-send-email-btw@mail.ustc.edu.cn> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: spf=none (sender IP is ) smtp.mailfrom=dprovan@bivio.net; x-originating-ip: [209.234.132.35] x-microsoft-exchange-diagnostics: 1; CY1PR0101MB0986; 5:+/pFVj4XWeaPEHvyKQku8zmLXEv7DGuHsSdNOKOCmszNxPcNIf8RzxBfRdbcmMeBezyRdrsVAhV5QORtMC5fmzkoLDanOV57quM9hOxq1nJFVJ1NtU7ktXRcxTsW1glbb/C4J+ZKmymcpzWVxnWt5A==; 24:y/YQnfhsWtMyZg5jPgnUL7nh2bamJk2G0gHOn0SBuU9ejyVu+w5iUgiH7RSmBb6xd2cHYevgEy6Q0HAJF+9vg2aAhU64yGzuibGf+l2Sfx4=; 20:A7VuwFNygdL0bFWRmse5BYXhM3IZlNFUtmIt6loScsqXTOr78E9SW75XU9w3uxdq1oBXphHzdToE1HSFkXqikA== x-microsoft-antispam: UriScan:;BCL:0;PCL:0;RULEID:;SRVR:CY1PR0101MB0986; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(262628399951293); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(2401047)(5005006)(8121501046)(520078)(3002001); SRVR:CY1PR0101MB0986; BCL:0; PCL:0; RULEID:; SRVR:CY1PR0101MB0986; x-forefront-prvs: 0730093765 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(6009001)(189002)(377454003)(199003)(13464003)(5007970100001)(5003600100002)(5004730100002)(189998001)(107886002)(46102003)(81156007)(5008740100001)(106116001)(2201001)(97736004)(76176999)(10400500002)(40100003)(5001770100001)(74316001)(86362001)(50986999)(5001960100002)(19580405001)(106356001)(102836002)(77096005)(122556002)(2950100001)(11100500001)(19580395003)(33656002)(87936001)(66066001)(101416001)(105586002)(2171001)(54356999)(64706001)(2900100001)(92566002)(5002640100001)(2501003); DIR:OUT; SFP:1102; SCL:1; SRVR:CY1PR0101MB0986; H:CY1PR0101MB0987.prod.exchangelabs.com; FPR:; SPF:None; PTR:InfoNoRecords; MX:3; A:1; LANG:en; received-spf: None (protection.outlook.com: bivio.net does not designate permitted sender hosts) spamdiagnosticoutput: 1:23 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: bivio.net X-MS-Exchange-CrossTenant-originalarrivaltime: 15 Oct 2015 16:22:53.7404 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 8731bc55-0e76-4eb7-ae4b-401e56037945 X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR0101MB0986 Subject: Re: [dpdk-dev] [PATCH] eal: don't reset getopt lib X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 Oct 2015 16:22:59 -0000 Looks perfect. Thanks! -don -----Original Message----- From: Tiwei Bie [mailto:btw@mail.ustc.edu.cn]=20 Sent: Thursday, October 15, 2015 4:46 AM To: Don Provan ; bruce.richardson@intel.com; dev@dpdk.or= g Subject: [PATCH] eal: don't reset getopt lib Someone may need to call rte_eal_init() with a fake argc/argv array in the = middle of using getopt() to parse its own unrelated argc/argv parameters. S= o getopt lib shouldn't be reset by rte_eal_init(). Now eal will always save optind, optarg and optopt (and optreset on FreeBSD) at the beginning, initialize optind (and optreset on FreeBSD) to 1= before calling getopt_long(), then restore all values after. Suggested-by: Don Provan Suggested-by: Bruce Richardson Signed-off-by: Tiwei Bie --- lib/librte_eal/bsdapp/eal/eal.c | 59 +++++++++++++++++++++++++++------ lib/librte_eal/linuxapp/eal/eal.c | 69 ++++++++++++++++++++++++++++++-----= ---- 2 files changed, 102 insertions(+), 26 deletions(-) diff --git a/lib/librte_eal/bsdapp/eal/eal.c b/lib/librte_eal/bsdapp/eal/ea= l.c index 1b6f705..bd09377 100644 --- a/lib/librte_eal/bsdapp/eal/eal.c +++ b/lib/librte_eal/bsdapp/eal/eal.c @@ -312,8 +312,20 @@ eal_log_level_parse(int argc, char **argv) int opt; char **argvopt; int option_index; + int old_optind; + int old_optopt; + int old_optreset; + char *old_optarg; + + /* save getopt lib */ + old_optind =3D optind; + old_optopt =3D optopt; + old_optreset =3D optreset; + old_optarg =3D optarg; =20 argvopt =3D argv; + optind =3D 1; + optreset =3D 1; =20 eal_reset_internal_config(&internal_config); =20 @@ -334,7 +346,11 @@ eal_log_level_parse(int argc, char **argv) break; } =20 - optind =3D 0; /* reset getopt lib */ + /* restore getopt lib */ + optind =3D old_optind; + optopt =3D old_optopt; + optreset =3D old_optreset; + optarg =3D old_optarg; } =20 /* Parse the argument given in the command line of the application */ @@ -= 345,25 +361,37 @@ eal_parse_args(int argc, char **argv) char **argvopt; int option_index; char *prgname =3D argv[0]; + int old_optind; + int old_optopt; + int old_optreset; + char *old_optarg; + + /* save getopt lib */ + old_optind =3D optind; + old_optopt =3D optopt; + old_optreset =3D optreset; + old_optarg =3D optarg; =20 argvopt =3D argv; + optind =3D 1; + optreset =3D 1; =20 while ((opt =3D getopt_long(argc, argvopt, eal_short_options, eal_long_options, &option_index)) !=3D EOF) { =20 - int ret; - /* getopt is not happy, stop right now */ if (opt =3D=3D '?') { eal_usage(prgname); - return -1; + ret =3D -1; + goto out; } =20 ret =3D eal_parse_common_option(opt, optarg, &internal_config); /* common parser is not happy */ if (ret < 0) { eal_usage(prgname); - return -1; + ret =3D -1; + goto out; } /* common parser handled this option */ if (ret =3D=3D 0) @@ -387,23 +415,34 @@ eal_parse_args(int argc, char **argv) "on FreeBSD\n", opt); } eal_usage(prgname); - return -1; + ret =3D -1; + goto out; } } =20 - if (eal_adjust_config(&internal_config) !=3D 0) - return -1; + if (eal_adjust_config(&internal_config) !=3D 0) { + ret =3D -1; + goto out; + } =20 /* sanity checks */ if (eal_check_common_options(&internal_config) !=3D 0) { eal_usage(prgname); - return -1; + ret =3D -1; + goto out; } =20 if (optind >=3D 0) argv[optind-1] =3D prgname; ret =3D optind-1; - optind =3D 0; /* reset getopt lib */ + +out: + /* restore getopt lib */ + optind =3D old_optind; + optopt =3D old_optopt; + optreset =3D old_optreset; + optarg =3D old_optarg; + return ret; } =20 diff --git a/lib/librte_eal/linuxapp/eal/eal.c b/lib/librte_eal/linuxapp/ea= l/eal.c index 33e1067..4796030 100644 --- a/lib/librte_eal/linuxapp/eal/eal.c +++ b/lib/librte_eal/linuxapp/eal/eal.c @@ -505,8 +505,17 @@ eal_log_level_parse(int argc, char **argv) int opt; char **argvopt; int option_index; + int old_optind; + int old_optopt; + char *old_optarg; + + /* save getopt lib */ + old_optind =3D optind; + old_optopt =3D optopt; + old_optarg =3D optarg; =20 argvopt =3D argv; + optind =3D 1; =20 eal_reset_internal_config(&internal_config); =20 @@ -527,7 +536,10 @@ eal_log_level_parse(int argc, char **argv) break; } =20 - optind =3D 0; /* reset getopt lib */ + /* restore getopt lib */ + optind =3D old_optind; + optopt =3D old_optopt; + optarg =3D old_optarg; } =20 /* Parse the argument given in the command line of the application */ @@ -= 539,25 +551,34 @@ eal_parse_args(int argc, char **argv) int option_index; char *prgname =3D argv[0]; struct shared_driver *solib; + int old_optind; + int old_optopt; + char *old_optarg; + + /* save getopt lib */ + old_optind =3D optind; + old_optopt =3D optopt; + old_optarg =3D optarg; =20 argvopt =3D argv; + optind =3D 1; =20 while ((opt =3D getopt_long(argc, argvopt, eal_short_options, eal_long_options, &option_index)) !=3D EOF) { =20 - int ret; - /* getopt is not happy, stop right now */ if (opt =3D=3D '?') { eal_usage(prgname); - return -1; + ret =3D -1; + goto out; } =20 ret =3D eal_parse_common_option(opt, optarg, &internal_config); /* common parser is not happy */ if (ret < 0) { eal_usage(prgname); - return -1; + ret =3D -1; + goto out; } /* common parser handled this option */ if (ret =3D=3D 0) @@ -573,7 +594,8 @@ eal_parse_args(int argc, char **argv) solib =3D malloc(sizeof(*solib)); if (solib =3D=3D NULL) { RTE_LOG(ERR, EAL, "malloc(solib) failed\n"); - return -1; + ret =3D -1; + goto out; } memset(solib, 0, sizeof(*solib)); strncpy(solib->name, optarg, PATH_MAX-1); @@ -589,7 +611,8 @@ eal_parse= _args(int argc, char **argv) RTE_LOG(ERR, EAL, "Can't support DPDK app " "running on Dom0, please configure" " RTE_LIBRTE_XEN_DOM0=3Dy\n"); - return -1; + ret =3D -1; + goto out; #endif break; =20 @@ -606,7 +629,8 @@ eal_parse_args(int argc, char **argv) RTE_LOG(ERR, EAL, "invalid parameters for --" OPT_SOCKET_MEM "\n"); eal_usage(prgname); - return -1; + ret =3D -1; + goto out; } break; =20 @@ -615,7 +639,8 @@ eal_parse_args(int argc, char **argv) RTE_LOG(ERR, EAL, "invalid parameter for --" OPT_BASE_VIRTADDR "\n"); eal_usage(prgname); - return -1; + ret =3D -1; + goto out; } break; =20 @@ -624,7 +649,8 @@ eal_parse_args(int argc, char **argv) RTE_LOG(ERR, EAL, "invalid parameters for --" OPT_VFIO_INTR "\n"); eal_usage(prgname); - return -1; + ret =3D -1; + goto out; } break; =20 @@ -646,17 +672,21 @@ eal_parse_args(int argc, char **argv) "on Linux\n", opt); } eal_usage(prgname); - return -1; + ret =3D -1; + goto out; } } =20 - if (eal_adjust_config(&internal_config) !=3D 0) - return -1; + if (eal_adjust_config(&internal_config) !=3D 0) { + ret =3D -1; + goto out; + } =20 /* sanity checks */ if (eal_check_common_options(&internal_config) !=3D 0) { eal_usage(prgname); - return -1; + ret =3D -1; + goto out; } =20 /* --xen-dom0 doesn't make sense with --socket-mem */ @@ -664,13 +694,20 = @@ eal_parse_args(int argc, char **argv) RTE_LOG(ERR, EAL, "Options --"OPT_SOCKET_MEM" cannot be specified " "together with --"OPT_XEN_DOM0"\n"); eal_usage(prgname); - return -1; + ret =3D -1; + goto out; } =20 if (optind >=3D 0) argv[optind-1] =3D prgname; ret =3D optind-1; - optind =3D 0; /* reset getopt lib */ + +out: + /* restore getopt lib */ + optind =3D old_optind; + optopt =3D old_optopt; + optarg =3D old_optarg; + return ret; } =20 -- 2.1.2