All of lore.kernel.org
 help / color / mirror / Atom feed
From: Petr Uzel <petr.uzel@suse.cz>
To: util-linux@vger.kernel.org
Subject: [PATCH 06/20] uuidd: factor out socket creation into separate function
Date: Thu, 29 Mar 2012 18:45:14 +0200	[thread overview]
Message-ID: <1333039528-24784-7-git-send-email-petr.uzel@suse.cz> (raw)
In-Reply-To: <1333039528-24784-1-git-send-email-petr.uzel@suse.cz>


Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
---
 misc-utils/uuidd.c |   74 ++++++++++++++++++++++++++++++++-------------------
 1 files changed, 46 insertions(+), 28 deletions(-)

diff --git a/misc-utils/uuidd.c b/misc-utils/uuidd.c
index c5886fd..a135816 100644
--- a/misc-utils/uuidd.c
+++ b/misc-utils/uuidd.c
@@ -247,34 +247,20 @@ static int create_pidfile(const char *pidfile_path, int quiet)
 	return fd_pidfile;
 }
 
-static void server_loop(const char *socket_path, const char *pidfile_path,
-			int debug, int timeout, int quiet, int no_fork)
+/*
+ * Create AF_UNIX, SOCK_STREAM socket and bind to @socket_path
+ *
+ * If @will_fork is true, then make sure the descriptor
+ * of the socket is >2, so that it wont be later closed
+ * during create_daemon().
+ *
+ * Return file descriptor corresponding to created socket.
+ */
+static int create_socket(const char *socket_path, int will_fork, int quiet)
 {
-	struct sockaddr_un	my_addr, from_addr;
-	socklen_t		fromlen;
-	int32_t			reply_len = 0;
-	uuid_t			uu;
+	struct sockaddr_un	my_addr;
 	mode_t			save_umask;
-	char			reply_buf[1024], *cp;
-	char			op, str[UUID_STR_LEN];
-	int			i, s, ns, len, num;
-	int			fd_pidfile = -1;
-	int			ret;
-
-	signal(SIGALRM, terminate_intr);
-	alarm(30);
-
-	if (pidfile_path)
-		fd_pidfile = create_pidfile(pidfile_path, quiet);
-
-	ret = call_daemon(socket_path, UUIDD_OP_GETPID, reply_buf, sizeof(reply_buf), 0, NULL);
-	if (ret > 0) {
-		if (!quiet)
-			printf(_("uuidd daemon already running at pid %s\n"),
-			       reply_buf);
-		exit(EXIT_FAILURE);
-	}
-	alarm(0);
+	int 			s;
 
 	if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
 		if (!quiet)
@@ -286,7 +272,7 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
 	 * Make sure the socket isn't using fd numbers 0-2 to avoid it
 	 * getting closed by create_daemon()
 	 */
-	while ((!debug || no_fork) && s <= 2) {
+	while (will_fork && s <= 2) {
 		s = dup(s);
 		if (s < 0)
 			err(EXIT_FAILURE, "dup");
@@ -308,7 +294,40 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
 		exit(EXIT_FAILURE);
 	}
 	umask(save_umask);
+	cleanup_socket = socket_path;
 
+	return s;
+}
+
+static void server_loop(const char *socket_path, const char *pidfile_path,
+			int debug, int timeout, int quiet, int no_fork)
+{
+	struct sockaddr_un	from_addr;
+	socklen_t		fromlen;
+	int32_t			reply_len = 0;
+	uuid_t			uu;
+	char			reply_buf[1024], *cp;
+	char			op, str[UUID_STR_LEN];
+	int			i, s, ns, len, num;
+	int			fd_pidfile = -1;
+	int			ret;
+
+	signal(SIGALRM, terminate_intr);
+	alarm(30);
+
+	if (pidfile_path)
+		fd_pidfile = create_pidfile(pidfile_path, quiet);
+
+	ret = call_daemon(socket_path, UUIDD_OP_GETPID, reply_buf, sizeof(reply_buf), 0, NULL);
+	if (ret > 0) {
+		if (!quiet)
+			printf(_("uuidd daemon already running at pid %s\n"),
+			       reply_buf);
+		exit(EXIT_FAILURE);
+	}
+	alarm(0);
+
+	s = create_socket(socket_path, (!debug || !no_fork), quiet);
 	if (listen(s, SOMAXCONN) < 0) {
 		if (!quiet)
 			fprintf(stderr, _("Couldn't listen on unix "
@@ -316,7 +335,6 @@ static void server_loop(const char *socket_path, const char *pidfile_path,
 		exit(EXIT_FAILURE);
 	}
 
-	cleanup_socket = socket_path;
 	if (!debug && !no_fork)
 		create_daemon();
 	signal(SIGHUP, terminate_intr);
-- 
1.7.7


  parent reply	other threads:[~2012-03-29 16:45 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-29 16:45 [PATCH 00/20] *** uuidd: refactoring & systemd support + build-sys fixes *** Petr Uzel
2012-03-29 16:45 ` [PATCH 01/20] uuidd: use UUIDD_OP_GETPID instead of magic number Petr Uzel
2012-03-29 16:45 ` [PATCH 02/20] uuidd: remove useless initialization of cleanup_socket Petr Uzel
2012-03-29 16:45 ` [PATCH 03/20] uuidd: factor out pidfile creation into separate function Petr Uzel
2012-03-29 16:45 ` [PATCH 04/20] uuidd: implement --no-pid option Petr Uzel
2012-04-03 12:51   ` Karel Zak
2012-04-05  7:36     ` Petr Uzel
2012-03-29 16:45 ` [PATCH 05/20] uuidd: implement --no-fork option Petr Uzel
2012-03-29 16:45 ` Petr Uzel [this message]
2012-03-29 16:45 ` [PATCH 07/20] uuidd: implement --socket-activation option Petr Uzel
2012-04-03 13:03   ` Karel Zak
2012-04-05  7:46     ` Petr Uzel
2012-03-29 16:45 ` [PATCH 08/20] uuidd: print all debugging information to stderr Petr Uzel
2012-03-29 16:45 ` [PATCH 09/20] uuidd: factor out dropping of privileges into separate function Petr Uzel
2012-03-29 16:45 ` [PATCH 10/20] uuidd: make drop_privs true by default in main() Petr Uzel
2012-03-29 21:29   ` Ted Ts'o
2012-03-31 16:38     ` Petr Uzel
2012-03-29 16:45 ` [PATCH 11/20] uuidd: introduce --keep-privs option Petr Uzel
2012-04-03 13:32   ` Karel Zak
2012-04-05  7:48     ` Petr Uzel
2012-03-29 16:45 ` [PATCH 12/20] uuidd: --socket-activation implies --keep-privs Petr Uzel
2012-04-03 13:38   ` Karel Zak
2012-04-05  7:49     ` Petr Uzel
2012-03-29 16:45 ` [PATCH 13/20] uuidd: add systemd unit files Petr Uzel
2012-04-03 14:01   ` Karel Zak
2012-04-03 14:47     ` Tom Gundersen
2012-04-05  7:52       ` Petr Uzel
2012-04-05  8:23         ` Karel Zak
2012-03-29 16:45 ` [PATCH 14/20] libuuid: use EXIT_FAILURE Petr Uzel
2012-03-29 16:45 ` [PATCH 15/20] libuuid: implement --disable-libuuid-exec-uuidd configure option Petr Uzel
2012-03-29 16:45 ` [PATCH 16/20] libuuid: fix typo in uuid_compare manpage Petr Uzel
2012-03-29 16:45 ` [PATCH 17/20] build-sys: run distcheck with verbose make rules Petr Uzel
2012-03-29 16:45 ` [PATCH 18/20] build-sys: add ttyutils.h to dist Petr Uzel
2012-03-29 16:45 ` [PATCH 19/20] build-sys: add fsprobe.h " Petr Uzel
2012-03-29 16:45 ` [PATCH 20/20] build-sys: fix installation of uuidd units with make distcheck Petr Uzel

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=1333039528-24784-7-git-send-email-petr.uzel@suse.cz \
    --to=petr.uzel@suse.cz \
    --cc=util-linux@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.