summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--utils/statd/rmtcall.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/utils/statd/rmtcall.c b/utils/statd/rmtcall.c
index 66a6eeb..45c84f9 100644
--- a/utils/statd/rmtcall.c
+++ b/utils/statd/rmtcall.c
@@ -52,6 +52,9 @@
static int sockfd = -1; /* notify socket */
+/* How many times to try looking for an unused privileged port */
+#define MAX_BRP_RETRIES 100
+
/*
* Initialize socket used to notify lockd of peer reboots.
*
@@ -68,14 +71,14 @@ statd_get_socket(void)
{
struct sockaddr_in sin;
struct servent *se;
- const int loopcnt = 100;
- int i, tmp_sockets[loopcnt];
+ static int prevsocks[MAX_BRP_RETRIES];
+ unsigned int retries;
if (sockfd >= 0)
return sockfd;
- for (i = 0; i < loopcnt; ++i) {
-
+ retries = 0;
+ do {
if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
xlog(L_ERROR, "%s: Can't create socket: %m", __func__);
break;
@@ -93,13 +96,19 @@ statd_get_socket(void)
se = getservbyport(sin.sin_port, "udp");
if (se == NULL)
break;
- /* rather not use that port, try again */
- tmp_sockets[i] = sockfd;
- }
+ if (retries == MAX_BRP_RETRIES) {
+ xlog(D_GENERAL, "%s: No unused privileged ports",
+ __func__);
+ break;
+ }
+
+ /* rather not use that port, try again */
+ prevsocks[retries++] = sockfd;
+ } while (1);
- while (--i >= 0)
- close(tmp_sockets[i]);
+ while (retries)
+ close(prevsocks[--retries]);
if (sockfd < 0)
return -1;