openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/util-linux/2.33.2-r0/util-linux-2.33.2/sys-utils/hwclock.c
static int manipulate_clock(const struct hwclock_control* ctl,
                            const time_t set_time,
                            const struct timeval startup_time,
                            struct adjtime* adjtime)
{
    /* The time at which we read the Hardware Clock */
    struct timeval read_time;
    /*
     * The Hardware Clock gives us a valid time, or at
     * least something close enough to fool mktime().
     */
    int hclock_valid = 0;
    /*
     * Tick synchronized time read from the Hardware Clock and
     * then drift corrected for all operations except --show.
     */
    struct timeval hclocktime = {0};
    /*
     * hclocktime correlated to startup_time. That is, what drift
     * corrected Hardware Clock time would have been at start up.
     */
    struct timeval startup_hclocktime = {0};
    /* Total Hardware Clock drift correction needed. */
    struct timeval tdrift;

    if ((ctl->set || ctl->systohc || ctl->adjust) &&
        (adjtime->local_utc == UTC) != ctl->universal)
    {
        adjtime->local_utc = ctl->universal ? UTC : LOCAL;
        adjtime->dirty = 1;
    }
    /*
     * Negate the drift correction, because we want to 'predict' a
     * Hardware Clock time that includes drift.
     */
    if (ctl->predict)
    {
        hclocktime = t2tv(set_time);
        calculate_adjustment(ctl, adjtime->drift_factor, adjtime->last_adj_time,
                             adjtime->not_adjusted, hclocktime.tv_sec, &tdrift);
        hclocktime = time_inc(hclocktime,
                              (double)-(tdrift.tv_sec + tdrift.tv_usec / 1E6));
        if (ctl->verbose)
        {
            printf(_("Target date:   %ld\n"), set_time);
            printf(_("Predicted RTC: %ld\n"), hclocktime.tv_sec);
        }
        return display_time(hclocktime);
    }

    if (ctl->systz)
        return set_system_clock(ctl, startup_time);

    if (ur->get_permissions())
        return EXIT_FAILURE;

