示例#1
0
static void mainloop (void) {
    /* data is persistant across calls */
    static struct timeval timestruct;
    static int changequartersec, changesec, changemin, changehour;
    static time_t lasttime, lastmin, lasthour, last4sec, last5sec, last20sec;
    static time_t lastautoadd;
    static time_t last3min, last2min, lastignoredec;
    static int first_loop = 1;
    static ir_uint64 last250ms;

    userinput *pubplist;
    userinput *urehash;
    ir_uint64 xdccsent;
    unsigned int i;
    int highests;
    unsigned int ss;
    upload *ul;
    transfer *tr;
    channel_t *ch;
    xdcc *xd;
    dccchat_t *chat;

    updatecontext();
    gnetwork = NULL;

    if (first_loop)
    {
        /* init if first time called */
        FD_ZERO(&gdata.readset);
        FD_ZERO(&gdata.writeset);
        changehour=changemin=changesec=changequartersec=0;
        gettimeofday(&timestruct, NULL);
        last250ms = gdata.curtimems;
        gdata.curtimems = timeval_to_ms(&timestruct);
        ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR, "Startup"
                " running: %ld ms", (long)(gdata.curtimems - last250ms));
        gdata.curtime = timestruct.tv_sec;
        lasttime=gdata.curtime;
        last250ms = gdata.curtimems;
        lastmin=(lasttime/60)-1;
        lasthour=(lasttime/60/60)-1;
        last4sec = last5sec = last20sec = last2min = last3min = lasttime;
        lastignoredec = lasttime;
        for (ss=0; ss<gdata.networks_online; ss++)
        {
            gdata.networks[ss].lastnotify = lasttime;
            gdata.networks[ss].lastslow = lasttime;
            gdata.networks[ss].server_input_line[0] = '\0';
        }

        gdata.cursendptr = 0;
        lastautoadd = gdata.curtime + 60;

        first_loop = 0;
    }

    updatecontext();

    FD_ZERO(&gdata.readset);
    FD_ZERO(&gdata.writeset);
    FD_ZERO(&gdata.execset);
    highests = 0;
#ifdef USE_CURL
    fetch_multi_fdset(&gdata.readset, &gdata.writeset, &gdata.execset, &highests);
#endif /* USE_CURL */

    highests = irc_select(highests);

    if (!gdata.background)
    {
        FD_SET(fileno(stdin), &gdata.readset);
        highests = max2(highests, fileno(stdin));
    }

    highests = chat_select_fdset(highests);
    highests = t_select_fdset(highests, changequartersec);
    highests = l_select_fdset(highests, changequartersec);
#ifndef WITHOUT_TELNET
    highests = telnet_select_fdset(highests);
#endif /* WITHOUT_TELNET */
#ifndef WITHOUT_HTTP
    highests = h_select_fdset(highests, changequartersec);
#endif /* WITHOUT_HTTP */

    if (gdata.md5build.file_fd != FD_UNUSED)
    {
        assert(gdata.md5build.xpack);
        FD_SET(gdata.md5build.file_fd, &gdata.readset);
        highests = max2(highests, gdata.md5build.file_fd);
    }

    updatecontext();

    if (gdata.debug > 81)
    {
        select_dump("try", highests);
    }

    if (gdata.attop) gotobot();

    tostdout_write();

    gettimeofday(&timestruct, NULL);
    gdata.selecttimems = timeval_to_ms(&timestruct);
    if (ir_kqueue_select(highests+1, &gdata.readset, &gdata.writeset, &gdata.execset) < 0)
    {
        if (errno != EINTR)
        {
            outerror(OUTERROR_TYPE_WARN,"Select returned an error: %s",strerror(errno));
            usleep(10000); /* prevent fast spinning */
        }

        /* data is undefined on error, zero and continue */
        FD_ZERO(&gdata.readset);
        FD_ZERO(&gdata.writeset);
        FD_ZERO(&gdata.execset);
    }

    if (gdata.debug > 81)
    {
        select_dump("got", highests);
    }

    /*----- one second check ----- */

    updatecontext();

    if (gettimeofday(&timestruct, NULL) < 0)
    {
        outerror(OUTERROR_TYPE_CRASH,"gettimeofday() failed! %s\n",strerror(errno));
    }

    gdata.curtimems = timeval_to_ms(&timestruct);
    gdata.curtime = timestruct.tv_sec;
    if (gdata.curtimems > gdata.selecttimems + 1000)
        outerror(OUTERROR_TYPE_WARN, "Iroffer was blocked for %lims",
                 (long)(gdata.curtimems - gdata.selecttimems));

    /* adjust for drift and cpu usage */
    if ((gdata.curtimems > (last250ms+1000)) ||
            (gdata.curtimems < last250ms))
    {
        /* skipped forward or backwards, correct */
        last250ms = gdata.curtimems-250;
    }

    if (gdata.curtimems >= (last250ms+250))
    {
        changequartersec = 1;
        /* note bandwidth limiting requires no drift! */
        last250ms += 250;
    }
    else
    {
        changequartersec = 0;
    }

    changesec = 0;
    if (gdata.curtime != lasttime) {

        if (gdata.curtime < lasttime - MAX_WAKEUP_WARN) {
            outerror(OUTERROR_TYPE_WARN, "System Time Changed Backwards %lim %lis!!\n",
                     (long)(lasttime-gdata.curtime)/60, (long)(lasttime-gdata.curtime)%60);
        }

        if (gdata.curtime > lasttime + MAX_WAKEUP_WARN) {
            outerror(OUTERROR_TYPE_WARN, "System Time Changed Forward or Mainloop Skipped %lim %lis!!\n",
                     (long)(gdata.curtime-lasttime)/60, (long)(gdata.curtime-lasttime)%60);
            if (gdata.debug > 0) {
                dump_slow_context();
            }
        }

        if (gdata.curtime > lasttime + MAX_WAKEUP_ERR) {
            outerror(OUTERROR_TYPE_WARN, "System Time Changed Forward or Mainloop Skipped %lim %lis!!\n",
                     (long)(gdata.curtime-lasttime)/60, (long)(gdata.curtime-lasttime)%60);
            if (gdata.debug > 0)
            {
                dumpcontext();
            }
        }

        lasttime = gdata.curtime;
        changesec = 1;

    }

    if (changesec && lasttime/60/60 != lasthour) {
        lasthour = lasttime/60/60;
        changehour = 1;
    }

    if (changesec && lasttime/60 != lastmin) {
        lastmin = lasttime/60;
        changemin = 1;
    }

    if (gdata.needsshutdown)
    {
        gdata.needsshutdown = 0;
        shutdowniroffer();
    }

    if (gdata.needsreap)
    {
        gdata.needsreap = 0;
        irc_resolved();
    }

