diff mbox series

[1/1] ns_ifmove.c: Rewrite to new API

Message ID 20230309145917.26846-1-pvorel@suse.cz
State Accepted
Headers show
Series [1/1] ns_ifmove.c: Rewrite to new API | expand

Commit Message

Petr Vorel March 9, 2023, 2:59 p.m. UTC
Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
 testcases/kernel/containers/share/ns_ifmove.c | 98 ++++++-------------
 1 file changed, 28 insertions(+), 70 deletions(-)

Comments

Andrea Cervesato March 9, 2023, 2:59 p.m. UTC | #1
Hi Petr,

looks fine to me.

Acked-by: Andrea Cervesato <andrea.cervesato@suse.com>

On 3/9/23 15:59, Petr Vorel wrote:
> Signed-off-by: Petr Vorel <pvorel@suse.cz>
> ---
>   testcases/kernel/containers/share/ns_ifmove.c | 98 ++++++-------------
>   1 file changed, 28 insertions(+), 70 deletions(-)
>
> diff --git a/testcases/kernel/containers/share/ns_ifmove.c b/testcases/kernel/containers/share/ns_ifmove.c
> index 12642c6f4..60dda94d3 100644
> --- a/testcases/kernel/containers/share/ns_ifmove.c
> +++ b/testcases/kernel/containers/share/ns_ifmove.c
> @@ -1,43 +1,31 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
>   /* Copyright (c) 2015 Red Hat, Inc.
> - *
> - * This program is free software: you can redistribute it and/or modify
> - * it under the terms of version 2 the GNU General Public License as
> - * published by the Free Software Foundation.
> - *
> - * 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 General Public License for more details.
> - *
> - * You should have received a copy of the GNU General Public License
> - * along with this program.  If not, see <http://www.gnu.org/licenses/>.
> - *
> + * Copyright (c) Linux Test Project, 2015-2022
> + * Copyright (c) 2023 Petr Vorel <pvorel@suse.cz>
>    * Written by Matus Marhefka <mmarhefk@redhat.com>
> + */
> +
> +/*\
> + * [Description]
>    *
> - ***********************************************************************
>    * Moves a network interface to the namespace of a process specified by a PID.
> - *
>    */
>   
> -#define _GNU_SOURCE
> -#include <stdlib.h>
> -#include <string.h>
> -#include <errno.h>
> -#include <unistd.h>
> -#include <asm/types.h>
> -#include <sys/socket.h>
> -#include <linux/rtnetlink.h>
> -#include <sys/ioctl.h>
> -#include <linux/if.h>
> -#include <net/ethernet.h>
> -#include <arpa/inet.h>
> -#include "test.h"
> -
>   #include "config.h"
>   
> -char *TCID = "ns_ifmove";
> +#define TST_NO_DEFAULT_MAIN
> +#include "tst_test.h"
> +#include "tst_safe_macros.h"
> +#include "tst_safe_net.h"
> +
> +#include <linux/if.h>
> +#include <linux/rtnetlink.h>
> +#include <net/ethernet.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
>   
> -#if HAVE_DECL_IFLA_NET_NS_PID
> +#ifdef HAVE_DECL_IFLA_NET_NS_PID
>   
>   struct {
>   	struct nlmsghdr nh;
> @@ -55,50 +43,28 @@ int get_intf_index_from_name(const char *intf_name)
>   	strncpy(ifr.ifr_name, intf_name, sizeof(ifr.ifr_name) - 1);
>   	ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0';
>   
> -	sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
> -	if (sock_fd == -1) {
> -		tst_resm(TINFO | TERRNO, "socket");
> -		return -1;
> -	}
> +	sock_fd = SAFE_SOCKET(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
>   
> -	/* gets interface index */
> -	if (ioctl(sock_fd, SIOCGIFINDEX, &ifr) == -1) {
> -		tst_resm(TINFO | TERRNO, "ioctl");
> -		close(sock_fd);
> -		return -1;
> -	}
> +	/* interface index */
> +	SAFE_IOCTL(sock_fd, SIOCGIFINDEX, &ifr);
> +	SAFE_CLOSE(sock_fd);
>   
> -	close(sock_fd);
>   	return ifr.ifr_ifindex;
>   }
>   
> -/*
> - * ./ns_ifmove <INTERFACE_NAME> <NAMESPACE_PID>
> - */
>   int main(int argc, char **argv)
>   {
>   	struct rtattr *rta;
>   	int intf_index, pid, rtnetlink_socket;
>   
>   	if (argc != 3) {
> -		tst_resm(TINFO, "%s <INTERFACE_NAME> <NAMESPACE_PID>",
> -			 argv[0]);
> +		printf("ns_ifmove <INTERFACE_NAME> <NAMESPACE_PID>\n");
>   		return 1;
>   	}
>   
>   	intf_index = get_intf_index_from_name(argv[1]);
> -	if (intf_index == -1) {
> -		tst_resm(TINFO , "unable to get interface index");
> -		return 1;
> -	}
> -
>   	pid = atoi(argv[2]);
> -
> -	rtnetlink_socket = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
> -	if (rtnetlink_socket == -1) {
> -		tst_resm(TINFO | TERRNO, "socket");
> -		return 1;
> -	}
> +	rtnetlink_socket = SAFE_SOCKET(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
>   
>   	memset(&req, 0, sizeof(req));
>   	req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
> @@ -115,20 +81,12 @@ int main(int argc, char **argv)
>   		RTA_LENGTH(sizeof(pid));
>   	memcpy(RTA_DATA(rta), &pid, sizeof(pid));
>   
> -	if (send(rtnetlink_socket, &req, req.nh.nlmsg_len, 0) == -1) {
> -		tst_resm(TINFO | TERRNO, "send");
> -		return 1;
> -	}
> +	SAFE_SEND(1, rtnetlink_socket, &req, req.nh.nlmsg_len, 0);
> +	SAFE_CLOSE(rtnetlink_socket);
>   
> -	close(rtnetlink_socket);
>   	return 0;
>   }
>   
>   #else
> -
> -int main(void)
> -{
> -	tst_brkm(TCONF, NULL, "IFLA_NET_NS_PID not defined in linux/if_link.h");
> -}
> -
> +	TST_TEST_TCONF("IFLA_NET_NS_PID not defined in linux/if_link.h");
>   #endif
Bird, Tim March 10, 2023, 2:54 a.m. UTC | #2
> -----Original Message-----
> From: ltp <ltp-bounces+tim.bird=sony.com@lists.linux.it> On Behalf Of Petr Vorel
> Sent: Thursday, March 9, 2023 7:59 AM
> To: ltp@lists.linux.it
> Subject: [LTP] [PATCH 1/1] ns_ifmove.c: Rewrite to new API
> 
> Signed-off-by: Petr Vorel <pvorel@suse.cz>
> ---
>  testcases/kernel/containers/share/ns_ifmove.c | 98 ++++++-------------
>  1 file changed, 28 insertions(+), 70 deletions(-)
> 
> diff --git a/testcases/kernel/containers/share/ns_ifmove.c b/testcases/kernel/containers/share/ns_ifmove.c
> index 12642c6f4..60dda94d3 100644
> --- a/testcases/kernel/containers/share/ns_ifmove.c
> +++ b/testcases/kernel/containers/share/ns_ifmove.c
> @@ -1,43 +1,31 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later

I don't see "or later" anywhere in the license header that was removed.

Is this the correct SPDX license identifier?
Maybe it should be 'GPL-2.0-only'
 -- Tim

>  /* Copyright (c) 2015 Red Hat, Inc.
> - *
> - * This program is free software: you can redistribute it and/or modify
> - * it under the terms of version 2 the GNU General Public License as
> - * published by the Free Software Foundation.
> - *
> - * 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 General Public License for more details.
> - *
> - * You should have received a copy of the GNU General Public License
> - * along with this program.  If not, see <http://www.gnu.org/licenses/>.
> - *
> + * Copyright (c) Linux Test Project, 2015-2022
> + * Copyright (c) 2023 Petr Vorel <pvorel@suse.cz>
>   * Written by Matus Marhefka <mmarhefk@redhat.com>
> + */
> +
> +/*\
> + * [Description]
>   *
> - ***********************************************************************
>   * Moves a network interface to the namespace of a process specified by a PID.
> - *
>   */
> 
> -#define _GNU_SOURCE
> -#include <stdlib.h>
> -#include <string.h>
> -#include <errno.h>
> -#include <unistd.h>
> -#include <asm/types.h>
> -#include <sys/socket.h>
> -#include <linux/rtnetlink.h>
> -#include <sys/ioctl.h>
> -#include <linux/if.h>
> -#include <net/ethernet.h>
> -#include <arpa/inet.h>
> -#include "test.h"
> -
>  #include "config.h"
> 
> -char *TCID = "ns_ifmove";
> +#define TST_NO_DEFAULT_MAIN
> +#include "tst_test.h"
> +#include "tst_safe_macros.h"
> +#include "tst_safe_net.h"
> +
> +#include <linux/if.h>
> +#include <linux/rtnetlink.h>
> +#include <net/ethernet.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> 
> -#if HAVE_DECL_IFLA_NET_NS_PID
> +#ifdef HAVE_DECL_IFLA_NET_NS_PID
> 
>  struct {
>  	struct nlmsghdr nh;
> @@ -55,50 +43,28 @@ int get_intf_index_from_name(const char *intf_name)
>  	strncpy(ifr.ifr_name, intf_name, sizeof(ifr.ifr_name) - 1);
>  	ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0';
> 
> -	sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
> -	if (sock_fd == -1) {
> -		tst_resm(TINFO | TERRNO, "socket");
> -		return -1;
> -	}
> +	sock_fd = SAFE_SOCKET(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
> 
> -	/* gets interface index */
> -	if (ioctl(sock_fd, SIOCGIFINDEX, &ifr) == -1) {
> -		tst_resm(TINFO | TERRNO, "ioctl");
> -		close(sock_fd);
> -		return -1;
> -	}
> +	/* interface index */
> +	SAFE_IOCTL(sock_fd, SIOCGIFINDEX, &ifr);
> +	SAFE_CLOSE(sock_fd);
> 
> -	close(sock_fd);
>  	return ifr.ifr_ifindex;
>  }
> 
> -/*
> - * ./ns_ifmove <INTERFACE_NAME> <NAMESPACE_PID>
> - */
>  int main(int argc, char **argv)
>  {
>  	struct rtattr *rta;
>  	int intf_index, pid, rtnetlink_socket;
> 
>  	if (argc != 3) {
> -		tst_resm(TINFO, "%s <INTERFACE_NAME> <NAMESPACE_PID>",
> -			 argv[0]);
> +		printf("ns_ifmove <INTERFACE_NAME> <NAMESPACE_PID>\n");
>  		return 1;
>  	}
> 
>  	intf_index = get_intf_index_from_name(argv[1]);
> -	if (intf_index == -1) {
> -		tst_resm(TINFO , "unable to get interface index");
> -		return 1;
> -	}
> -
>  	pid = atoi(argv[2]);
> -
> -	rtnetlink_socket = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
> -	if (rtnetlink_socket == -1) {
> -		tst_resm(TINFO | TERRNO, "socket");
> -		return 1;
> -	}
> +	rtnetlink_socket = SAFE_SOCKET(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
> 
>  	memset(&req, 0, sizeof(req));
>  	req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
> @@ -115,20 +81,12 @@ int main(int argc, char **argv)
>  		RTA_LENGTH(sizeof(pid));
>  	memcpy(RTA_DATA(rta), &pid, sizeof(pid));
> 
> -	if (send(rtnetlink_socket, &req, req.nh.nlmsg_len, 0) == -1) {
> -		tst_resm(TINFO | TERRNO, "send");
> -		return 1;
> -	}
> +	SAFE_SEND(1, rtnetlink_socket, &req, req.nh.nlmsg_len, 0);
> +	SAFE_CLOSE(rtnetlink_socket);
> 
> -	close(rtnetlink_socket);
>  	return 0;
>  }
> 
>  #else
> -
> -int main(void)
> -{
> -	tst_brkm(TCONF, NULL, "IFLA_NET_NS_PID not defined in linux/if_link.h");
> -}
> -
> +	TST_TEST_TCONF("IFLA_NET_NS_PID not defined in linux/if_link.h");
>  #endif
> --
> 2.39.2
> 
> 
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
Petr Vorel March 10, 2023, 7:34 a.m. UTC | #3
Hi Tim,

> > +++ b/testcases/kernel/containers/share/ns_ifmove.c
> > @@ -1,43 +1,31 @@
> > +// SPDX-License-Identifier: GPL-2.0-or-later

> I don't see "or later" anywhere in the license header that was removed.

> Is this the correct SPDX license identifier?
> Maybe it should be 'GPL-2.0-only'
Good catch, thank you!

Kind regards,
Petr

>  -- Tim

> >  /* Copyright (c) 2015 Red Hat, Inc.
> > - *
> > - * This program is free software: you can redistribute it and/or modify
> > - * it under the terms of version 2 the GNU General Public License as
> > - * published by the Free Software Foundation.
> > - *
> > - * 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 General Public License for more details.
> > - *
> > - * You should have received a copy of the GNU General Public License
> > - * along with this program.  If not, see <http://www.gnu.org/licenses/>.
> > - *
> > + * Copyright (c) Linux Test Project, 2015-2022
> > + * Copyright (c) 2023 Petr Vorel <pvorel@suse.cz>
> >   * Written by Matus Marhefka <mmarhefk@redhat.com>
> > + */
Petr Vorel March 10, 2023, 11:47 a.m. UTC | #4
Hi all,

> Hi Tim,

> > > +++ b/testcases/kernel/containers/share/ns_ifmove.c
> > > @@ -1,43 +1,31 @@
> > > +// SPDX-License-Identifier: GPL-2.0-or-later

> > I don't see "or later" anywhere in the license header that was removed.

> > Is this the correct SPDX license identifier?
> > Maybe it should be 'GPL-2.0-only'
> Good catch, thank you!

FYI I contacted Matus Marhefka privately and got his ack:

	I am not on that mailing list anymore. Anyway, feel free to re-license any
	of my tests to GPL-v2+, you have my agreement :)

=> going to use the original version.

Kind regards,
Petr

> Kind regards,
> Petr

> >  -- Tim

> > >  /* Copyright (c) 2015 Red Hat, Inc.
> > > - *
> > > - * This program is free software: you can redistribute it and/or modify
> > > - * it under the terms of version 2 the GNU General Public License as
> > > - * published by the Free Software Foundation.
> > > - *
> > > - * 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 General Public License for more details.
> > > - *
> > > - * You should have received a copy of the GNU General Public License
> > > - * along with this program.  If not, see <http://www.gnu.org/licenses/>.
> > > - *
> > > + * Copyright (c) Linux Test Project, 2015-2022
> > > + * Copyright (c) 2023 Petr Vorel <pvorel@suse.cz>
> > >   * Written by Matus Marhefka <mmarhefk@redhat.com>
> > > + */
Bird, Tim March 10, 2023, 6:11 p.m. UTC | #5
> -----Original Message-----
> From: Petr Vorel <pvorel@suse.cz>
> 
> Hi all,
> 
> > Hi Tim,
> 
> > > > +++ b/testcases/kernel/containers/share/ns_ifmove.c
> > > > @@ -1,43 +1,31 @@
> > > > +// SPDX-License-Identifier: GPL-2.0-or-later
> 
> > > I don't see "or later" anywhere in the license header that was removed.
> 
> > > Is this the correct SPDX license identifier?
> > > Maybe it should be 'GPL-2.0-only'
> > Good catch, thank you!
> 
> FYI I contacted Matus Marhefka privately and got his ack:
> 
> 	I am not on that mailing list anymore. Anyway, feel free to re-license any
> 	of my tests to GPL-v2+, you have my agreement :)
> 
> => going to use the original version.

Sounds good.  You may want to note this authorization to change the
license, in the commit message for this patch.
  -- Tim

> 
> Kind regards,
> Petr
> 
> > Kind regards,
> > Petr
> 
> > >  -- Tim
> 
> > > >  /* Copyright (c) 2015 Red Hat, Inc.
> > > > - *
> > > > - * This program is free software: you can redistribute it and/or modify
> > > > - * it under the terms of version 2 the GNU General Public License as
> > > > - * published by the Free Software Foundation.
> > > > - *
> > > > - * 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 General Public License for more details.
> > > > - *
> > > > - * You should have received a copy of the GNU General Public License
> > > > - * along with this program.  If not, see <http://www.gnu.org/licenses/>.
> > > > - *
> > > > + * Copyright (c) Linux Test Project, 2015-2022
> > > > + * Copyright (c) 2023 Petr Vorel <pvorel@suse.cz>
> > > >   * Written by Matus Marhefka <mmarhefk@redhat.com>
> > > > + */
Petr Vorel March 13, 2023, 8:53 p.m. UTC | #6
> > -----Original Message-----
> > From: Petr Vorel <pvorel@suse.cz>

> > Hi all,

> > > Hi Tim,

> > > > > +++ b/testcases/kernel/containers/share/ns_ifmove.c
> > > > > @@ -1,43 +1,31 @@
> > > > > +// SPDX-License-Identifier: GPL-2.0-or-later

> > > > I don't see "or later" anywhere in the license header that was removed.

> > > > Is this the correct SPDX license identifier?
> > > > Maybe it should be 'GPL-2.0-only'
> > > Good catch, thank you!

> > FYI I contacted Matus Marhefka privately and got his ack:

> > 	I am not on that mailing list anymore. Anyway, feel free to re-license any
> > 	of my tests to GPL-v2+, you have my agreement :)

> > => going to use the original version.

> Sounds good.  You may want to note this authorization to change the
> license, in the commit message for this patch.

Hi Tim,

Make sense, I'll do. Thanks for paying attention to license, it's important.

Kind regards,
Petr

>   -- Tim
Wei Gao March 15, 2023, 12:56 a.m. UTC | #7
On Thu, Mar 09, 2023 at 03:59:17PM +0100, Petr Vorel wrote:
> Signed-off-by: Petr Vorel <pvorel@suse.cz>
> ---
>  testcases/kernel/containers/share/ns_ifmove.c | 98 ++++++-------------
>  1 file changed, 28 insertions(+), 70 deletions(-)
> 
>  	int intf_index, pid, rtnetlink_socket;
>  
>  	if (argc != 3) {
> -		tst_resm(TINFO, "%s <INTERFACE_NAME> <NAMESPACE_PID>",
> -			 argv[0]);
> +		printf("ns_ifmove <INTERFACE_NAME> <NAMESPACE_PID>\n");
>  		return 1;
>  	}
>  
should we use tst_res(TINFO) instead of printf?
Petr Vorel March 15, 2023, 2:01 a.m. UTC | #8
> On Thu, Mar 09, 2023 at 03:59:17PM +0100, Petr Vorel wrote:
> > Signed-off-by: Petr Vorel <pvorel@suse.cz>
> > ---
> >  testcases/kernel/containers/share/ns_ifmove.c | 98 ++++++-------------
> >  1 file changed, 28 insertions(+), 70 deletions(-)

> >  	int intf_index, pid, rtnetlink_socket;

> >  	if (argc != 3) {
> > -		tst_resm(TINFO, "%s <INTERFACE_NAME> <NAMESPACE_PID>",
> > -			 argv[0]);
> > +		printf("ns_ifmove <INTERFACE_NAME> <NAMESPACE_PID>\n");
> >  		return 1;
> >  	}

> should we use tst_res(TINFO) instead of printf?

NO. If you look into testcases/lib/, none of *.c tools use tst_res().
The only separation between API for tests and API for tools / library
than TST_NO_DEFAULT_MAIN.

Actually, unless SAFE_*() are useful to use (here SAFE_SOCKET() and
SAFE_IOCTL()), we prefer to not use tst_test.h at all (see tst_cgctl.c,
tst_device.c, ...)

I also think that tst_get_free_pids.c does not need TST_NO_DEFAULT_MAIN and
tst_test workarounds.

Kind regards,
Petr
Wei Gao March 15, 2023, 4:38 a.m. UTC | #9
On Wed, Mar 15, 2023 at 03:01:40AM +0100, Petr Vorel wrote:
> > On Thu, Mar 09, 2023 at 03:59:17PM +0100, Petr Vorel wrote:
> > > Signed-off-by: Petr Vorel <pvorel@suse.cz>
> > > ---
> > >  testcases/kernel/containers/share/ns_ifmove.c | 98 ++++++-------------
> > >  1 file changed, 28 insertions(+), 70 deletions(-)
> 
> > >  	int intf_index, pid, rtnetlink_socket;
> 
> > >  	if (argc != 3) {
> > > -		tst_resm(TINFO, "%s <INTERFACE_NAME> <NAMESPACE_PID>",
> > > -			 argv[0]);
> > > +		printf("ns_ifmove <INTERFACE_NAME> <NAMESPACE_PID>\n");
> > >  		return 1;
> > >  	}
> 
> > should we use tst_res(TINFO) instead of printf?
> 
> NO. If you look into testcases/lib/, none of *.c tools use tst_res().
> The only separation between API for tests and API for tools / library
> than TST_NO_DEFAULT_MAIN.
Got it, this is lib API. Why ns_ifmove.c's parent directory name is 
"share" instead of "lib" or at least contain "lib" key word?

> 
> Actually, unless SAFE_*() are useful to use (here SAFE_SOCKET() and
> SAFE_IOCTL()), we prefer to not use tst_test.h at all (see tst_cgctl.c,
> tst_device.c, ...)

> 
> I also think that tst_get_free_pids.c does not need TST_NO_DEFAULT_MAIN and
> tst_test workarounds.
Is there possible split current tst_test.h to two files and remove 
TST_NO_DEFAULT_MAIN workaround? Such as tst_tools.h(include SAFE*) for lib api
and main.h for testcase.

> 
> Kind regards,
> Petr
Petr Vorel March 15, 2023, 10:35 a.m. UTC | #10
Hi Wei,

> > > should we use tst_res(TINFO) instead of printf?

> > NO. If you look into testcases/lib/, none of *.c tools use tst_res().
> > The only separation between API for tests and API for tools / library
> > than TST_NO_DEFAULT_MAIN.
> Got it, this is lib API. Why ns_ifmove.c's parent directory name is 
> "share" instead of "lib" or at least contain "lib" key word?

The move is in another patchset:
https://lore.kernel.org/ltp/20230310124125.14279-1-pvorel@suse.cz/

> > Actually, unless SAFE_*() are useful to use (here SAFE_SOCKET() and
> > SAFE_IOCTL()), we prefer to not use tst_test.h at all (see tst_cgctl.c,
> > tst_device.c, ...)


> > I also think that tst_get_free_pids.c does not need TST_NO_DEFAULT_MAIN and
> > tst_test workarounds.
> Is there possible split current tst_test.h to two files and remove 
> TST_NO_DEFAULT_MAIN workaround? Such as tst_tools.h(include SAFE*) for lib api
> and main.h for testcase.

IMHO that would not be trivial, i.e. not worth of the effort.

Kind regards,
Petr
Petr Vorel March 23, 2023, 3:59 p.m. UTC | #11
Hi all,

FYI merged. Thanks for your comments.

Kind regards,
Petr
diff mbox series

Patch

diff --git a/testcases/kernel/containers/share/ns_ifmove.c b/testcases/kernel/containers/share/ns_ifmove.c
index 12642c6f4..60dda94d3 100644
--- a/testcases/kernel/containers/share/ns_ifmove.c
+++ b/testcases/kernel/containers/share/ns_ifmove.c
@@ -1,43 +1,31 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
 /* Copyright (c) 2015 Red Hat, Inc.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of version 2 the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
+ * Copyright (c) Linux Test Project, 2015-2022
+ * Copyright (c) 2023 Petr Vorel <pvorel@suse.cz>
  * Written by Matus Marhefka <mmarhefk@redhat.com>
+ */
+
+/*\
+ * [Description]
  *
- ***********************************************************************
  * Moves a network interface to the namespace of a process specified by a PID.
- *
  */
 
-#define _GNU_SOURCE
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <unistd.h>
-#include <asm/types.h>
-#include <sys/socket.h>
-#include <linux/rtnetlink.h>
-#include <sys/ioctl.h>
-#include <linux/if.h>
-#include <net/ethernet.h>
-#include <arpa/inet.h>
-#include "test.h"
-
 #include "config.h"
 
-char *TCID = "ns_ifmove";
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "tst_safe_net.h"
+
+#include <linux/if.h>
+#include <linux/rtnetlink.h>
+#include <net/ethernet.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
-#if HAVE_DECL_IFLA_NET_NS_PID
+#ifdef HAVE_DECL_IFLA_NET_NS_PID
 
 struct {
 	struct nlmsghdr nh;
@@ -55,50 +43,28 @@  int get_intf_index_from_name(const char *intf_name)
 	strncpy(ifr.ifr_name, intf_name, sizeof(ifr.ifr_name) - 1);
 	ifr.ifr_name[sizeof(ifr.ifr_name)-1] = '\0';
 
-	sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
-	if (sock_fd == -1) {
-		tst_resm(TINFO | TERRNO, "socket");
-		return -1;
-	}
+	sock_fd = SAFE_SOCKET(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
 
-	/* gets interface index */
-	if (ioctl(sock_fd, SIOCGIFINDEX, &ifr) == -1) {
-		tst_resm(TINFO | TERRNO, "ioctl");
-		close(sock_fd);
-		return -1;
-	}
+	/* interface index */
+	SAFE_IOCTL(sock_fd, SIOCGIFINDEX, &ifr);
+	SAFE_CLOSE(sock_fd);
 
-	close(sock_fd);
 	return ifr.ifr_ifindex;
 }
 
-/*
- * ./ns_ifmove <INTERFACE_NAME> <NAMESPACE_PID>
- */
 int main(int argc, char **argv)
 {
 	struct rtattr *rta;
 	int intf_index, pid, rtnetlink_socket;
 
 	if (argc != 3) {
-		tst_resm(TINFO, "%s <INTERFACE_NAME> <NAMESPACE_PID>",
-			 argv[0]);
+		printf("ns_ifmove <INTERFACE_NAME> <NAMESPACE_PID>\n");
 		return 1;
 	}
 
 	intf_index = get_intf_index_from_name(argv[1]);
-	if (intf_index == -1) {
-		tst_resm(TINFO , "unable to get interface index");
-		return 1;
-	}
-
 	pid = atoi(argv[2]);
-
-	rtnetlink_socket = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
-	if (rtnetlink_socket == -1) {
-		tst_resm(TINFO | TERRNO, "socket");
-		return 1;
-	}
+	rtnetlink_socket = SAFE_SOCKET(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
 
 	memset(&req, 0, sizeof(req));
 	req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
@@ -115,20 +81,12 @@  int main(int argc, char **argv)
 		RTA_LENGTH(sizeof(pid));
 	memcpy(RTA_DATA(rta), &pid, sizeof(pid));
 
-	if (send(rtnetlink_socket, &req, req.nh.nlmsg_len, 0) == -1) {
-		tst_resm(TINFO | TERRNO, "send");
-		return 1;
-	}
+	SAFE_SEND(1, rtnetlink_socket, &req, req.nh.nlmsg_len, 0);
+	SAFE_CLOSE(rtnetlink_socket);
 
-	close(rtnetlink_socket);
 	return 0;
 }
 
 #else
-
-int main(void)
-{
-	tst_brkm(TCONF, NULL, "IFLA_NET_NS_PID not defined in linux/if_link.h");
-}
-
+	TST_TEST_TCONF("IFLA_NET_NS_PID not defined in linux/if_link.h");
 #endif