    /*
     * Read and drift correct RTC time; except for RTC set functions
     * without the --update-drift option because: 1) it's not needed;
     * 2) it enables setting a corrupted RTC without reading it first;
     * 3) it significantly reduces system shutdown time.
     */
    if (!((ctl->set || ctl->systohc) && !ctl->update))
    {
        /*
         * Timing critical - do not change the order of, or put
         * anything between the follow three statements.
         * Synchronization failure MUST exit, because all drift
         * operations are invalid without it.
         */
        if (synchronize_to_clock_tick(ctl))
            return EXIT_FAILURE;
        read_hardware_clock(ctl, &hclock_valid, &hclocktime.tv_sec);
        gettimeofday(&read_time, NULL);

        if (!hclock_valid)
        {
            warnx(_("RTC read returned an invalid value."));
            return EXIT_FAILURE;
        }
        /*
         * Calculate and apply drift correction to the Hardware Clock
         * time for everything except --show
         */
        calculate_adjustment(ctl, adjtime->drift_factor, adjtime->last_adj_time,
                             adjtime->not_adjusted, hclocktime.tv_sec, &tdrift);
        if (!ctl->show)
            hclocktime = time_inc(tdrift, hclocktime.tv_sec);

        startup_hclocktime =
            time_inc(hclocktime, time_diff(startup_time, read_time));
    }
    if (ctl->show || ctl->get)
    {
        return display_time(startup_hclocktime);
    }
    else if (ctl->set)
    {
        set_hardware_clock_exact(ctl, set_time, startup_time);
        if (!ctl->noadjfile)
            adjust_drift_factor(ctl, adjtime, t2tv(set_time),
                                startup_hclocktime);
    }
    else if (ctl->adjust)
    {
        if (tdrift.tv_sec > 0 || tdrift.tv_sec < -1)
            do_adjustment(ctl, adjtime, hclocktime, read_time);
        else
            printf(_("Needed adjustment is less than one second, "
                     "so not setting clock.\n"));
    }
    else if (ctl->systohc)
    {
        struct timeval nowtime, reftime;
        /*
         * We can only set_hardware_clock_exact to a
         * whole seconds time, so we set it with
         * reference to the most recent whole
         * seconds time.
         */
        gettimeofday(&nowtime, NULL);
        reftime.tv_sec = nowtime.tv_sec;
        reftime.tv_usec = 0;
        set_hardware_clock_exact(ctl, (time_t)reftime.tv_sec, reftime);
        if (!ctl->noadjfile)
            adjust_drift_factor(ctl, adjtime, nowtime, hclocktime);
    }
    else if (ctl->hctosys)
    {
        return set_system_clock(ctl, hclocktime);
    }
    if (!ctl->noadjfile && adjtime->dirty)
        return save_adjtime(ctl, adjtime);
    return EXIT_SUCCESS;
}
/* Do all the normal work of hwclock - read, set clock, etc. */
Do all the normal work of hwclock - read, set clock, etc.
Do all the normal work of hwclock - read set clock etc .
static int manipulate_clock(const struct hwclock_control* ctl,
                            const time_t set_time,
                            const struct timeval startup_time,
                            struct adjtime* adjtime)
{
    struct timeval read_time;
    int hclock_valid = 0;
    struct timeval hclocktime = {0};
    struct timeval startup_hclocktime = {0};
    struct timeval tdrift;
    if ((ctl->set || ctl->systohc || ctl->adjust) &&
        (adjtime->local_utc == UTC) != ctl->universal)
    {
        adjtime->local_utc = ctl->universal ? UTC : LOCAL;
        adjtime->dirty = 1;
    }
    if (ctl->predict)
    {
        hclocktime = t2tv(set_time);
        calculate_adjustment(ctl, adjtime->drift_factor, adjtime->last_adj_time,
                             adjtime->not_adjusted, hclocktime.tv_sec, &tdrift);
        hclocktime = time_inc(hclocktime,
                              (double)-(tdrift.tv_sec + tdrift.tv_usec / 1E6));
        if (ctl->verbose)
        {
            printf(_("Target date:   %ld\n"), set_time);
            printf(_("Predicted RTC: %ld\n"), hclocktime.tv_sec);
        }
        return display_time(hclocktime);
    }
    if (ctl->systz)
        return set_system_clock(ctl, startup_time);
    if (ur->get_permissions())
        return EXIT_FAILURE;
    if (!((ctl->set || ctl->systohc) && !ctl->update))
    {
        if (synchronize_to_clock_tick(ctl))
            return EXIT_FAILURE;
        read_hardware_clock(ctl, &hclock_valid, &hclocktime.tv_sec);
        gettimeofday(&read_time, NULL);
        if (!hclock_valid)
        {
            warnx(_("RTC read returned an invalid value."));
            return EXIT_FAILURE;
        }
        calculate_adjustment(ctl, adjtime->drift_factor, adjtime->last_adj_time,
                             adjtime->not_adjusted, hclocktime.tv_sec, &tdrift);
        if (!ctl->show)
            hclocktime = time_inc(tdrift, hclocktime.tv_sec);
        startup_hclocktime =
            time_inc(hclocktime, time_diff(startup_time, read_time));
    }
    if (ctl->show || ctl->get)
    {
        return display_time(startup_hclocktime);
    }
    else if (ctl->set)
    {
        set_hardware_clock_exact(ctl, set_time, startup_time);
        if (!ctl->noadjfile)
            adjust_drift_factor(ctl, adjtime, t2tv(set_time),
                                startup_hclocktime);
    }
    else if (ctl->adjust)
    {
        if (tdrift.tv_sec > 0 || tdrift.tv_sec < -1)
            do_adjustment(ctl, adjtime, hclocktime, read_time);
        else
            printf(_("Needed adjustment is less than one second, "
                     "so not setting clock.\n"));
    }
    else if (ctl->systohc)
    {
        struct timeval nowtime, reftime;
        gettimeofday(&nowtime, NULL);
        reftime.tv_sec = nowtime.tv_sec;
        reftime.tv_usec = 0;
        set_hardware_clock_exact(ctl, (time_t)reftime.tv_sec, reftime);
        if (!ctl->noadjfile)
            adjust_drift_factor(ctl, adjtime, nowtime, hclocktime);
    }
    else if (ctl->hctosys)
    {
        return set_system_clock(ctl, hclocktime);
    }
    if (!ctl->noadjfile && adjtime->dirty)
        return save_adjtime(ctl, adjtime);
    return EXIT_SUCCESS;
}
static int manipulate_clock ( const struct hwclock_control * ctl , const time_t set_time , const struct timeval startup_time , struct adjtime * adjtime ) { struct timeval read_time ; int hclock_valid = 0 ; struct timeval hclocktime = { 0 } ; struct timeval startup_hclocktime = { 0 } ; struct timeval tdrift ; if ( ( ctl -> set || ctl -> systohc || ctl -> adjust ) && ( adjtime -> local_utc == UTC ) != ctl -> universal ) { adjtime -> local_utc = ctl -> universal ? UTC : LOCAL ; adjtime -> dirty = 1 ; } if ( ctl -> predict ) { hclocktime = t2tv ( set_time ) ; calculate_adjustment ( ctl , adjtime -> drift_factor , adjtime -> last_adj_time , adjtime -> not_adjusted , hclocktime . tv_sec , & tdrift ) ; hclocktime = time_inc ( hclocktime , ( double ) - ( tdrift . tv_sec + tdrift . tv_usec / 1E6 ) ) ; if ( ctl -> verbose ) { printf ( _ ( " \n " ) , set_time ) ; printf ( _ ( " \n " ) , hclocktime . tv_sec ) ; } return display_time ( hclocktime ) ; } if ( ctl -> systz ) return set_system_clock ( ctl , startup_time ) ; if ( ur -> get_permissions ( ) ) return EXIT_FAILURE ; if ( ! ( ( ctl -> set || ctl -> systohc ) && ! ctl -> update ) ) { if ( synchronize_to_clock_tick ( ctl ) ) return EXIT_FAILURE ; read_hardware_clock ( ctl , & hclock_valid , & hclocktime . tv_sec ) ; gettimeofday ( & read_time , NULL ) ; if ( ! hclock_valid ) { warnx ( _ ( " " ) ) ; return EXIT_FAILURE ; } calculate_adjustment ( ctl , adjtime -> drift_factor , adjtime -> last_adj_time , adjtime -> not_adjusted , hclocktime . tv_sec , & tdrift ) ; if ( ! ctl -> show ) hclocktime = time_inc ( tdrift , hclocktime . tv_sec ) ; startup_hclocktime = time_inc ( hclocktime , time_diff ( startup_time , read_time ) ) ; } if ( ctl -> show || ctl -> get ) { return display_time ( startup_hclocktime ) ; } else if ( ctl -> set ) { set_hardware_clock_exact ( ctl , set_time , startup_time ) ; if ( ! ctl -> noadjfile ) adjust_drift_factor ( ctl , adjtime , t2tv ( set_time ) , startup_hclocktime ) ; } else if ( ctl -> adjust ) { if ( tdrift . tv_sec > 0 || tdrift . tv_sec < -1 ) do_adjustment ( ctl , adjtime , hclocktime , read_time ) ; else printf ( _ ( " " " \n " ) ) ; } else if ( ctl -> systohc ) { struct timeval nowtime , reftime ; gettimeofday ( & nowtime , NULL ) ; reftime . tv_sec = nowtime . tv_sec ; reftime . tv_usec = 0 ; set_hardware_clock_exact ( ctl , ( time_t ) reftime . tv_sec , reftime ) ; if ( ! ctl -> noadjfile ) adjust_drift_factor ( ctl , adjtime , nowtime , hclocktime ) ; } else if ( ctl -> hctosys ) { return set_system_clock ( ctl , hclocktime ) ; } if ( ! ctl -> noadjfile && adjtime -> dirty ) return save_adjtime ( ctl , adjtime ) ; return EXIT_SUCCESS ; }
Do all the normal work of hwclock - read, set clock, etc.
Do all the normal work of hwclock - read set clock etc .
/* The time at which we read the Hardware Clock */ /*
     * The Hardware Clock gives us a valid time, or at
     * least something close enough to fool mktime().
     */ /*
     * Tick synchronized time read from the Hardware Clock and
     * then drift corrected for all operations except --show.
     */ /*
     * hclocktime correlated to startup_time. That is, what drift
     * corrected Hardware Clock time would have been at start up.
     */ /* Total Hardware Clock drift correction needed. */ /*
     * Negate the drift correction, because we want to 'predict' a
     * Hardware Clock time that includes drift.
     */ /*
     * Read and drift correct RTC time; except for RTC set functions
     * without the --update-drift option because: 1) it's not needed;
     * 2) it enables setting a corrupted RTC without reading it first;
     * 3) it significantly reduces system shutdown time.
     */ /*
         * Timing critical - do not change the order of, or put
         * anything between the follow three statements.
         * Synchronization failure MUST exit, because all drift
         * operations are invalid without it.
         */ /*
         * Calculate and apply drift correction to the Hardware Clock
         * time for everything except --show
         */ /*
         * We can only set_hardware_clock_exact to a
         * whole seconds time, so we set it with
         * reference to the most recent whole
         * seconds time.
         */