#ifdef USE_CURL
    fetch_perform();
#endif /* USE_CURL */

    updatecontext();

    if (changesec) {
        gdata.totaluptime++;
        gdata.xdccsent[(gdata.curtime+1)%XDCC_SENT_SIZE] = 0;
        gdata.xdccrecv[(gdata.curtime+1)%XDCC_SENT_SIZE] = 0;

        xdccsent = 0;
        for (i=0; i<XDCC_SENT_SIZE; i++)
            xdccsent += (ir_uint64)gdata.xdccsum[i];
        if (((float)xdccsent)/XDCC_SENT_SIZE/1024.0 > gdata.sentrecord)
            gdata.sentrecord = ((float)xdccsent)/XDCC_SENT_SIZE/1024.0;
        gdata.xdccsum[(gdata.curtime+1)%XDCC_SENT_SIZE] = 0;

        run_delayed_jobs();
    }

    updatecontext();

    /*----- see if anything waiting on console ----- */
    gdata.needsclear = 0;
    if (!gdata.background && FD_ISSET(fileno(stdin), &gdata.readset))
        parseconsole();

    irc_perform(changesec);
    l_perform(changesec);
    chat_perform();
    t_perform(changesec, changequartersec);
#ifndef WITHOUT_TELNET
    telnet_perform();
#endif /* WITHOUT_TELNET */
#ifndef WITHOUT_HTTP
    h_perform(changesec, changequartersec);
