From owner-svn-src-all@FreeBSD.ORG Mon Feb 7 00:19:19 2011 Return-Path: Delivered-To: svn-src-all@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 14A15106566C; Mon, 7 Feb 2011 00:19:19 +0000 (UTC) (envelope-from to.my.trociny@gmail.com) Received: from mail-fx0-f54.google.com (mail-fx0-f54.google.com [209.85.161.54]) by mx1.freebsd.org (Postfix) with ESMTP id 462AD8FC0A; Mon, 7 Feb 2011 00:19:17 +0000 (UTC) Received: by fxm16 with SMTP id 16so4491739fxm.13 for ; Sun, 06 Feb 2011 16:19:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:references:x-comment-to:date :in-reply-to:message-id:user-agent:mime-version:content-type; bh=2rg1qMDEBye59hXuP+MIfIbJYGvxag3D6w8gTrSyXS8=; b=UGsrSat5EYexEsa3CPJB1RqKmoCnU9eMEPVLRLAX1a9fZf7VP2Bfo+l2XPaFyckMTN 6m48UHm2yn6WKvfy+2Yvc2Glsk4037E4M7VqaS/LFAUq/6QFvgq/4X3hBhIKKac0uCDF KHg8Ogho3b78DMmCSoIYz3PbqMUcylqY58rwA= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:references:x-comment-to:date:in-reply-to :message-id:user-agent:mime-version:content-type; b=voQCbt4slCV5W6QmJ8BargUuZDvQ5HHQ+BAola5rvrK6Dw+TYJ8evS4duBvOpbE4pf 1CMWW6Xfyr7tA3MRjOPmqRF4hU2p5Pcm8X97m3LOfPM+adS1WMre5zAOUd7O/jjhLIUR QpPNVXkVKXrykNsxKz4axQaycL6Y0MnbvHUkk= Received: by 10.223.96.73 with SMTP id g9mr2445237fan.24.1296945763942; Sat, 05 Feb 2011 14:42:43 -0800 (PST) Received: from localhost ([95.69.174.185]) by mx.google.com with ESMTPS id 17sm694510far.19.2011.02.05.14.42.32 (version=TLSv1/SSLv3 cipher=RC4-MD5); Sat, 05 Feb 2011 14:42:42 -0800 (PST) From: Mikolaj Golub To: Pawel Jakub Dawidek References: <201101282156.p0SLulCc001547@svn.freebsd.org> X-Comment-To: Pawel Jakub Dawidek Date: Sun, 06 Feb 2011 00:42:31 +0200 In-Reply-To: <201101282156.p0SLulCc001547@svn.freebsd.org> (Pawel Jakub Dawidek's message of "Fri, 28 Jan 2011 21:56:47 +0000 (UTC)") Message-ID: <8662syrtm0.fsf@kopusha.home.net> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: svn-src-head@freebsd.org, svn-src-all@freebsd.org, src-committers@freebsd.org Subject: Re: svn commit: r218044 - head/sbin/hastd X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 07 Feb 2011 00:19:19 -0000 On Fri, 28 Jan 2011 21:56:47 +0000 (UTC) Pawel Jakub Dawidek wrote: PJD> Author: pjd PJD> Date: Fri Jan 28 21:56:47 2011 PJD> New Revision: 218044 PJD> URL: http://svn.freebsd.org/changeset/base/218044 PJD> Log: PJD> Add function to assert that the only descriptors we have open are the ones PJD> we expect to be open. Also assert that they point at expected type. PJD> PJD> Because openlog(3) API is unable to tell us descriptor number it is using, we PJD> have to close syslog socket, remember assert message in local buffer and if we PJD> fail on assertion, reopen syslog socket and log the message. PJD> PJD> MFC after: 1 week PJD> Modified: PJD> head/sbin/hastd/hastd.c PJD> head/sbin/hastd/hastd.h PJD> Modified: head/sbin/hastd/hastd.c PJD> ============================================================================== PJD> --- head/sbin/hastd/hastd.c Fri Jan 28 21:52:37 2011 (r218043) PJD> +++ head/sbin/hastd/hastd.c Fri Jan 28 21:56:47 2011 (r218044) PJD> @@ -34,6 +34,7 @@ __FBSDID("$FreeBSD$"); PJD> #include PJD> #include PJD> #include PJD> +#include PJD> #include PJD> PJD> #include PJD> @@ -119,6 +120,146 @@ descriptors_cleanup(struct hast_resource PJD> pjdlog_fini(); PJD> } PJD> PJD> +static const char * PJD> +dtype2str(mode_t mode) PJD> +{ PJD> + PJD> + if (S_ISBLK(mode)) PJD> + return ("block device"); PJD> + else if (S_ISCHR(mode)) PJD> + return ("character device"); PJD> + else if (S_ISDIR(mode)) PJD> + return ("directory"); PJD> + else if (S_ISFIFO(mode)) PJD> + return ("pipe or FIFO"); PJD> + else if (S_ISLNK(mode)) PJD> + return ("symbolic link"); PJD> + else if (S_ISREG(mode)) PJD> + return ("regular file"); PJD> + else if (S_ISSOCK(mode)) PJD> + return ("socket"); PJD> + else if (S_ISWHT(mode)) PJD> + return ("whiteout"); PJD> + else PJD> + return ("unknown"); PJD> +} PJD> + PJD> +void PJD> +descriptors_assert(const struct hast_resource *res, int pjdlogmode) PJD> +{ PJD> + char msg[256]; PJD> + struct stat sb; PJD> + long maxfd; PJD> + bool isopen; PJD> + mode_t mode; PJD> + int fd; PJD> + PJD> + /* PJD> + * At this point descriptor to syslog socket is closed, so if we want PJD> + * to log assertion message, we have to first store it in 'msg' local PJD> + * buffer and then open syslog socket and log it. PJD> + */ PJD> + msg[0] = '\0'; PJD> + PJD> + maxfd = sysconf(_SC_OPEN_MAX); PJD> + if (maxfd < 0) { PJD> + pjdlog_errno(LOG_WARNING, "sysconf(_SC_OPEN_MAX) failed"); As it is commented above :-) syslog socket is closed so we can't call pjdlog_errno() here. PJD> + maxfd = 16384; PJD> + } PJD> + for (fd = 0; fd <= maxfd; fd++) { PJD> + if (fstat(fd, &sb) == 0) { PJD> + isopen = true; PJD> + mode = sb.st_mode; PJD> + } else if (errno == EBADF) { PJD> + isopen = false; PJD> + mode = 0; PJD> + } else { PJD> + isopen = true; /* silence gcc */ PJD> + mode = 0; /* silence gcc */ PJD> + snprintf(msg, sizeof(msg), PJD> + "Unable to fstat descriptor %d: %s", fd, PJD> + strerror(errno)); Shouldn't it break here? PJD> + } PJD> + if (fd == STDIN_FILENO || fd == STDOUT_FILENO || PJD> + fd == STDERR_FILENO) { PJD> + if (!isopen) { PJD> + snprintf(msg, sizeof(msg), PJD> + "Descriptor %d (%s) is closed, but should be open.", PJD> + fd, (fd == STDIN_FILENO ? "stdin" : PJD> + (fd == STDOUT_FILENO ? "stdout" : "stderr"))); PJD> + break; PJD> + } PJD> + } else if (fd == proto_descriptor(res->hr_event)) { PJD> + if (!isopen) { PJD> + snprintf(msg, sizeof(msg), PJD> + "Descriptor %d (event) is closed, but should be open.", PJD> + fd); PJD> + break; PJD> + } PJD> + if (!S_ISSOCK(mode)) { PJD> + snprintf(msg, sizeof(msg), PJD> + "Descriptor %d (event) is %s, but should be %s.", PJD> + fd, dtype2str(mode), dtype2str(S_IFSOCK)); PJD> + break; PJD> + } PJD> + } else if (fd == proto_descriptor(res->hr_ctrl)) { PJD> + if (!isopen) { PJD> + snprintf(msg, sizeof(msg), PJD> + "Descriptor %d (ctrl) is closed, but should be open.", PJD> + fd); PJD> + break; PJD> + } PJD> + if (!S_ISSOCK(mode)) { PJD> + snprintf(msg, sizeof(msg), PJD> + "Descriptor %d (ctrl) is %s, but should be %s.", PJD> + fd, dtype2str(mode), dtype2str(S_IFSOCK)); PJD> + break; PJD> + } PJD> + } else if (res->hr_role == HAST_ROLE_SECONDARY && PJD> + fd == proto_descriptor(res->hr_remotein)) { PJD> + if (!isopen) { PJD> + snprintf(msg, sizeof(msg), PJD> + "Descriptor %d (remote in) is closed, but should be open.", PJD> + fd); PJD> + break; PJD> + } PJD> + if (!S_ISSOCK(mode)) { PJD> + snprintf(msg, sizeof(msg), PJD> + "Descriptor %d (remote in) is %s, but should be %s.", PJD> + fd, dtype2str(mode), dtype2str(S_IFSOCK)); PJD> + break; PJD> + } PJD> + } else if (res->hr_role == HAST_ROLE_SECONDARY && PJD> + fd == proto_descriptor(res->hr_remoteout)) { PJD> + if (!isopen) { PJD> + snprintf(msg, sizeof(msg), PJD> + "Descriptor %d (remote out) is closed, but should be open.", PJD> + fd); PJD> + break; PJD> + } PJD> + if (!S_ISSOCK(mode)) { PJD> + snprintf(msg, sizeof(msg), PJD> + "Descriptor %d (remote out) is %s, but should be %s.", PJD> + fd, dtype2str(mode), dtype2str(S_IFSOCK)); PJD> + break; PJD> + } PJD> + } else { PJD> + if (isopen) { PJD> + snprintf(msg, sizeof(msg), PJD> + "Descriptor %d is open (%s), but should be closed.", PJD> + fd, dtype2str(mode)); PJD> + break; PJD> + } PJD> + } PJD> + } PJD> + if (msg[0] != '\0') { PJD> + pjdlog_init(pjdlogmode); PJD> + pjdlog_prefix_set("[%s] (%s) ", res->hr_name, PJD> + role2str(res->hr_role)); PJD> + PJDLOG_ABORT("%s", msg); PJD> + } PJD> +} PJD> + PJD> static void PJD> child_exit_log(unsigned int pid, int status) PJD> { PJD> Modified: head/sbin/hastd/hastd.h PJD> ============================================================================== PJD> --- head/sbin/hastd/hastd.h Fri Jan 28 21:52:37 2011 (r218043) PJD> +++ head/sbin/hastd/hastd.h Fri Jan 28 21:56:47 2011 (r218044) PJD> @@ -44,6 +44,7 @@ extern bool sigexit_received; PJD> extern struct pidfh *pfh; PJD> PJD> void descriptors_cleanup(struct hast_resource *res); PJD> +void descriptors_assert(const struct hast_resource *res, int pjdlogmode); PJD> PJD> void hastd_primary(struct hast_resource *res); PJD> void hastd_secondary(struct hast_resource *res, struct nv *nvin); PJD> _______________________________________________ PJD> svn-src-all@freebsd.org mailing list PJD> http://lists.freebsd.org/mailman/listinfo/svn-src-all PJD> To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org" -- Mikolaj Golub