Deleted Added
full compact
primary.c (218217) primary.c (218218)
1/*-
2 * Copyright (c) 2009 The FreeBSD Foundation
3 * Copyright (c) 2010 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 The FreeBSD Foundation
3 * Copyright (c) 2010 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/primary.c 218217 2011-02-03 11:33:32Z pjd $");
32__FBSDID("$FreeBSD: head/sbin/hastd/primary.c 218218 2011-02-03 11:39:49Z pjd $");
33
34#include <sys/types.h>
35#include <sys/time.h>
36#include <sys/bio.h>
37#include <sys/disk.h>
38#include <sys/refcount.h>
39#include <sys/stat.h>
40

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

483 * anything.
484 */
485 res->hr_primary_localcnt = 1;
486 res->hr_primary_remotecnt = 0;
487 if (metadata_write(res) < 0)
488 exit(EX_NOINPUT);
489}
490
33
34#include <sys/types.h>
35#include <sys/time.h>
36#include <sys/bio.h>
37#include <sys/disk.h>
38#include <sys/refcount.h>
39#include <sys/stat.h>
40

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

483 * anything.
484 */
485 res->hr_primary_localcnt = 1;
486 res->hr_primary_remotecnt = 0;
487 if (metadata_write(res) < 0)
488 exit(EX_NOINPUT);
489}
490
491static int
492primary_connect(struct hast_resource *res, struct proto_conn **connp)
493{
494 struct proto_conn *conn;
495 int16_t val;
496
497 val = 1;
498 if (proto_send(res->hr_conn, &val, sizeof(val)) < 0) {
499 primary_exit(EX_TEMPFAIL,
500 "Unable to send connection request to parent");
501 }
502 if (proto_recv(res->hr_conn, &val, sizeof(val)) < 0) {
503 primary_exit(EX_TEMPFAIL,
504 "Unable to receive reply to connection request from parent");
505 }
506 if (val != 0) {
507 errno = val;
508 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
509 res->hr_remoteaddr);
510 return (-1);
511 }
512 if (proto_connection_recv(res->hr_conn, true, &conn) < 0) {
513 primary_exit(EX_TEMPFAIL,
514 "Unable to receive connection from parent");
515 }
516 if (proto_connect_wait(conn, HAST_TIMEOUT) < 0) {
517 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
518 res->hr_remoteaddr);
519 proto_close(conn);
520 return (-1);
521 }
522 /* Error in setting timeout is not critical, but why should it fail? */
523 if (proto_timeout(conn, res->hr_timeout) < 0)
524 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
525
526 *connp = conn;
527
528 return (0);
529}
530
491static bool
492init_remote(struct hast_resource *res, struct proto_conn **inp,
493 struct proto_conn **outp)
494{
495 struct proto_conn *in, *out;
496 struct nv *nvout, *nvin;
497 const unsigned char *token;
498 unsigned char *map;

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

503 size_t size;
504
505 PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
506 PJDLOG_ASSERT(real_remote(res));
507
508 in = out = NULL;
509 errmsg = NULL;
510
531static bool
532init_remote(struct hast_resource *res, struct proto_conn **inp,
533 struct proto_conn **outp)
534{
535 struct proto_conn *in, *out;
536 struct nv *nvout, *nvin;
537 const unsigned char *token;
538 unsigned char *map;

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

543 size_t size;
544
545 PJDLOG_ASSERT((inp == NULL && outp == NULL) || (inp != NULL && outp != NULL));
546 PJDLOG_ASSERT(real_remote(res));
547
548 in = out = NULL;
549 errmsg = NULL;
550
511 /* Prepare outgoing connection with remote node. */
512 if (proto_client(res->hr_remoteaddr, &out) < 0) {
513 primary_exit(EX_TEMPFAIL,
514 "Unable to create outgoing connection to %s",
515 res->hr_remoteaddr);
516 }
517 /* Try to connect, but accept failure. */
518 if (proto_connect(out, HAST_TIMEOUT) < 0) {
519 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
520 res->hr_remoteaddr);
521 goto close;
522 }
523 /* Error in setting timeout is not critical, but why should it fail? */
524 if (proto_timeout(out, res->hr_timeout) < 0)
525 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
551 if (primary_connect(res, &out) == -1)
552 return (false);
553
526 /*
527 * First handshake step.
528 * Setup outgoing connection with remote node.
529 */
530 nvout = nv_alloc();
531 nv_add_string(nvout, res->hr_name, "resource");
532 if (nv_error(nvout) != 0) {
533 pjdlog_common(LOG_WARNING, 0, nv_error(nvout),

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

571 }
572 bcopy(token, res->hr_token, sizeof(res->hr_token));
573 nv_free(nvin);
574
575 /*
576 * Second handshake step.
577 * Setup incoming connection with remote node.
578 */
554 /*
555 * First handshake step.
556 * Setup outgoing connection with remote node.
557 */
558 nvout = nv_alloc();
559 nv_add_string(nvout, res->hr_name, "resource");
560 if (nv_error(nvout) != 0) {
561 pjdlog_common(LOG_WARNING, 0, nv_error(nvout),

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

599 }
600 bcopy(token, res->hr_token, sizeof(res->hr_token));
601 nv_free(nvin);
602
603 /*
604 * Second handshake step.
605 * Setup incoming connection with remote node.
606 */
579 if (proto_client(res->hr_remoteaddr, &in) < 0) {
580 primary_exit(EX_TEMPFAIL,
581 "Unable to create incoming connection to %s",
582 res->hr_remoteaddr);
583 }
584 /* Try to connect, but accept failure. */
585 if (proto_connect(in, HAST_TIMEOUT) < 0) {
586 pjdlog_errno(LOG_WARNING, "Unable to connect to %s",
587 res->hr_remoteaddr);
607 if (primary_connect(res, &in) == -1)
588 goto close;
608 goto close;
589 }
590 /* Error in setting timeout is not critical, but why should it fail? */
591 if (proto_timeout(in, res->hr_timeout) < 0)
592 pjdlog_errno(LOG_WARNING, "Unable to set connection timeout");
609
593 nvout = nv_alloc();
594 nv_add_string(nvout, res->hr_name, "resource");
595 nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token),
596 "token");
597 if (res->hr_resuid == 0) {
598 /*
599 * The resuid field was not yet initialized.
600 * Because we do synchronization inside init_resuid(), it is

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

787void
788hastd_primary(struct hast_resource *res)
789{
790 pthread_t td;
791 pid_t pid;
792 int error, mode;
793
794 /*
610 nvout = nv_alloc();
611 nv_add_string(nvout, res->hr_name, "resource");
612 nv_add_uint8_array(nvout, res->hr_token, sizeof(res->hr_token),
613 "token");
614 if (res->hr_resuid == 0) {
615 /*
616 * The resuid field was not yet initialized.
617 * Because we do synchronization inside init_resuid(), it is

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

804void
805hastd_primary(struct hast_resource *res)
806{
807 pthread_t td;
808 pid_t pid;
809 int error, mode;
810
811 /*
795 * Create communication channel between parent and child.
812 * Create communication channel for sending control commands from
813 * parent to child.
796 */
797 if (proto_client("socketpair://", &res->hr_ctrl) < 0) {
798 /* TODO: There's no need for this to be fatal error. */
799 KEEP_ERRNO((void)pidfile_remove(pfh));
800 pjdlog_exit(EX_OSERR,
801 "Unable to create control sockets between parent and child");
802 }
803 /*
814 */
815 if (proto_client("socketpair://", &res->hr_ctrl) < 0) {
816 /* TODO: There's no need for this to be fatal error. */
817 KEEP_ERRNO((void)pidfile_remove(pfh));
818 pjdlog_exit(EX_OSERR,
819 "Unable to create control sockets between parent and child");
820 }
821 /*
804 * Create communication channel between child and parent.
822 * Create communication channel for sending events from child to parent.
805 */
806 if (proto_client("socketpair://", &res->hr_event) < 0) {
807 /* TODO: There's no need for this to be fatal error. */
808 KEEP_ERRNO((void)pidfile_remove(pfh));
809 pjdlog_exit(EX_OSERR,
810 "Unable to create event sockets between child and parent");
811 }
823 */
824 if (proto_client("socketpair://", &res->hr_event) < 0) {
825 /* TODO: There's no need for this to be fatal error. */
826 KEEP_ERRNO((void)pidfile_remove(pfh));
827 pjdlog_exit(EX_OSERR,
828 "Unable to create event sockets between child and parent");
829 }
830 /*
831 * Create communication channel for sending connection requests from
832 * child to parent.
833 */
834 if (proto_client("socketpair://", &res->hr_conn) < 0) {
835 /* TODO: There's no need for this to be fatal error. */
836 KEEP_ERRNO((void)pidfile_remove(pfh));
837 pjdlog_exit(EX_OSERR,
838 "Unable to create connection sockets between child and parent");
839 }
812
813 pid = fork();
814 if (pid < 0) {
815 /* TODO: There's no need for this to be fatal error. */
816 KEEP_ERRNO((void)pidfile_remove(pfh));
817 pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
818 }
819
820 if (pid > 0) {
821 /* This is parent. */
822 /* Declare that we are receiver. */
823 proto_recv(res->hr_event, NULL, 0);
840
841 pid = fork();
842 if (pid < 0) {
843 /* TODO: There's no need for this to be fatal error. */
844 KEEP_ERRNO((void)pidfile_remove(pfh));
845 pjdlog_exit(EX_TEMPFAIL, "Unable to fork");
846 }
847
848 if (pid > 0) {
849 /* This is parent. */
850 /* Declare that we are receiver. */
851 proto_recv(res->hr_event, NULL, 0);
852 proto_recv(res->hr_conn, NULL, 0);
824 /* Declare that we are sender. */
825 proto_send(res->hr_ctrl, NULL, 0);
826 res->hr_workerpid = pid;
827 return;
828 }
829
830 gres = res;
831 mode = pjdlog_mode_get();
832
833 /* Declare that we are sender. */
834 proto_send(res->hr_event, NULL, 0);
853 /* Declare that we are sender. */
854 proto_send(res->hr_ctrl, NULL, 0);
855 res->hr_workerpid = pid;
856 return;
857 }
858
859 gres = res;
860 mode = pjdlog_mode_get();
861
862 /* Declare that we are sender. */
863 proto_send(res->hr_event, NULL, 0);
864 proto_send(res->hr_conn, NULL, 0);
835 /* Declare that we are receiver. */
836 proto_recv(res->hr_ctrl, NULL, 0);
837 descriptors_cleanup(res);
838
839 descriptors_assert(res, mode);
840
841 pjdlog_init(mode);
842 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));

--- 1225 unchanged lines hidden ---
865 /* Declare that we are receiver. */
866 proto_recv(res->hr_ctrl, NULL, 0);
867 descriptors_cleanup(res);
868
869 descriptors_assert(res, mode);
870
871 pjdlog_init(mode);
872 pjdlog_prefix_set("[%s] (%s) ", res->hr_name, role2str(res->hr_role));

--- 1225 unchanged lines hidden ---