~ubuntu-branches/ubuntu/precise/corosync/precise-proposed

« back to all changes in this revision

Viewing changes to lib/coroipcc.c

  • Committer: Bazaar Package Importer
  • Author(s): Andres Rodriguez
  • Date: 2010-07-02 01:24:53 UTC
  • mfrom: (1.1.4 upstream) (5.1.9 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100702012453-qky79tg6hjly2dmq
Tags: 1.2.1-1ubuntu1
* Merge from debian unstable (LP: #600900). Remaining changes:
  - Raised consensus time out to 5000ms
  - debian/control, debian/rules: Removing now-unnecessary quilt build-dep

Show diffs side-by-side

added added

removed removed

Lines of Context:
71
71
 
72
72
#include "util.h"
73
73
 
 
74
/*
 
75
 * Define sem_wait timeout (real timeout will be (n-1;n) )
 
76
 */
 
77
#define IPC_SEMWAIT_TIMEOUT 2
 
78
 
74
79
struct ipc_instance {
75
80
        int fd;
76
81
#if _POSIX_THREAD_PROCESS_SHARED < 1
456
461
{
457
462
#if _POSIX_THREAD_PROCESS_SHARED < 1
458
463
        struct sembuf sop;
 
464
#else
 
465
        struct timespec timeout;
 
466
        struct pollfd pfd;
459
467
#endif
460
468
        coroipc_response_header_t *response_header;
461
469
        int res;
462
470
 
463
471
#if _POSIX_THREAD_PROCESS_SHARED > 0
464
472
retry_semwait:
465
 
        res = sem_wait (&ipc_instance->control_buffer->sem1);
 
473
        timeout.tv_sec = time(NULL) + IPC_SEMWAIT_TIMEOUT;
 
474
        timeout.tv_nsec = 0;
 
475
 
 
476
        res = sem_timedwait (&ipc_instance->control_buffer->sem1, &timeout);
 
477
        if (res == -1 && errno == ETIMEDOUT) {
 
478
                pfd.fd = ipc_instance->fd;
 
479
                pfd.events = 0;
 
480
 
 
481
                poll (&pfd, 1, 0);
 
482
                if (pfd.revents == POLLERR || pfd.revents == POLLHUP) {
 
483
                        return (CS_ERR_LIBRARY);
 
484
                }
 
485
 
 
486
                goto retry_semwait;
 
487
        }
 
488
 
466
489
        if (res == -1 && errno == EINTR) {
467
490
                goto retry_semwait;
468
491
        }
505
528
{
506
529
#if _POSIX_THREAD_PROCESS_SHARED < 1
507
530
        struct sembuf sop;
 
531
#else
 
532
        struct timespec timeout;
 
533
        struct pollfd pfd;
508
534
#endif
509
535
        int res;
510
536
 
511
537
#if _POSIX_THREAD_PROCESS_SHARED > 0
512
538
retry_semwait:
513
 
        res = sem_wait (&ipc_instance->control_buffer->sem1);
 
539
        timeout.tv_sec = time(NULL) + IPC_SEMWAIT_TIMEOUT;
 
540
        timeout.tv_nsec = 0;
 
541
 
 
542
        res = sem_timedwait (&ipc_instance->control_buffer->sem1, &timeout);
 
543
        if (res == -1 && errno == ETIMEDOUT) {
 
544
                pfd.fd = ipc_instance->fd;
 
545
                pfd.events = 0;
 
546
 
 
547
                poll (&pfd, 1, 0);
 
548
                if (pfd.revents == POLLERR || pfd.revents == POLLHUP) {
 
549
                        return (CS_ERR_LIBRARY);
 
550
                }
 
551
 
 
552
                goto retry_semwait;
 
553
        }
 
554
 
514
555
        if (res == -1 && errno == EINTR) {
515
556
                goto retry_semwait;
516
557
        }
609
650
        sys_res = connect (request_fd, (struct sockaddr *)&address,
610
651
                COROSYNC_SUN_LEN(&address));
611
652
        if (sys_res == -1) {
612
 
                close (request_fd);
613
 
                return (CS_ERR_TRY_AGAIN);
 
653
                res = CS_ERR_TRY_AGAIN;
 
654
                goto error_connect;
614
655
        }
615
656
 
616
657
        res = memory_map (
618
659
                "control_buffer-XXXXXX",
619
660
                (void *)&ipc_instance->control_buffer,
620
661
                8192);
 
662
        if (res == -1) {
 
663
                res = CS_ERR_LIBRARY;
 
664
                goto error_connect;
 
665
        }
621
666
 
622
667
        res = memory_map (
623
668
                request_map_path,
624
669
                "request_buffer-XXXXXX",
625
670
                (void *)&ipc_instance->request_buffer,
626
671
                request_size);
 
672
        if (res == -1) {
 
673
                res = CS_ERR_LIBRARY;
 
674
                goto error_request_buffer;
 
675
        }
627
676
 
628
677
        res = memory_map (
629
678
                response_map_path,
630
679
                "response_buffer-XXXXXX",
631
680
                (void *)&ipc_instance->response_buffer,
632
681
                response_size);
 
682
        if (res == -1) {
 
683
                res = CS_ERR_LIBRARY;
 
684
                goto error_response_buffer;
 
685
        }
633
686
 
634
687
        res = circular_memory_map (
635
688
                dispatch_map_path,
636
689
                "dispatch_buffer-XXXXXX",
637
690
                (void *)&ipc_instance->dispatch_buffer,
638
691
                dispatch_size);
 
692
        if (res == -1) {
 
693
                res = CS_ERR_LIBRARY;
 
694
                goto error_dispatch_buffer;
 
695
        }
639
696
 
640
697
#if _POSIX_THREAD_PROCESS_SHARED > 0
641
698
        sem_init (&ipc_instance->control_buffer->sem0, 1, 0);
661
718
                 * an existing shared memory segment for which we have access
662
719
                 */
663
720
                if (errno != EEXIST && errno != EACCES) {
664
 
                        goto res_exit;
 
721
                        goto error_exit;
665
722
                }
666
723
        }
667
724
 
668
725
        semun.val = 0;
669
726
        res = semctl (ipc_instance->semid, 0, SETVAL, semun);
670
727
        if (res != 0) {
671
 
                goto res_exit;
 
728
                goto error_exit;
672
729
        }
673
730
 
674
731
        res = semctl (ipc_instance->semid, 1, SETVAL, semun);
675
732
        if (res != 0) {
676
 
                goto res_exit;
 
733
                goto error_exit;
677
734
        }
678
735
#endif
679
736
 
696
753
 
697
754
        res = socket_send (request_fd, &req_setup, sizeof (mar_req_setup_t));
698
755
        if (res != CS_OK) {
699
 
                goto res_exit;
 
756
                goto error_exit;
700
757
        }
701
758
        res = socket_recv (request_fd, &res_setup, sizeof (mar_res_setup_t));
702
759
        if (res != CS_OK) {
703
 
                goto res_exit;
 
760
                goto error_exit;
704
761
        }
705
762
 
706
763
        ipc_instance->fd = request_fd;
707
764
        ipc_instance->flow_control_state = 0;
708
765
 
709
766
        if (res_setup.error == CS_ERR_TRY_AGAIN) {
710
 
                goto res_exit;
 
767
                res = res_setup.error;
 
768
                goto error_exit;
711
769
        }
712
770
 
713
771
        ipc_instance->control_size = 8192;
721
779
 
722
780
        return (res_setup.error);
723
781
 
724
 
res_exit:
725
 
        close (request_fd);
 
782
error_exit:
726
783
#if _POSIX_THREAD_PROCESS_SHARED < 1
727
784
        if (ipc_instance->semid > 0)
728
785
                semctl (ipc_instance->semid, 0, IPC_RMID);
729
786
#endif
730
 
        return (res_setup.error);
 
787
        memory_unmap (ipc_instance->dispatch_buffer, dispatch_size);
 
788
error_dispatch_buffer:
 
789
        memory_unmap (ipc_instance->response_buffer, response_size);
 
790
error_response_buffer:
 
791
        memory_unmap (ipc_instance->request_buffer, request_size);
 
792
error_request_buffer:
 
793
        memory_unmap (ipc_instance->control_buffer, 8192);
 
794
error_connect:
 
795
        close (request_fd);
 
796
 
 
797
        hdb_handle_destroy (&ipc_hdb, *handle);
 
798
        hdb_handle_put (&ipc_hdb, *handle);
 
799
 
 
800
        return (res);
731
801
}
732
802
 
733
803
cs_error_t