#endif /* WITHOUT_HTTP */

    /*----- time for a delayed shutdown? ----- */
    if (changesec && gdata.delayedshutdown)
    {
        if (!irlist_size(&gdata.trans))
        {
            ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR,
                    "Delayed Shutdown Activated, No Transfers Remaining");
            shutdowniroffer();
        }
    }

    updatecontext();
    for (ss=0; ss<gdata.networks_online; ss++) {
        gnetwork = &(gdata.networks[ss]);
        /*----- send server stuff ----- */
        if (changesec) {
            sendserver();
            if (gdata.curtime%INAMNT_SIZE == (INAMNT_SIZE-1))
                gnetwork->inamnt[0] = 0;
            else
                gnetwork->inamnt[gdata.curtime%INAMNT_SIZE+1] = 0;
        }
        /*----- see if we can send out some xdcc lists */
        if (changesec && gnetwork->serverstatus == SERVERSTATUS_CONNECTED) {
            if (!irlist_size((&gnetwork->serverq_normal)) && !irlist_size(&(gnetwork->serverq_slow)))
                sendxdlqueue();
        }
    }
    gnetwork = NULL;

    /*----- see if its time to change maxb */
    if (changehour) {
        gdata.maxb = gdata.overallmaxspeed;
        if (gdata.overallmaxspeeddayspeed != gdata.overallmaxspeed) {
            struct tm *localt;
            localt = localtime(&gdata.curtime);

            if ((unsigned int)localt->tm_hour >= gdata.overallmaxspeeddaytimestart
                    && (unsigned int)localt->tm_hour < gdata.overallmaxspeeddaytimeend
                    && ( gdata.overallmaxspeeddaydays & (1 << (unsigned int)localt->tm_wday)) )
                gdata.maxb = gdata.overallmaxspeeddayspeed;
        }
        isrotatelog();
        expire_options();
    }

    /*----- see if we've hit a transferlimit or need to reset counters */
    if (changesec)
    {
        unsigned int ii;
        unsigned int transferlimits_over = 0;
        for (ii=0; ii<NUMBER_TRANSFERLIMITS; ii++)
        {
            /* reset counters? */
            if ((!gdata.transferlimits[ii].ends) ||
                    (gdata.transferlimits[ii].ends < gdata.curtime))
            {
                struct tm *localt;
                if (gdata.transferlimits[ii].limit && gdata.transferlimits[ii].ends)
                {
                    ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR,
                            "Resetting %s transfer limit, used %" LLPRINTFMT "uMB of the %" LLPRINTFMT "uMB limit",
                            transferlimit_type_to_string(ii),
                            gdata.transferlimits[ii].used / 1024 / 1024,
                            gdata.transferlimits[ii].limit / 1024 / 1024);
                }

                /* find our next end time */
                localt = localtime(&gdata.curtime);
                localt->tm_sec = localt->tm_min = localt->tm_hour = 0; /* midnight */
                switch (ii)
                {
                case TRANSFERLIMIT_DAILY:
                    /* tomorrow */
                    localt->tm_mday++;
                    break;

                case TRANSFERLIMIT_WEEKLY:
                    /* next sunday morning */
                    localt->tm_mday += 7 - localt->tm_wday;
                    break;

                case TRANSFERLIMIT_MONTHLY:
                    /* next month */
                    localt->tm_mday = gdata.start_of_month;
                    localt->tm_mon++;
                    break;

                default:
                    outerror(OUTERROR_TYPE_CRASH, "unknown type %u", ii);
                }
                /* tm_wday and tm_yday are ignored in mktime() */
                gdata.transferlimits[ii].ends = mktime(localt);
                gdata.transferlimits[ii].used = 0;
                if ( ii == TRANSFERLIMIT_DAILY )
                    reset_download_limits();
            }

            if (!transferlimits_over &&
                    gdata.transferlimits[ii].limit &&
                    (gdata.transferlimits[ii].used >= gdata.transferlimits[ii].limit))
            {
                transferlimits_over = 1 + ii;

                if (!gdata.transferlimits_over)
                {
                    char *tempstr = transfer_limit_exceeded_msg(ii);

                    ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR,
                            "All %" LLPRINTFMT "uMB of the %s transfer limit used. Stopping transfers.",
                            gdata.transferlimits[ii].limit / 1024 / 1024,
                            transferlimit_type_to_string(ii));

                    /* remove queued users */
                    queue_all_remove(&gdata.mainqueue, tempstr);
                    queue_all_remove(&gdata.idlequeue, tempstr);

                    /* stop transfers */
                    for (tr = irlist_get_head(&gdata.trans); tr; tr = irlist_get_next(tr))
                    {
                        if (tr->tr_status != TRANSFER_STATUS_DONE)
                        {
                            gnetwork = &(gdata.networks[tr->net]);
                            t_closeconn(tr,tempstr,0);
                        }
                    }

                    gnetwork = NULL;
                    mydelete(tempstr);
                }
            }
        }

        if (gdata.transferlimits_over != transferlimits_over)
        {
            if (!transferlimits_over)
            {
                ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR,
                        "No longer over any transfer limits. Transfers are now allowed.");
            }
            gdata.transferlimits_over = transferlimits_over;
        }
    }

    /*----- gdata.autoignore_threshold seconds ----- */
    if (changesec && ((unsigned)gdata.curtime > (lastignoredec + gdata.autoignore_threshold)))
    {
        igninfo *ignore;

        lastignoredec += gdata.autoignore_threshold;

        ignore = irlist_get_head(&gdata.ignorelist);

        while(ignore)
        {
            ignore->bucket--;
            if ((ignore->flags & IGN_IGNORING) && (ignore->bucket == 0))
            {
                ignore->flags &= ~IGN_IGNORING;
                ioutput(OUT_S|OUT_L|OUT_D, COLOR_NO_COLOR,
                        "Ignore removed for %s",ignore->hostmask);
                write_statefile();
            }
            if (ignore->bucket == 0)
            {
                mydelete(ignore->hostmask);
                ignore = irlist_delete(&gdata.ignorelist, ignore);
            }
            else
            {
                ignore = irlist_get_next(ignore);
            }
        }
    }

    /*----- periodicmsg_time seconds ----- */
    if (changesec) {
        send_periodicmsg();
    }

    updatecontext();

    /*----- 5 seconds ----- */
    if (changesec && (gdata.curtime - last5sec > 4)) {
        last5sec = gdata.curtime;

        updatecontext();
        /*----- server timeout ----- */
        for (ss=0; ss<gdata.networks_online; ss++) {
            gnetwork = &(gdata.networks[ss]);
            if (gdata.needsshutdown)
                continue;
            if ((gnetwork->serverstatus == SERVERSTATUS_CONNECTED) &&
                    (gdata.curtime > gnetwork->lastservercontact + SRVRTOUT)) {
                if (gnetwork->servertime < 3)
                {
                    const char *servname = gnetwork->curserveractualname ? gnetwork->curserveractualname : gnetwork->curserver.hostname;
                    size_t     len       = 6 + strlen(servname);
                    char       *tempstr3 = mymalloc(len + 1);
                    snprintf(tempstr3, len + 1, "PING %s\n", servname);
                    writeserver_ssl(tempstr3, len);
                    if (gdata.debug > 0)
                    {
                        tempstr3[len-1] = '\0';
                        len--;
                        ioutput(OUT_S, COLOR_MAGENTA, "<NORES<: %s", tempstr3);
                    }
                    mydelete(tempstr3);
                    gnetwork->servertime++;
                }
                else if (gnetwork->servertime == 3) {
                    ioutput(OUT_S|OUT_L|OUT_D, COLOR_RED,
                            "Closing Server Connection on %s: No Response for %u minutes.",
                            gnetwork->name, SRVRTOUT/60);
                    close_server();
                    gnetwork->servertime = 0;
                }
            }


            /*----- ping server ----- */
            if (gnetwork->recentsent) {
                pingserver();
                gnetwork->recentsent--;
            }

        }
    } /* networks */
    gnetwork = NULL;

    /*----- 4 seconds ----- */
    if (changesec && (gdata.curtime - last4sec > 3))
    {

        /*----- update lastspeed, check minspeed ----- */
        tr = irlist_get_head(&gdata.trans);
        while(tr)
        {
            if ( tr->con.connecttime+(MIN_TL/2) > gdata.curtime ) /* initial */
            {
                tr->lastspeed =
                    (tr->lastspeed)*DCL_SPDW_I +
                    (((float)(tr->bytessent-tr->lastspeedamt))/1024.0)*(1.0-DCL_SPDW_I)/((float)(gdata.curtime-last4sec)*1.0);
            }
            else                                              /* ongoing */
            {
                tr->lastspeed =
                    (tr->lastspeed)*DCL_SPDW_O +
                    (((float)(tr->bytessent-tr->lastspeedamt))/1024.0)*(1.0-DCL_SPDW_O)/((float)(gdata.curtime-last4sec)*1.0);
            }

            tr->lastspeedamt = tr->bytessent;

            t_checkminspeed(tr);

            tr = irlist_get_next(tr);
        }

        ul = irlist_get_head(&gdata.uploads);
        while(ul)
        {
            if ( ul->con.connecttime+(MIN_TL/2) > gdata.curtime ) /* initial */
            {
                ul->lastspeed =
                    (ul->lastspeed)*DCL_SPDW_I +
                    (((float)(ul->bytesgot-ul->lastspeedamt))/1024.0)*(1.0-DCL_SPDW_I)/((float)(gdata.curtime-last4sec)*1.0);
            }
            else                                                /* ongoing */
            {
                ul->lastspeed =
                    (ul->lastspeed)*DCL_SPDW_O +
                    (((float)(ul->bytesgot-ul->lastspeedamt))/1024.0)*(1.0-DCL_SPDW_O)/((float)(gdata.curtime-last4sec)*1.0);
            }
            ul->lastspeedamt = ul->bytesgot;

            ul = irlist_get_next(ul);
        }

        last4sec = gdata.curtime;
    }

    updatecontext();
    /*----- check for size change ----- */
    if (changesec)
        checktermsize();

    updatecontext();

    for (ss=0; ss<gdata.networks_online; ss++) {
        gnetwork = &(gdata.networks[ss]);
        /*----- plist stuff ----- */
        if ((gnetwork->serverstatus == SERVERSTATUS_CONNECTED) &&
                changemin &&
                irlist_size(&gdata.xdccs) &&
                !gdata.transferlimits_over &&
                (irlist_size(&(gnetwork->serverq_channel)) < irlist_size(&gdata.xdccs)) &&
                (!gdata.queuesize || irlist_size(&gdata.mainqueue) < gdata.queuesize) &&
                (gdata.nolisting <= gdata.curtime))
        {
            char *tchanf = NULL, *tchanm = NULL, *tchans = NULL;

            for(ch = irlist_get_head(&(gnetwork->channels));
                    ch;
                    ch = irlist_get_next(ch))
            {
                if ((ch->flags & CHAN_ONCHAN) &&
                        (ch->nextann < gdata.curtime) &&
                        ch->plisttime &&
                        (((gdata.curtime / 60) % ch->plisttime) == ch->plistoffset))
                {
                    ch->nextmsg = gdata.curtime + ch->delay;
                    if (ch->pgroup != NULL)
                    {
                        ioutput(OUT_S|OUT_D, COLOR_NO_COLOR, "Plist sent to %s (pgroup)", ch->name);
                        pubplist = mycalloc(sizeof(userinput));
                        pubplist->method = method_xdl_channel;
                        pubplist->net = gnetwork->net;
                        pubplist->level = ADMIN_LEVEL_PUBLIC;
                        a_fillwith_plist(pubplist, ch->name, ch);
                        u_parseit(pubplist);
                        mydelete(pubplist);
                        continue;
                    }
                    if (ch->flags & CHAN_MINIMAL)
                    {
                        if (tchanm)
                        {
                            strncat(tchanm,",",maxtextlength-strlen(tchanm)-1);
                            strncat(tchanm,ch->name,maxtextlength-strlen(tchanm)-1);
                        }
                        else
                        {
                            tchanm = mymalloc(maxtextlength);
                            strncpy(tchanm,ch->name,maxtextlength-1);
                        }
                    }
                    else if (ch->flags & CHAN_SUMMARY)
                    {
                        if (tchans)
                        {
                            strncat(tchans,",",maxtextlength-strlen(tchans)-1);
                            strncat(tchans,ch->name,maxtextlength-strlen(tchans)-1);
                        }
                        else
                        {
                            tchans = mymalloc(maxtextlength);
                            strncpy(tchans,ch->name,maxtextlength-1);
                        }
                    }
                    else
                    {
                        if (tchanf)
                        {
                            strncat(tchanf,",",maxtextlength-strlen(tchanf)-1);
                            strncat(tchanf,ch->name,maxtextlength-strlen(tchanf)-1);
                        }
                        else
                        {
                            tchanf = mymalloc(maxtextlength);
                            strncpy(tchanf,ch->name,maxtextlength-1);
                        }
                    }
                }
            }

            if (tchans)
            {
                if (gdata.restrictprivlist && !gdata.creditline && !irlist_size(&gdata.headline))
                {
                    ioutput(OUT_S|OUT_D, COLOR_NO_COLOR,
                            "Can't send Summary Plist to %s (restrictprivlist is set and no creditline or headline, summary makes no sense!)", tchans);
                }
                else
                {
                    ioutput(OUT_S|OUT_D, COLOR_NO_COLOR, "Plist sent to %s (summary)", tchans);
                    pubplist = mycalloc(sizeof(userinput));
                    a_fillwith_msg2(pubplist, tchans, "XDL");
                    pubplist->method = method_xdl_channel_sum;
                    u_parseit(pubplist);
                    mydelete(pubplist);
                }
                mydelete(tchans);
            }
            if (tchanf) {
                ioutput(OUT_S|OUT_D, COLOR_NO_COLOR, "Plist sent to %s (full)", tchanf);
                pubplist = mycalloc(sizeof(userinput));
                a_fillwith_plist(pubplist, tchanf, NULL);
                pubplist->method = method_xdl_channel;
                u_parseit(pubplist);
                mydelete(pubplist);
                mydelete(tchanf);
            }
            if (tchanm) {
                ioutput(OUT_S|OUT_D, COLOR_NO_COLOR, "Plist sent to %s (minimal)", tchanm);
                pubplist = mycalloc(sizeof(userinput));
                a_fillwith_msg2(pubplist, tchanm, "XDL");
                pubplist->method = method_xdl_channel_min;
                u_parseit(pubplist);
                mydelete(pubplist);
                mydelete(tchanm);
            }

        }
    } /* networks */
    gnetwork = NULL;

    updatecontext();
    /*----- low bandwidth send, save state file ----- */
    if (changesec && (gdata.curtime - last3min > 180)) {
        last3min = gdata.curtime;

        xdccsent = 0;
        for (i=0; i<XDCC_SENT_SIZE; i++)
            xdccsent += (ir_uint64)gdata.xdccsent[i];
        xdccsent /= XDCC_SENT_SIZE*1024;

        if ((xdccsent < (unsigned)gdata.lowbdwth) &&
                !gdata.exiting &&
                irlist_size(&gdata.mainqueue) &&
                (irlist_size(&gdata.trans) < gdata.maxtrans))
        {
            check_idle_queue(0);
            send_from_queue(1, 0, NULL);
        }
        write_files();
    }

    updatecontext();
    for (ss=0; ss<gdata.networks_online; ss++) {
        gnetwork = &(gdata.networks[ss]);
        /*----- queue notify ----- */
        if (changesec && gdata.notifytime && (!gdata.quietmode) &&
                ((unsigned)gdata.curtime > (gnetwork->lastnotify + (gdata.notifytime*60))))
        {
            gnetwork->lastnotify = gdata.curtime;

            if (gnetwork->serverstatus == SERVERSTATUS_CONNECTED)
            {
                if ((irlist_size(&(gnetwork->serverq_fast)) >= 10) ||
                        (irlist_size(&(gnetwork->serverq_normal)) >= 10) ||
                        (irlist_size(&(gnetwork->serverq_slow)) >= 50))
                {
                    ioutput(OUT_S|OUT_D|OUT_L, COLOR_NO_COLOR,
                            "notifications skipped on %s, server queue is rather large",
                            gnetwork->name);
                }
                else
                {
                    notifyqueued();
                    notifybandwidth();
                    notifybandwidthtrans();
                }
            }

        }
    } /* networks */
    gnetwork = NULL;

    updatecontext();
    /*----- log stats / remote admin stats ----- */
    if ( changesec &&
            ((unsigned)gdata.curtime >= (last2min + gdata.status_time_dcc_chat)))
    {
        last2min = gdata.curtime;
        if (gdata.logstats)
        {
            logstat();

            chat_writestatus();
        }
    }

    updatecontext();

    /* look to see if any files changed */
    if (changesec)
        look_for_file_remove();

    updatecontext();

    /*----- 20 seconds ----- */
    if (changesec && (gdata.curtime - last20sec > 19)) {
        expire_badip();

        if (gdata.logfd != FD_UNUSED)
        {
            /* cycle */
            close(gdata.logfd);
            gdata.logfd = FD_UNUSED;
        }

        updatecontext();

        for (ss=0; ss<gdata.networks_online; ss++) {
            gnetwork = &(gdata.networks[ss]);
            /* try rejoining channels not on */
            ch = irlist_get_head(&(gnetwork->channels));
            while(ch)
            {
                if ((gnetwork->serverstatus == SERVERSTATUS_CONNECTED) &&
                        !(ch->flags & CHAN_ONCHAN))
                {
                    joinchannel(ch);
                }
                ch = irlist_get_next(ch);
            }
        } /* networks */
        gnetwork = NULL;

        last20sec = gdata.curtime;

        updatecontext();

        for (ss=0; ss<gdata.networks_online; ss++) {
            gnetwork = &(gdata.networks[ss]);
            /* try to regain nick */
            if (!gnetwork->user_nick || strcmp(get_config_nick(), gnetwork->user_nick))
            {
                writeserver(WRITESERVER_NORMAL, "NICK %s", get_config_nick());
            }
        } /* networks */
        gnetwork = NULL;

        updatecontext();

        /* update status line */
        if (!gdata.background && !gdata.noscreen) {
            char tempstr[maxtextlength];
            char tempstr2[maxtextlengthshort];

            if (gdata.attop) gotobot();

            tostdout(IRVT_SAVE_CURSOR);

            getstatusline(tempstr,maxtextlength);
            tempstr[min2(maxtextlength-2,gdata.termcols-4)] = '\0';
            snprintf(tempstr2, maxtextlengthshort, IRVT_CURSOR_HOME1 "[ %%-%us ]", gdata.termlines - 1, gdata.termcols - 4);
            tostdout(tempstr2,tempstr);

            tostdout(IRVT_CURSOR_HOME2 IRVT_UNSAVE_CURSOR, gdata.termlines, gdata.termcols);
        }

        admin_jobs();
#ifdef USE_RUBY
        rehash_myruby(1);
#endif /* USE_RUBY */
        delayed_announce();
    }

    updatecontext();

    if (changemin)
    {
        reverify_restrictsend();
        update_hour_dinoex(lastmin);
        check_idle_queue(0);
        clean_uploadhost();
        auto_rehash();
    }

    updatecontext();

    if ((gdata.md5build.file_fd != FD_UNUSED) &&
            FD_ISSET(gdata.md5build.file_fd, &gdata.readset))
    {
        ssize_t howmuch;
#if defined(_OS_CYGWIN)
        int reads_per_loop = 32;
#else /* _OS_CYGWIN */
        int reads_per_loop = 64;
#endif /* _OS_CYGWIN */

        assert(gdata.md5build.xpack);

        while (reads_per_loop--)
        {
            howmuch = read(gdata.md5build.file_fd, gdata.sendbuff, BUFFERSIZE);

            if (gdata.debug >30)
            {
                ioutput(OUT_S, COLOR_YELLOW, "MD5: [Pack %u] read %ld",
                        number_of_pack(gdata.md5build.xpack), (long)howmuch);
            }

            if ((howmuch < 0) && (errno != EAGAIN))
            {
                outerror(OUTERROR_TYPE_WARN, "MD5: [Pack %u] Can't read data from file '%s': %s",
                         number_of_pack(gdata.md5build.xpack),
                         gdata.md5build.xpack->file, strerror(errno));

                event_close(gdata.md5build.file_fd);
                gdata.md5build.file_fd = FD_UNUSED;
                gdata.md5build.xpack = NULL;
                break;
            }
            else if (howmuch < 0)
            {
                break;
            }
            else if (howmuch == 0)
            {
                /* EOF */
                outerror(OUTERROR_TYPE_WARN, "MD5: [Pack %u] Can't read data from file '%s': %s",
                         number_of_pack(gdata.md5build.xpack),
                         gdata.md5build.xpack->file, "truncated");
                start_md5_hash(gdata.md5build.xpack, number_of_pack(gdata.md5build.xpack));
                break;
            }
            /* else got data */
            MD5Update(&gdata.md5build.md5sum, gdata.sendbuff, howmuch);
            if (!gdata.nocrc32)
                crc32_update((char *)gdata.sendbuff, howmuch);
            gdata.md5build.bytes += howmuch;
            if (gdata.md5build.bytes == gdata.md5build.xpack->st_size)
            {
                complete_md5_hash();
                break;
            }
        }
    }

    if (!gdata.nomd5sum && changesec && (!gdata.md5build.xpack))
    {
        unsigned int packnum = 1;
        /* see if any pack needs a md5sum calculated */
        if (gdata.nomd5_start <= gdata.curtime)
            for (xd = irlist_get_head(&gdata.xdccs); xd; xd = irlist_get_next(xd), packnum++)
            {
                if (!gdata.nocrc32)
                {
                    if (!xd->has_crc32)
                        xd->has_md5sum = 0; /* force recheck with crc */
                }
                if (!xd->has_md5sum)
                {
                    if (verifyshell(&gdata.md5sum_exclude, xd->file))
                        continue;
                    if (!gdata.attop) gototop();
                    start_md5_hash(xd, packnum);
                    break;
                }
            }
    }

    updatecontext();

    if (gdata.exiting && has_closed_servers()) {

        for (chat = irlist_get_head(&gdata.dccchats);
                chat;
                chat = irlist_delete(&gdata.dccchats,chat))
        {
            writedccchat(chat, 0, "iroffer exited, Closing DCC Chat\n");
            shutdowndccchat(chat,1);
        }

        mylog("iroffer exited\n\n");

        exit_iroffer(0);
    }

    updatecontext();

    if (gdata.needsrehash) {
        gdata.needsrehash = 0;
        urehash = mycalloc(sizeof(userinput));
        a_fillwith_msg2(urehash, NULL, "REHASH");
        urehash->method = method_out_all;  /* just OUT_S|OUT_L|OUT_D it */
        urehash->net = 0;
        urehash->level = ADMIN_LEVEL_FULL;
        u_parseit(urehash);
        mydelete(urehash);
    }

    updatecontext();

    chat = irlist_get_head(&gdata.dccchats);
    while (chat)
    {
        if (chat->status == DCCCHAT_UNUSED)
        {
            chat = irlist_delete(&gdata.dccchats,chat);
        }
        else
        {
            flushdccchat(chat);
            chat = irlist_get_next(chat);
        }
    }

    if (gdata.autoadd_time > 0)
    {
        if (changesec && ((unsigned)gdata.curtime > (lastautoadd + gdata.autoadd_time)))
        {
            lastautoadd = gdata.curtime;
            autoadd_all();
        }
    }

    /* END */
    updatecontext();
    if (gdata.needsclear) drawbot();

    changehour=changemin=0;

}
示例#2
0
int
main(int argc, char **argv)
{
	char *myname;
	char *optionp;
	char *opigp;
	int mflag;
	int readonly;
	struct cachefs_mountargs margs;
	char *backfstypep;
	char *reducep;
	char *specp;
	int xx;
	int stat_loc;
	char *newargv[20];
	char *mntp;
	pid_t pid;
	int mounted;
	int c;
	int lockid;
	int Oflg;
	char *strp;
	char servname[33];
	int notify = 1;
	struct stat64 statb;
	struct mnttagdesc mtdesc;
	char mops[MAX_MNTOPT_STR];
	char cfs_nfsv4ops[MAX_MNTOPT_STR];
	uint32_t nfsvers = 0;
	uint32_t nfsvers_error = FALSE;
	int nfsv3pass = 0;
	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	if (argv[0]) {
		myname = strrchr(argv[0], '/');
		if (myname)
			myname++;
		else
			myname = argv[0];
	} else {
		myname = "path unknown";
	}

	optionp = NULL;
	nomnttab = 0;
	quiet = 0;
	readonly = 0;
	Oflg = 0;
	cfs_nfsv4ops[0] = '\0';

	/* process command line options */
	while ((c = getopt(argc, argv, "mo:Orq")) != EOF) {
		switch (c) {
		case 'm':	/* no entry in /etc/mnttab */
			nomnttab = 1;
			break;

		case 'o':
			optionp = optarg;
			break;

		case 'O':
			Oflg++;
			break;

		case 'r':	/* read only mount */
			readonly = 1;
			break;

		case 'q':
			quiet = 1;
			break;

		default:
			usage("invalid option");
			return (1);
		}
	}

	/* if -o not specified */
	if (optionp == NULL) {
		usage(gettext("\"-o backfstype\" must be specified"));
		return (1);
	}

	/* verify special device and mount point are specified */
	if (argc - optind < 2) {
		usage(gettext("must specify special device and mount point"));
		return (1);
	}

	/* Store mount point and special device. */
	specp = argv[argc - 2];
	mntp = argv[argc - 1];

	/* Initialize default mount values */
	margs.cfs_options.opt_flags = CFS_ACCESS_BACKFS;
	margs.cfs_options.opt_popsize = DEF_POP_SIZE;
	margs.cfs_options.opt_fgsize = DEF_FILEGRP_SIZE;
	margs.cfs_fsid = NULL;
	memset(margs.cfs_cacheid, 0, sizeof (margs.cfs_cacheid));
	margs.cfs_cachedir = CFS_DEF_DIR;
	margs.cfs_backfs = NULL;
	margs.cfs_acregmin = 0;
	margs.cfs_acregmax = 0;
	margs.cfs_acdirmin = 0;
	margs.cfs_acdirmax = 0;
	mflag = MS_OPTIONSTR;
	if (nomnttab)
		mflag |= MS_NOMNTTAB;
	backfstypep = NULL;

	/* process -o options */
	xx = set_cfs_args(optionp, &margs, &mflag, &backfstypep, &reducep,
	    &notify, &nfsv3pass);
	if (xx) {
		return (1);
	}
	strcpy(mops, optionp);

	/* backfstype has to be specified */
	if (backfstypep == NULL) {
		usage(gettext("\"-o backfstype\" must be specified"));
		return (1);
	}

	if ((strcmp(backfstypep, "nfs") != 0) &&
				(strcmp(backfstypep, "hsfs") != 0)) {
		pr_err(gettext("%s as backfstype is not supported."),
					backfstypep);
		return (1);
	}

	/* set default write mode if not specified */
	if ((margs.cfs_options.opt_flags &
	    (CFS_WRITE_AROUND|CFS_NONSHARED)) == 0) {
		margs.cfs_options.opt_flags |= CFS_WRITE_AROUND;
		if (strcmp(backfstypep, "hsfs") == 0)
			mflag |= MS_RDONLY;
	}

	/* if read-only was specified with the -r option */
	if (readonly) {
		mflag |= MS_RDONLY;
	}

	/* if overlay was specified with -O option */
	if (Oflg) {
		mflag |= MS_OVERLAY;
	}

	/* get the fsid of the backfs and the cacheid */
	margs.cfs_fsid = get_back_fsid(specp);
	if (margs.cfs_fsid == NULL) {
		pr_err(gettext("out of memory"));
		return (1);
	}

	/*
	 * If using this cachedir to mount a file system for the first time
	 * after reboot, the ncheck for the sanity of the cachedir
	 */
	if (first_time_ab(margs.cfs_cachedir))
		if (check_cache(margs.cfs_cachedir))
			return (1);

	/* get the front file system cache id if necessary */
	if (margs.cfs_cacheid[0] == '\0') {
		char *cacheid = get_cacheid(margs.cfs_fsid, mntp);

		if (cacheid == NULL) {
			pr_err(gettext("default cacheid too long"));
			return (1);
		}

		strcpy(margs.cfs_cacheid, cacheid);
	}

	/* lock the cache directory shared */
	lockid = cachefs_dir_lock(margs.cfs_cachedir, 1);
	if (lockid == -1) {
		/* exit if could not get the lock */
		return (1);
	}

	/* if no mount point was specified and we are not remounting */
	mounted = 0;
	if ((margs.cfs_backfs == NULL) &&
	    (((mflag & MS_REMOUNT) == 0) ||
	    (margs.cfs_options.opt_flags & CFS_SLIDE))) {
		/* if a disconnectable mount */
		xx = 0;
		if (margs.cfs_options.opt_flags & CFS_DISCONNECTABLE) {
			/* see if the server is alive */
			xx = pingserver(specp);
		}

		/* attempt to mount the back file system */
		if (xx == 0) {
			xx = dobackmnt(&margs, reducep, specp, backfstypep,
			    myname, readonly);
			/*
			 * nfs mount exits with a value of 32 if a timeout
			 * error occurs trying the mount.
			 */
			if (xx && (xx != 32)) {
				cachefs_dir_unlock(lockid);
				rmdir(margs.cfs_backfs);
				return (1);
			}
			if (xx == 0)
				mounted = 1;
		}
	}

	/*
	 * At this point the back file system should be mounted.
	 * Get NFS version information for the back filesystem if
	 * it is NFS. The version information is required
	 * because NFS version 4 is incompatible with cachefs
	 * and we provide pass-through support for NFS version 4
	 * with cachefs, aka the cachefs mount is installed but
	 * there is no caching. This is indicated to the kernel
	 * during the mount by setting the CFS_BACKFS_NFSV4 flag.
	 */
	if (margs.cfs_backfs != NULL && strcmp(backfstypep, "nfs") == 0) {

		nfsvers = cachefs_get_back_nfsvers(margs.cfs_backfs, nomnttab);
		switch (nfsvers) {
		case 2:
			break;

		case 3:
			if (nfsv3pass) {
				/* Force pass through (for debugging) */
				margs.cfs_options.opt_flags = CFS_BACKFS_NFSV4;
				if (cfs_nfsv4_build_opts(optionp,
						cfs_nfsv4ops) != 0) {
					nfsvers_error = TRUE;
					goto clean_backmnt;
				}
			}
			break;

		case 4:
			/*
			 * overwrite old option flags with NFSv4 flag.
			 * Note that will also operate in strict
			 * consistency mode. Clean up the option string
			 * to get rid of the cachefs-specific options
			 * to be in sync with the opt flags, otherwise
			 * these can make it into the mnttab and cause
			 * problems (esp. the disconnected option).
			 */
			margs.cfs_options.opt_flags = CFS_BACKFS_NFSV4;
			if (cfs_nfsv4_build_opts(optionp, cfs_nfsv4ops) != 0) {
				nfsvers_error = TRUE;
				goto clean_backmnt;
			}
			break;

		default:
			/* error, unknown version */
			nfsvers_error = TRUE;
			goto clean_backmnt;
		}
	}

	/*
	 * Grab server name from special file arg if it is there or set
	 * server name to "server unknown".
	 */
	margs.cfs_hostname = servname;
	strncpy(servname, specp, sizeof (servname));
	servname[sizeof (servname) - 1] = '\0';
	strp = strchr(servname, ':');
	if (strp == NULL) {
		margs.cfs_hostname = "server unknown";
		margs.cfs_backfsname = specp;
	} else {
		*strp = '\0';
		/*
		 * The rest of the special file arg is the name of
		 * the back filesystem.
		 */
		strp++;
		margs.cfs_backfsname = strp;
	}

	/* mount the cache file system */
	xx = mount((margs.cfs_backfs != NULL) ? margs.cfs_backfs : "nobackfs",
		mntp, mflag | MS_DATA, MNTTYPE_CFS,
		&margs, sizeof (margs),
		(cfs_nfsv4ops[0] == '\0' ? mops : cfs_nfsv4ops),
		MAX_MNTOPT_STR);
clean_backmnt:
	if (xx == -1 || nfsvers_error) {
		if (nfsvers_error) {
			pr_err(gettext("nfs version error."));
		} else if (errno == ESRCH) {
			pr_err(gettext("mount failed, options do not match."));
		} else if ((errno == EAGAIN) && (margs.cfs_backfs == NULL)) {
			pr_err(gettext("mount failed, server not responding."));
		} else {
			pr_err(gettext("mount failed %s"), strerror(errno));
		}

		/* try to unmount the back file system if we mounted it */
		if (mounted) {
			xx = 1;
			newargv[xx++] = "umount";
			newargv[xx++] = margs.cfs_backfs;
			newargv[xx++] = NULL;

			/* fork */
			if ((pid = fork()) == -1) {
				pr_err(gettext("could not fork: %s"),
				    strerror(errno));
				cachefs_dir_unlock(lockid);
				return (1);
			}

			/* if the child */
			if (pid == 0) {
				/* do the unmount */
				doexec(backfstypep, newargv, "umount");
			}

			/* else if the parent */
			else {
				wait(0);
			}
			rmdir(margs.cfs_backfs);
		}

		cachefs_dir_unlock(lockid);
		return (1);
	}

	/* release the lock on the cache directory */
	cachefs_dir_unlock(lockid);

	/* record the mount information in the fscache directory */
	record_mount(mntp, specp, margs.cfs_backfs, backfstypep,
		margs.cfs_cachedir, margs.cfs_cacheid,
		(cfs_nfsv4ops[0] == '\0' ? optionp : cfs_nfsv4ops), reducep);

	/* notify the daemon of the mount */
	if (notify)
		daemon_notify(margs.cfs_cachedir, margs.cfs_cacheid);

	/* update mnttab file if necessary */
	if (!nomnttab) {
		/*
		 * If we added the back file system, tag it with ignore,
		 * however, don't fail the mount after its done
		 * if the tag can't be added (eg., this would cause
		 * automounter problems).
		 */
		if (mounted) {
			FILE *mt;
			struct extmnttab mnt;

			if ((mt = fopen(MNTTAB, "r")) == NULL)
				return (1);
			while (getextmntent(mt, &mnt, sizeof (mnt)) != -1) {
				if (mnt.mnt_mountp != NULL &&
				    strcmp(margs.cfs_backfs,
					mnt.mnt_mountp) == 0) {
					/* found it, do tag ioctl */
					mtdesc.mtd_major = mnt.mnt_major;
					mtdesc.mtd_minor = mnt.mnt_minor;
					mtdesc.mtd_mntpt = margs.cfs_backfs;
					mtdesc.mtd_tag = MNTOPT_IGNORE;

					(void) ioctl(fileno(mt),
						MNTIOC_SETTAG, &mtdesc);
					break;
				}
			}
			fclose(mt);
		}
	}

	/* return success */
	return (0);
}