~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to clients/upssched.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
61
61
        int     verbose = 0;            /* use for debugging */
62
62
 
63
63
        struct  sigaction sa;
64
 
        sigset_t us_sigmask;
 
64
        sigset_t nut_upssched_sigmask;
65
65
 
66
66
        /* ups name and notify type (string) as received from upsmon */
67
67
        const   char    *upsname, *notify_type;
144
144
#endif
145
145
 
146
146
                unlink(pipefn);
147
 
                exit(0);
 
147
                exit(EXIT_SUCCESS);
148
148
        }
149
149
 
150
150
        emptyctr = 0;
259
259
        }
260
260
}
261
261
 
262
 
static int open_sock(const char *fn)
 
262
static int open_sock(void)
263
263
{
264
264
        int     ret, fd;
265
265
        struct  sockaddr_un     ssaddr;
274
274
 
275
275
        unlink(pipefn);
276
276
 
277
 
        /* group gets access so upsd can be a different user but same group */
278
277
        umask(0007);
279
278
 
280
279
        ret = bind(fd, (struct sockaddr *) &ssaddr, sizeof ssaddr);
334
333
 
335
334
        ret = write(conn->fd, buf, strlen(buf));
336
335
 
337
 
        if (ret != strlen(buf)) {
 
336
        if ((ret < 1) || (ret != (int) strlen(buf))) {
338
337
                upsdebugx(2, "write to fd %d failed", conn->fd);
339
338
 
340
339
                close(conn->fd);
350
349
{
351
350
        int     acc, ret;
352
351
        struct  conn_t  *tmp, *last;
353
 
        struct  sockaddr_un sa;
 
352
        struct  sockaddr_un     saddr;
354
353
        socklen_t       salen;
355
354
 
356
 
        salen = sizeof(sa);
357
 
        acc = accept(sockfd, (struct sockaddr *) &sa, &salen);
 
355
        salen = sizeof(saddr);
 
356
        acc = accept(sockfd, (struct sockaddr *) &saddr, &salen);
358
357
 
359
358
        if (acc < 0) {
360
359
                upslog(LOG_ERR, "accept on unix fd failed");
467
466
 
468
467
                if (ret == -1) {
469
468
                        upslogx(LOG_NOTICE, "Parse error on sock: %s",
470
 
                                &conn->ctx.errmsg);
 
469
                                conn->ctx.errmsg);
471
470
 
472
471
                        return 0;       /* nothing parsed */
473
472
                }
517
516
        dup(0);
518
517
        dup(0);
519
518
 
520
 
        pipefd = open_sock(pipefn);
 
519
        pipefd = open_sock();
521
520
 
522
521
        if (verbose)
523
522
                upslogx(LOG_INFO, "Timer daemon started");
546
545
 
547
546
                        if (tmp->fd > maxfd)
548
547
                                maxfd = tmp->fd;
549
 
                }
 
548
                }
550
549
 
551
550
                ret = select(maxfd + 1, &rfds, NULL, NULL, &tv);
552
551
 
577
576
 
578
577
/* --- 'client' functions --- */
579
578
 
580
 
static int try_connect(const char *pipefn)
 
579
static int try_connect(void)
581
580
{
582
581
        int     pipefd, ret;
583
 
        struct  sockaddr_un sa;
 
582
        struct  sockaddr_un saddr;
584
583
 
585
 
        memset(&sa, '\0', sizeof(sa));
586
 
        sa.sun_family = AF_UNIX;
587
 
        snprintf(sa.sun_path, sizeof(sa.sun_path), "%s", pipefn);
 
584
        memset(&sa, '\0', sizeof(saddr));
 
585
        saddr.sun_family = AF_UNIX;
 
586
        snprintf(saddr.sun_path, sizeof(saddr.sun_path), "%s", pipefn);
588
587
 
589
588
        pipefd = socket(AF_UNIX, SOCK_STREAM, 0);
590
589
 
591
590
        if (pipefd == -1)
592
591
                fatal("socket");
593
592
 
594
 
        ret = connect(pipefd, (const struct sockaddr *) &sa, sizeof(sa));
 
593
        ret = connect(pipefd, (const struct sockaddr *) &saddr, sizeof(saddr));
595
594
 
596
595
        if (ret != -1)
597
596
                return pipefd;
606
605
}
607
606
 
608
607
/* try to connect to bg process, and start one if necessary */
609
 
static int check_parent(const char *cmd)
 
608
static int check_parent(const char *cmd, const char *arg2)
610
609
{
611
610
        int     pipefd, lockfd, tries = 0;
612
611
 
613
612
        for (tries = 0; tries < MAX_TRIES; tries++) {
614
613
 
615
 
                pipefd = try_connect(pipefn);
 
614
                pipefd = try_connect();
616
615
 
617
616
                if (pipefd != -1)
618
617
                        return pipefd;
620
619
                /* timer daemon isn't running */
621
620
 
622
621
                /* it's not running, so there's nothing to cancel */
623
 
                if (!strcmp(cmd, "CANCEL"))
 
622
                if (!strcmp(cmd, "CANCEL") && (arg2 == NULL))
624
623
                        return PARENT_UNNECESSARY;
625
624
 
 
625
                /* arg2 non-NULL means there is a cancel action available */
 
626
 
626
627
                /* we need to start the daemon, so try to get the lock */
627
628
 
628
629
                lockfd = get_lock(lockfn);
642
643
        }
643
644
 
644
645
        upslog(LOG_ERR, "Failed to connect to parent and failed to create parent");
645
 
        exit(1);
 
646
        exit(EXIT_FAILURE);
646
647
}
647
648
 
648
649
static void read_timeout(int sig)
653
654
 
654
655
static void setup_sigalrm(void)
655
656
{
656
 
        sigemptyset(&us_sigmask);
657
 
        sa.sa_mask = us_sigmask;
 
657
        sigemptyset(&nut_upssched_sigmask);
 
658
        sa.sa_mask = nut_upssched_sigmask;
658
659
        sa.sa_flags = 0;
659
660
        sa.sa_handler = read_timeout;
660
661
        sigaction(SIGALRM, &sa, NULL);
683
684
 
684
685
        for (i = 0; i < MAX_TRIES; i++) {
685
686
 
686
 
                pipefd = check_parent(cmd);
 
687
                pipefd = check_parent(cmd, arg2);
687
688
 
688
689
                if (pipefd == PARENT_STARTED) {
689
690
 
701
702
                ret = write(pipefd, buf, strlen(buf));
702
703
 
703
704
                /* if we can't send the whole thing, loop back and try again */
704
 
                if (ret != strlen(buf)) {
 
705
                if ((ret < 1) || (ret != (int) strlen(buf))) {
705
706
                        upslogx(LOG_ERR, "write failed, trying again");
706
707
                        close(pipefd);
707
708
                        continue;
708
709
                }
709
710
 
 
711
                /* ugh - probably should use select here... */
710
712
                setup_sigalrm();
711
713
 
712
714
                alarm(2);
784
786
                if (verbose)    
785
787
                        upslogx(LOG_INFO, "Executing command: %s", ca1);
786
788
 
787
 
                exec_cmd((char *) ca1);
 
789
                exec_cmd(ca1);
788
790
                return;
789
791
        }
790
792
 
833
835
}
834
836
 
835
837
/* called for fatal errors in parseconf like malloc failures */
836
 
void upssched_err(const char *errmsg)
 
838
static void upssched_err(const char *errmsg)
837
839
{
838
840
        upslogx(LOG_ERR, "Fatal error in parseconf(upssched.conf): %s", errmsg);
839
841
}
863
865
                        continue;
864
866
 
865
867
                if (!conf_arg(ctx.numargs, ctx.arglist)) {
866
 
                        int     i;
 
868
                        unsigned int    i;
867
869
                        char    errmsg[SMALLBUF];
868
870
 
869
871
                        snprintf(errmsg, sizeof(errmsg), 
873
875
                                snprintfcat(errmsg, sizeof(errmsg), " %s", 
874
876
                                        ctx.arglist[i]);
875
877
 
876
 
                        upslogx(LOG_WARNING, errmsg);
 
878
                        upslogx(LOG_WARNING, "%s", errmsg);
877
879
                }
878
880
        }
879
881
 
894
896
        if ((!upsname) || (!notify_type)) {
895
897
                printf("Error: UPSNAME and NOTIFYTYPE must be set.\n");
896
898
                printf("This program should only be run from upsmon.\n");
897
 
                exit(1);
 
899
                exit(EXIT_FAILURE);
898
900
        }
899
901
 
900
902
        /* see if this matches anything in the config file */
901
903
        checkconf();
902
904
 
903
 
        return 0;
 
905
        exit(EXIT_SUCCESS);
904
906
}