From patchwork Thu Jan 10 14:27:10 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hemmo Nieminen X-Patchwork-Id: 1960361 Return-Path: X-Original-To: patchwork-linux-nfs@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 297E7DF264 for ; Thu, 10 Jan 2013 14:34:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751113Ab3AJOeE (ORCPT ); Thu, 10 Jan 2013 09:34:04 -0500 Received: from mail-la0-f51.google.com ([209.85.215.51]:61672 "EHLO mail-la0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751432Ab3AJOeE (ORCPT ); Thu, 10 Jan 2013 09:34:04 -0500 Received: by mail-la0-f51.google.com with SMTP id fj20so662798lab.38 for ; Thu, 10 Jan 2013 06:34:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:sender:date:from:to:subject:message-id:mime-version :content-type:content-disposition:user-agent; bh=pQQgZIU9kYRpV1AKg+yCxTJu7XUPK2SHKRYgmWtuwWY=; b=NQkS9xnm6437fXNd8vHUQAcVBWR2UtU/6zEWzmu2iIINUrFfHTg2N84/NgRWRpp2vL qssJqGJCTDUtFHidxa6tbdvTbFGftD3FocMyRiCv4ofgNpaYFqUbIGExT1lfCU20LEY6 DgUa7MCopac6zJAoYToswA1JJahgXpHGOUOvYTEA6hyQhvRdgeTPf2IiViToe7D4NCMH RzJrP+fIUnlXUlT/+BLOBiYCPz5pNvtnjJsma76LoEDtdCyzPtuOH0tOeP+GLLC85JWH ai1eA7Y/Bf9Z9w2n0Wjt8ecPzlZPbYZu9H6BBt4Xi7jT9F80+03LuillUCd3O6UHbcoJ 2KZg== X-Received: by 10.152.110.18 with SMTP id hw18mr69496428lab.22.1357828023891; Thu, 10 Jan 2013 06:27:03 -0800 (PST) Received: from localhost (dsl-hkibrasgw3-54fb7d-194.dhcp.inet.fi. [84.251.125.194]) by mx.google.com with ESMTPS id fv12sm821911lab.12.2013.01.10.06.27.03 (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Thu, 10 Jan 2013 06:27:03 -0800 (PST) Date: Thu, 10 Jan 2013 16:27:10 +0200 From: Hemmo Nieminen To: linux-nfs@vger.kernel.org Subject: [nfs-utils PATCH] Binding a privileged port in rpc.statd. Message-ID: <20130110142710.GA2544@fcku.lan> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-nfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-nfs@vger.kernel.org Hi, It seems that rpc.statd will fail to bind to a (free) port, if the first port it receives is listed in /etc/services. The bindresvport will keep on returning the same port if we close the socket too early. The attached patch should fix this. diff --git a/utils/statd/rmtcall.c b/utils/statd/rmtcall.c index 4ecb03c..fd576d9 100644 --- a/utils/statd/rmtcall.c +++ b/utils/statd/rmtcall.c @@ -68,21 +68,19 @@ statd_get_socket(void) { struct sockaddr_in sin; struct servent *se; - int loopcnt = 100; + const int loopcnt = 100; + int i, tmp_sockets[loopcnt]; if (sockfd >= 0) return sockfd; - while (loopcnt-- > 0) { - - if (sockfd >= 0) close(sockfd); + for (i = 0; i < loopcnt; ++i) { if ((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { xlog(L_ERROR, "%s: Can't create socket: %m", __func__); - return -1; + break; } - memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); @@ -96,7 +94,16 @@ statd_get_socket(void) if (se == NULL) break; /* rather not use that port, try again */ + + tmp_sockets[i] = sockfd; } + + while (--i >= 0) + close(tmp_sockets[i]); + + if (sockfd < 0) + return -1; + FD_SET(sockfd, &SVC_FDSET); return sockfd; }