Deleted Added
full compact
hastd.c (218138) hastd.c (218218)
1/*-
2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4 * All rights reserved.
5 *
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
8 *

--- 15 unchanged lines hidden (view full) ---

24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 2009-2010 The FreeBSD Foundation
3 * Copyright (c) 2010-2011 Pawel Jakub Dawidek <pjd@FreeBSD.org>
4 * All rights reserved.
5 *
6 * This software was developed by Pawel Jakub Dawidek under sponsorship from
7 * the FreeBSD Foundation.
8 *

--- 15 unchanged lines hidden (view full) ---

24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sbin/hastd/hastd.c 218138 2011-01-31 18:32:17Z pjd $");
32__FBSDID("$FreeBSD: head/sbin/hastd/hastd.c 218218 2011-02-03 11:39:49Z pjd $");
33
34#include <sys/param.h>
35#include <sys/linker.h>
36#include <sys/module.h>
37#include <sys/stat.h>
38#include <sys/wait.h>
39
40#include <err.h>

--- 168 unchanged lines hidden (view full) ---

209 break;
210 }
211 if (!S_ISSOCK(mode)) {
212 snprintf(msg, sizeof(msg),
213 "Descriptor %d (ctrl) is %s, but should be %s.",
214 fd, dtype2str(mode), dtype2str(S_IFSOCK));
215 break;
216 }
33
34#include <sys/param.h>
35#include <sys/linker.h>
36#include <sys/module.h>
37#include <sys/stat.h>
38#include <sys/wait.h>
39
40#include <err.h>

--- 168 unchanged lines hidden (view full) ---

209 break;
210 }
211 if (!S_ISSOCK(mode)) {
212 snprintf(msg, sizeof(msg),
213 "Descriptor %d (ctrl) is %s, but should be %s.",
214 fd, dtype2str(mode), dtype2str(S_IFSOCK));
215 break;
216 }
217 } else if (fd == proto_descriptor(res->hr_conn)) {
218 if (!isopen) {
219 snprintf(msg, sizeof(msg),
220 "Descriptor %d (conn) is closed, but should be open.",
221 fd);
222 break;
223 }
224 if (!S_ISSOCK(mode)) {
225 snprintf(msg, sizeof(msg),
226 "Descriptor %d (conn) is %s, but should be %s.",
227 fd, dtype2str(mode), dtype2str(S_IFSOCK));
228 break;
229 }
217 } else if (res->hr_role == HAST_ROLE_SECONDARY &&
218 fd == proto_descriptor(res->hr_remotein)) {
219 if (!isopen) {
220 snprintf(msg, sizeof(msg),
221 "Descriptor %d (remote in) is closed, but should be open.",
222 fd);
223 break;
224 }

--- 572 unchanged lines hidden (view full) ---

797 nv_free(nvout);
798 if (nverr != NULL)
799 nv_free(nverr);
800 proto_close(conn);
801 pjdlog_prefix_set("%s", "");
802}
803
804static void
230 } else if (res->hr_role == HAST_ROLE_SECONDARY &&
231 fd == proto_descriptor(res->hr_remotein)) {
232 if (!isopen) {
233 snprintf(msg, sizeof(msg),
234 "Descriptor %d (remote in) is closed, but should be open.",
235 fd);
236 break;
237 }

--- 572 unchanged lines hidden (view full) ---

810 nv_free(nvout);
811 if (nverr != NULL)
812 nv_free(nverr);
813 proto_close(conn);
814 pjdlog_prefix_set("%s", "");
815}
816
817static void
818connection_migrate(struct hast_resource *res)
819{
820 struct proto_conn *conn;
821 int16_t val = 0;
822
823 if (proto_recv(res->hr_conn, &val, sizeof(val)) < 0) {
824 pjdlog_errno(LOG_WARNING,
825 "Unable to receive connection command");
826 return;
827 }
828 if (proto_client(res->hr_remoteaddr, &conn) < 0) {
829 val = errno;
830 pjdlog_errno(LOG_WARNING,
831 "Unable to create outgoing connection to %s",
832 res->hr_remoteaddr);
833 goto out;
834 }
835 if (proto_connect(conn, -1) < 0) {
836 val = errno;
837 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
838 res->hr_remoteaddr);
839 proto_close(conn);
840 goto out;
841 }
842 val = 0;
843out:
844 if (proto_send(res->hr_conn, &val, sizeof(val)) < 0) {
845 pjdlog_errno(LOG_WARNING,
846 "Unable to send reply to connection request");
847 }
848 if (val == 0 && proto_connection_send(res->hr_conn, conn) < 0)
849 pjdlog_errno(LOG_WARNING, "Unable to send connection");
850}
851
852static void
805main_loop(void)
806{
807 struct hast_resource *res;
808 struct timeval seltimeout;
809 struct timespec sigtimeout;
810 int fd, maxfd, ret, signo;
811 sigset_t mask;
812 fd_set rfds;

--- 40 unchanged lines hidden (view full) ---

853 FD_SET(fd, &rfds);
854 fd = proto_descriptor(cfg->hc_listenconn);
855 PJDLOG_ASSERT(fd >= 0);
856 FD_SET(fd, &rfds);
857 maxfd = fd > maxfd ? fd : maxfd;
858 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
859 if (res->hr_event == NULL)
860 continue;
853main_loop(void)
854{
855 struct hast_resource *res;
856 struct timeval seltimeout;
857 struct timespec sigtimeout;
858 int fd, maxfd, ret, signo;
859 sigset_t mask;
860 fd_set rfds;

--- 40 unchanged lines hidden (view full) ---

901 FD_SET(fd, &rfds);
902 fd = proto_descriptor(cfg->hc_listenconn);
903 PJDLOG_ASSERT(fd >= 0);
904 FD_SET(fd, &rfds);
905 maxfd = fd > maxfd ? fd : maxfd;
906 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
907 if (res->hr_event == NULL)
908 continue;
909 PJDLOG_ASSERT(res->hr_conn != NULL);
861 fd = proto_descriptor(res->hr_event);
862 PJDLOG_ASSERT(fd >= 0);
863 FD_SET(fd, &rfds);
864 maxfd = fd > maxfd ? fd : maxfd;
910 fd = proto_descriptor(res->hr_event);
911 PJDLOG_ASSERT(fd >= 0);
912 FD_SET(fd, &rfds);
913 maxfd = fd > maxfd ? fd : maxfd;
914 if (res->hr_role == HAST_ROLE_PRIMARY) {
915 /* Only primary workers asks for connections. */
916 fd = proto_descriptor(res->hr_conn);
917 PJDLOG_ASSERT(fd >= 0);
918 FD_SET(fd, &rfds);
919 maxfd = fd > maxfd ? fd : maxfd;
920 }
865 }
866
867 PJDLOG_ASSERT(maxfd + 1 <= (int)FD_SETSIZE);
868 ret = select(maxfd + 1, &rfds, NULL, NULL, &seltimeout);
869 if (ret == 0)
870 hook_check();
871 else if (ret == -1) {
872 if (errno == EINTR)

--- 4 unchanged lines hidden (view full) ---

877
878 if (FD_ISSET(proto_descriptor(cfg->hc_controlconn), &rfds))
879 control_handle(cfg);
880 if (FD_ISSET(proto_descriptor(cfg->hc_listenconn), &rfds))
881 listen_accept();
882 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
883 if (res->hr_event == NULL)
884 continue;
921 }
922
923 PJDLOG_ASSERT(maxfd + 1 <= (int)FD_SETSIZE);
924 ret = select(maxfd + 1, &rfds, NULL, NULL, &seltimeout);
925 if (ret == 0)
926 hook_check();
927 else if (ret == -1) {
928 if (errno == EINTR)

--- 4 unchanged lines hidden (view full) ---

933
934 if (FD_ISSET(proto_descriptor(cfg->hc_controlconn), &rfds))
935 control_handle(cfg);
936 if (FD_ISSET(proto_descriptor(cfg->hc_listenconn), &rfds))
937 listen_accept();
938 TAILQ_FOREACH(res, &cfg->hc_resources, hr_next) {
939 if (res->hr_event == NULL)
940 continue;
941 PJDLOG_ASSERT(res->hr_conn != NULL);
885 if (FD_ISSET(proto_descriptor(res->hr_event), &rfds)) {
886 if (event_recv(res) == 0)
887 continue;
888 /* The worker process exited? */
889 proto_close(res->hr_event);
890 res->hr_event = NULL;
942 if (FD_ISSET(proto_descriptor(res->hr_event), &rfds)) {
943 if (event_recv(res) == 0)
944 continue;
945 /* The worker process exited? */
946 proto_close(res->hr_event);
947 res->hr_event = NULL;
948 proto_close(res->hr_conn);
949 res->hr_conn = NULL;
950 continue;
891 }
951 }
952 if (res->hr_role == HAST_ROLE_PRIMARY &&
953 FD_ISSET(proto_descriptor(res->hr_conn), &rfds)) {
954 connection_migrate(res);
955 }
892 }
893 }
894}
895
896static void
897dummy_sighandler(int sig __unused)
898{
899 /* Nothing to do. */

--- 116 unchanged lines hidden ---
956 }
957 }
958}
959
960static void
961dummy_sighandler(int sig __unused)
962{
963 /* Nothing to do. */

--- 116 unchanged lines hidden ---