From: Cyril Hrubis <chrubis@suse.cz>
To: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
Cc: ltp-list@lists.sourceforge.net
Subject: Re: [LTP] [PATCH v5 01/11] SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname()
Date: Wed, 5 Aug 2015 16:00:02 +0200 [thread overview]
Message-ID: <20150805140001.GA821@rei.suse.de> (raw)
In-Reply-To: <1437024647-2066-1-git-send-email-zenglg.jy@cn.fujitsu.com>
Hi!
> +int safe_bind(const char *file, const int lineno, void (cleanup_fn)(void),
> + int socket, const struct sockaddr *address,
> + socklen_t address_len)
> +{
> + int err, ret, i;
> +
> + for (i = 0; i < 30; i++) {
> + ret = bind(socket, address, address_len);
> + err = errno;
> +
> + if (!ret)
> + return 0;
> +
> + if (err != EADDRINUSE) {
> + tst_brkm(TBROK | TERRNO, cleanup_fn,
> + "%s:%d: bind(%d, %p, %d) failed", file, lineno,
> + socket, address, address_len);
I think that it would be a bit nicer if we created a helper to translate
the socket structure to it's string representation (and use it whenever
we deal with struct sockaddr passed from user). We would have to switch
on sa_family and then print either ip address or path (for unix
sockets), etc. But that would get us bind(8, '127.0.0.1:1024', ...)
failed instead of the raw pointer to the structure which is kind of
useless.
> + }
> +
> + tst_resm(TINFO, "bind('%p') failed with %s, try %2i...",
> + address, tst_strerrno(err), i+1);
Hmm, why do we do tst_strerno(err) here when
the only value for errno can be EADDRINUSE
(since we exit with tst_brkm() otherwise)
Why not just hardcode EADDRINUSE to the
message?
> + if (i == 0 && err == EADDRINUSE)
> + tst_resm(TINFO, "The given address is already in use.");
This message is not really useful because we
allready printed that bind failed with
EADDRINUSE with the tst_resm above.
> + usleep(1000000);
We can use sleep(1) instead now.
> + }
> +
> + tst_brkm(TBROK | TERRNO, cleanup_fn,
> + "%s:%d: Failed to bind(%d, %p, %d) after 30 retries",
> + file, lineno, socket, address, address_len);
> +}
> +
> +int safe_listen(const char *file, const int lineno, void (cleanup_fn)(void),
> + int socket, int backlog)
> +{
> + int rval;
> +
> + rval = listen(socket, backlog);
> +
> + if (rval < 0) {
> + tst_brkm(TBROK | TERRNO, cleanup_fn,
> + "%s:%d: listen(%d, %d) failed", file, lineno, socket,
> + backlog);
> + }
> +
> + return rval;
> +}
> +
> +int safe_connect(const char *file, const int lineno, void (cleanup_fn)(void),
> + int sockfd, const struct sockaddr *addr, socklen_t addrlen)
> +{
> + int rval;
> +
> + rval = connect(sockfd, addr, addrlen);
> +
> + if (rval < 0) {
> + tst_brkm(TBROK | TERRNO, cleanup_fn,
> + "%s:%d: connect(%d, %p, %d) failed", file, lineno,
> + sockfd, addr, addrlen);
> + }
> +
> + return rval;
> +}
> +
> +int safe_getsockname(const char *file, const int lineno,
> + void (cleanup_fn)(void), int sockfd, struct sockaddr *addr,
> + socklen_t *addrlen)
> +{
> + int rval;
> +
> + rval = getsockname(sockfd, addr, addrlen);
> +
> + if (rval < 0) {
> + tst_brkm(TBROK | TERRNO, cleanup_fn,
> + "%s:%d: getsockname(%d, %p, %p) failed", file, lineno,
> + sockfd, addr, addrlen);
> + }
> +
> + return rval;
> +}
--
Cyril Hrubis
chrubis@suse.cz
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
next prev parent reply other threads:[~2015-08-05 14:00 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-03-31 7:46 [LTP] [PATCH 1/6] lib6/runcc.c: use TCONF instead of TBROK when no C compiler is present Xing Gu
2015-03-31 7:46 ` [LTP] [PATCH 2/6] lib6/asapi_04.c: cleanup Xing Gu
2015-04-30 11:55 ` Cyril Hrubis
2015-04-30 12:18 ` Cyril Hrubis
2015-03-31 7:46 ` [LTP] [PATCH 3/6] lib6/asapi_05.c: cleanup Xing Gu
2015-04-30 12:15 ` Cyril Hrubis
2015-03-31 7:46 ` [LTP] [PATCH 4/6] lib6/asapi_06.c: cleanup Xing Gu
2015-04-30 13:33 ` Cyril Hrubis
2015-03-31 7:46 ` [LTP] [PATCH 5/6] lib6/getaddrinfo_01.c: cleanup Xing Gu
2015-03-31 7:46 ` [LTP] [PATCH 6/6] ipv6_lib: add it into default Xing Gu
2015-03-31 14:52 ` [LTP] [PATCH 1/6] lib6/runcc.c: use TCONF instead of TBROK when no C compiler is present Alexey Kodanev
2015-04-01 1:38 ` gux.fnst
2015-04-01 7:15 ` Alexey Kodanev
2015-04-14 9:24 ` gux.fnst
2015-04-28 17:24 ` Cyril Hrubis
[not found] ` <1433247689-3984-1-git-send-email-zenglg.jy@cn.fujitsu.com>
[not found] ` <1433247689-3984-2-git-send-email-zenglg.jy@cn.fujitsu.com>
2015-06-06 12:52 ` [LTP] [PATCH v2 1/6] lib6/runcc.c: Cleanup Alexey Kodanev
2015-06-16 12:24 ` [LTP] [PATCH v3 1/7] SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname() Zeng Linggang
2015-06-16 12:24 ` [LTP] [PATCH v3 2/7] lib6/runcc.c: Cleanup Zeng Linggang
2015-06-19 16:08 ` Alexey Kodanev
2015-06-16 12:24 ` [LTP] [PATCH v3 3/7] lib6/asapi_04.c: Cleanup Zeng Linggang
2015-06-16 12:24 ` [LTP] [PATCH v3 4/7] lib6/asapi_05.c: Cleanup Zeng Linggang
2015-06-16 12:24 ` [LTP] [PATCH v3 5/7] lib6/asapi_06.c: Cleanup Zeng Linggang
2015-06-16 12:24 ` [LTP] [PATCH v3 6/7] lib6/getaddrinfo_01.c: Cleanup Zeng Linggang
2015-06-16 12:24 ` [LTP] [PATCH v3 7/7] ipv6_lib: Add it into default Zeng Linggang
2015-06-19 14:34 ` [LTP] [PATCH v3 1/7] SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname() Alexey Kodanev
2015-07-09 3:05 ` [LTP] [PATCH v4 01/11] " Zeng Linggang
2015-07-09 3:05 ` [LTP] [PATCH v4 02/11] lib6/runcc.c: Cleanup Zeng Linggang
2015-07-09 3:05 ` [LTP] [PATCH v4 03/11] asapi_01: Use 'tst_tmpdir' and more cleanup Zeng Linggang
2015-07-09 3:05 ` [LTP] [PATCH v4 04/11] asapi_02: " Zeng Linggang
2015-07-09 3:05 ` [LTP] [PATCH v4 05/11] asapi_03: " Zeng Linggang
2015-07-09 3:05 ` [LTP] [PATCH v4 06/11] lib6/asapi_04.c: Cleanup Zeng Linggang
2015-07-09 3:05 ` [LTP] [PATCH v4 07/11] lib6/asapi_05.c: Cleanup Zeng Linggang
2015-07-09 3:05 ` [LTP] [PATCH v4 08/11] lib6/asapi_06.c: Cleanup Zeng Linggang
2015-07-09 3:05 ` [LTP] [PATCH v4 09/11] asapi_07: Use 'tst_tmpdir' and more cleanup Zeng Linggang
2015-07-09 3:05 ` [LTP] [PATCH v4 10/11] lib6/getaddrinfo_01.c: Cleanup Zeng Linggang
2015-07-09 3:05 ` [LTP] [PATCH v4 11/11] ipv6_lib: Add it into default Zeng Linggang
2015-07-15 15:49 ` [LTP] [PATCH v4 01/11] SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname() Alexey Kodanev
2015-07-16 2:30 ` Zeng Linggang
2015-07-16 5:30 ` [LTP] [PATCH v5 " Zeng Linggang
2015-07-16 5:30 ` [LTP] [PATCH v5 02/11] lib6/runcc.c: Cleanup Zeng Linggang
2015-08-05 17:17 ` Cyril Hrubis
2015-08-05 17:29 ` Cyril Hrubis
[not found] ` <1441016132-18804-1-git-send-email-zenglg.jy@cn.fujitsu.com>
[not found] ` <1441016132-18804-2-git-send-email-zenglg.jy@cn.fujitsu.com>
2015-09-14 9:56 ` [LTP] [PATCH v6 2/7] SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname() Alexey Kodanev
2015-07-16 5:30 ` [LTP] [PATCH v5 03/11] asapi_01: Use 'tst_tmpdir' and more cleanup Zeng Linggang
2015-07-16 5:30 ` [LTP] [PATCH v5 04/11] asapi_02: " Zeng Linggang
2015-07-16 5:30 ` [LTP] [PATCH v5 05/11] asapi_03: " Zeng Linggang
2015-07-16 5:30 ` [LTP] [PATCH v5 06/11] lib6/asapi_04.c: Cleanup Zeng Linggang
2015-07-16 5:30 ` [LTP] [PATCH v5 07/11] lib6/asapi_05.c: Cleanup Zeng Linggang
2015-07-16 5:30 ` [LTP] [PATCH v5 08/11] lib6/asapi_06.c: Cleanup Zeng Linggang
2015-07-16 5:30 ` [LTP] [PATCH v5 09/11] asapi_07: Use 'tst_tmpdir' and more cleanup Zeng Linggang
2015-07-16 5:30 ` [LTP] [PATCH v5 10/11] lib6/getaddrinfo_01.c: Cleanup Zeng Linggang
2015-07-16 5:30 ` [LTP] [PATCH v5 11/11] ipv6_lib: Add it into default Zeng Linggang
2015-08-05 14:00 ` Cyril Hrubis [this message]
[not found] ` <55C236CB.8060508@oracle.com>
[not found] ` <1438858276.11271.20.camel@G08FNSTD140232>
2015-08-06 11:56 ` [LTP] [PATCH v5 01/11] SAFE_MACROS: Add socket(), bind(), listen(), connect() and getsockname() Cyril Hrubis
[not found] ` <1433247689-3984-3-git-send-email-zenglg.jy@cn.fujitsu.com>
2015-06-06 13:22 ` [LTP] [PATCH v2 2/6] lib6/asapi_04.c: Cleanup Alexey Kodanev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150805140001.GA821@rei.suse.de \
--to=chrubis@suse.cz \
--cc=ltp-list@lists.sourceforge.net \
--cc=zenglg.jy@cn.fujitsu.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox