039bf80
From 08ba2f630c8eebd023ae68d8e2abd1e7170468af Mon Sep 17 00:00:00 2001
039bf80
From: TJ Saunders <tj@castaglia.org>
039bf80
Date: Sun, 14 May 2017 14:09:23 -0700
039bf80
Subject: [PATCH] Issue #501: Avoid a spurious "Address already in use" error
039bf80
 on startup because we are listening on a local socket twice.
039bf80
039bf80
---
039bf80
 modules/mod_ctrls.c | 22 +++++++---------------
039bf80
 1 file changed, 7 insertions(+), 15 deletions(-)
039bf80
039bf80
diff --git a/modules/mod_ctrls.c b/modules/mod_ctrls.c
039bf80
index 25ea723..8efd8b4 100644
039bf80
--- a/modules/mod_ctrls.c
039bf80
+++ b/modules/mod_ctrls.c
039bf80
@@ -2,8 +2,7 @@
039bf80
  * ProFTPD: mod_ctrls -- a module implementing the ftpdctl local socket
039bf80
  *          server, as well as several utility functions for other Controls
039bf80
  *          modules
039bf80
- *
039bf80
- * Copyright (c) 2000-2016 TJ Saunders
039bf80
+ * Copyright (c) 2000-2017 TJ Saunders
039bf80
  *
039bf80
  * This program is free software; you can redistribute it and/or modify
039bf80
  * it under the terms of the GNU General Public License as published by
039bf80
@@ -81,8 +80,6 @@ static ctrls_acl_t ctrls_sock_acl;
039bf80
 
039bf80
 static unsigned char ctrls_engine = TRUE;
039bf80
 
039bf80
-#define CTRLS_LISTEN_FL_REMOVE_SOCKET	0x0001
039bf80
-
039bf80
 /* Necessary prototypes */
039bf80
 static int ctrls_setblock(int sockfd);
039bf80
 static int ctrls_setnonblock(int sockfd);
039bf80
@@ -437,7 +434,7 @@ static int ctrls_cls_write(void) {
039bf80
 }
039bf80
 
039bf80
 /* Create a listening local socket */
039bf80
-static int ctrls_listen(const char *sock_file, int flags) {
039bf80
+static int ctrls_listen(const char *sock_file) {
039bf80
   int sockfd = -1, len = 0;
039bf80
   struct sockaddr_un sock;
039bf80
 #if !defined(SO_PEERCRED) && !defined(HAVE_GETPEEREID) && \
039bf80
@@ -497,12 +494,10 @@ static int ctrls_listen(const char *sock_file, int flags) {
039bf80
     return -1;
039bf80
   }
039bf80
 
039bf80
-  if (flags & CTRLS_LISTEN_FL_REMOVE_SOCKET) {
039bf80
-    /* Make sure the path to which we want to bind this socket doesn't already
039bf80
-     * exist.
039bf80
-     */
039bf80
-    (void) unlink(sock_file);
039bf80
-  }
039bf80
+  /* Make sure the path to which we want to bind this socket doesn't already
039bf80
+   * exist.
039bf80
+   */
039bf80
+  (void) unlink(sock_file);
039bf80
 
039bf80
   /* Fill in the socket structure fields */
039bf80
   memset(&sock, 0, sizeof(sock));
039bf80
@@ -1206,7 +1201,7 @@ static void ctrls_postparse_ev(const void *event_data, void *user_data) {
039bf80
 
039bf80
   /* Start listening on the ctrl socket */
039bf80
   PRIVS_ROOT
039bf80
-  ctrls_sockfd = ctrls_listen(ctrls_sock_file, CTRLS_LISTEN_FL_REMOVE_SOCKET);
039bf80
+  ctrls_sockfd = ctrls_listen(ctrls_sock_file);
039bf80
   PRIVS_RELINQUISH
039bf80
 
039bf80
   /* Start a timer for the checking/processing of the ctrl socket.  */
039bf80
@@ -1298,9 +1293,6 @@ static int ctrls_init(void) {
039bf80
   memset(&ctrls_sock_acl, '\0', sizeof(ctrls_acl_t));
039bf80
   ctrls_sock_acl.acl_usrs.allow = ctrls_sock_acl.acl_grps.allow = FALSE;
039bf80
 
039bf80
-  /* Start listening on the ctrl socket */
039bf80
-  ctrls_sockfd = ctrls_listen(ctrls_sock_file, 0);
039bf80
-
039bf80
   pr_event_register(&ctrls_module, "core.restart", ctrls_restart_ev, NULL);
039bf80
   pr_event_register(&ctrls_module, "core.shutdown", ctrls_shutdown_ev, NULL);
039bf80
   pr_event_register(&ctrls_module, "core.postparse", ctrls_postparse_ev, NULL);