openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/rsyslog/8.1903.0-r0/rsyslog-8.1903.0/runtime/operatingstate.c
static void osf_checkOnStartup(void)
{
    int do_rename = 1;
    const char* fn_osf = (const char*)glblGetOperatingStateFile();
    char iobuf[sizeof(STATE_CLEAN_CLOSE)];
    const int len_clean_close = sizeof(STATE_CLEAN_CLOSE) - 1;
    assert(fn_osf != NULL);

    const int fd = open(fn_osf, O_RDONLY | O_LARGEFILE | O_CLOEXEC, 0);
    if (fd == -1)
    {
        if (errno != ENOENT)
        {
            LogError(errno, RS_RET_ERR,
                     "error opening existing operatingStateFile '%s' - "
                     "this may be an indication of a problem; ignoring",
                     fn_osf);
        }
        do_rename = 0;
        goto done;
    }
    assert(fd != -1);
    int offs = lseek64(fd, -(len_clean_close + 1), SEEK_END);
    if (offs == -1)
    {
        LogError(
            errno, RS_RET_IO_ERROR,
            "error seeking to end of existing operatingStateFile "
            "'%s' - this may be an indication of a problem, e.g. empty file",
            fn_osf);
        goto done;
    }
    int rd = read(fd, iobuf, len_clean_close);
    if (rd == -1)
    {
        LogError(errno, RS_RET_IO_ERROR,
                 "error reading existing operatingStateFile "
                 "'%s' - this probably indicates an improper shutdown",
                 fn_osf);
        goto done;
    }
    else
    {
        assert(rd <= len_clean_close);
        iobuf[rd] = '\0';
        if (rd != len_clean_close || strcmp(iobuf, STATE_CLEAN_CLOSE) != 0)
        {
            LogError(
                errno, RS_RET_IO_ERROR,
                "existing operatingStateFile '%s' does not end "
                "with '%s, instead it has '%s' - this probably indicates an "
                "improper shutdown",
                fn_osf, STATE_CLEAN_CLOSE, iobuf);
            goto done;
        }
    }

    /* all ok! */
    do_rename = 0;
done:
    if (fd != -1)
    {
        close(fd);
    }
    if (do_rename)
    {
        char newname[MAXFNAME];
        snprintf(newname, sizeof(newname) - 1, "%s.previous", fn_osf);
        newname[MAXFNAME - 1] = '\0';
        if (rename(fn_osf, newname) != 0)
        {
            LogError(
                errno, RS_RET_IO_ERROR,
                "could not rename existing operatingStateFile "
                "'%s' to '%s' - ignored, but will probably be overwritten now",
                fn_osf, newname);
        }
        else
        {
            LogMsg(errno, RS_RET_OK, LOG_INFO,
                   "existing state file '%s' renamed "
                   "to '%s' - you may want to review it",
                   fn_osf, newname);
        }
    }
    return;
}
/* check if old osf points to a problem and, if so, report */
check if old osf points to a problem and, if so, report
check if old osf points to a problem and if so report
static void osf_checkOnStartup(void)
{
    int do_rename = 1;
    const char* fn_osf = (const char*)glblGetOperatingStateFile();
    char iobuf[sizeof(STATE_CLEAN_CLOSE)];
    const int len_clean_close = sizeof(STATE_CLEAN_CLOSE) - 1;
    assert(fn_osf != NULL);
    const int fd = open(fn_osf, O_RDONLY | O_LARGEFILE | O_CLOEXEC, 0);
    if (fd == -1)
    {
        if (errno != ENOENT)
        {
            LogError(errno, RS_RET_ERR,
                     "error opening existing operatingStateFile '%s' - "
                     "this may be an indication of a problem; ignoring",
                     fn_osf);
        }
        do_rename = 0;
        goto done;
    }
    assert(fd != -1);
    int offs = lseek64(fd, -(len_clean_close + 1), SEEK_END);
    if (offs == -1)
    {
        LogError(
            errno, RS_RET_IO_ERROR,
            "error seeking to end of existing operatingStateFile "
            "'%s' - this may be an indication of a problem, e.g. empty file",
            fn_osf);
        goto done;
    }
    int rd = read(fd, iobuf, len_clean_close);
    if (rd == -1)
    {
        LogError(errno, RS_RET_IO_ERROR,
                 "error reading existing operatingStateFile "
                 "'%s' - this probably indicates an improper shutdown",
                 fn_osf);
        goto done;
    }
    else
    {
        assert(rd <= len_clean_close);
        iobuf[rd] = '\0';
        if (rd != len_clean_close || strcmp(iobuf, STATE_CLEAN_CLOSE) != 0)
        {
            LogError(
                errno, RS_RET_IO_ERROR,
                "existing operatingStateFile '%s' does not end "
                "with '%s, instead it has '%s' - this probably indicates an "
                "improper shutdown",
                fn_osf, STATE_CLEAN_CLOSE, iobuf);
            goto done;
        }
    }
    do_rename = 0;
done:
    if (fd != -1)
    {
        close(fd);
    }
    if (do_rename)
    {
        char newname[MAXFNAME];
        snprintf(newname, sizeof(newname) - 1, "%s.previous", fn_osf);
        newname[MAXFNAME - 1] = '\0';
        if (rename(fn_osf, newname) != 0)
        {
            LogError(
                errno, RS_RET_IO_ERROR,
                "could not rename existing operatingStateFile "
                "'%s' to '%s' - ignored, but will probably be overwritten now",
                fn_osf, newname);
        }
        else
        {
            LogMsg(errno, RS_RET_OK, LOG_INFO,
                   "existing state file '%s' renamed "
                   "to '%s' - you may want to review it",
                   fn_osf, newname);
        }
    }
    return;
}
static void osf_checkOnStartup ( void ) { int do_rename = 1 ; const char * fn_osf = ( const char * ) glblGetOperatingStateFile ( ) ; char iobuf [ sizeof ( STATE_CLEAN_CLOSE ) ] ; const int len_clean_close = sizeof ( STATE_CLEAN_CLOSE ) - 1 ; assert ( fn_osf != NULL ) ; const int fd = open ( fn_osf , O_RDONLY | O_LARGEFILE | O_CLOEXEC , 0 ) ; if ( fd == -1 ) { if ( errno != ENOENT ) { LogError ( errno , RS_RET_ERR , " " " " , fn_osf ) ; } do_rename = 0 ; goto done ; } assert ( fd != -1 ) ; int offs = lseek64 ( fd , - ( len_clean_close + 1 ) , SEEK_END ) ; if ( offs == -1 ) { LogError ( errno , RS_RET_IO_ERROR , " " " " , fn_osf ) ; goto done ; } int rd = read ( fd , iobuf , len_clean_close ) ; if ( rd == -1 ) { LogError ( errno , RS_RET_IO_ERROR , " " " " , fn_osf ) ; goto done ; } else { assert ( rd <= len_clean_close ) ; iobuf [ rd ] = ' \0 ' ; if ( rd != len_clean_close || strcmp ( iobuf , STATE_CLEAN_CLOSE ) != 0 ) { LogError ( errno , RS_RET_IO_ERROR , " " " " " " , fn_osf , STATE_CLEAN_CLOSE , iobuf ) ; goto done ; } } do_rename = 0 ; done : if ( fd != -1 ) { close ( fd ) ; } if ( do_rename ) { char newname [ MAXFNAME ] ; snprintf ( newname , sizeof ( newname ) - 1 , " " , fn_osf ) ; newname [ MAXFNAME - 1 ] = ' \0 ' ; if ( rename ( fn_osf , newname ) != 0 ) { LogError ( errno , RS_RET_IO_ERROR , " " " " , fn_osf , newname ) ; } else { LogMsg ( errno , RS_RET_OK , LOG_INFO , " " " " , fn_osf , newname ) ; } } return ; }
check if old osf points to a problem and, if so, report
check if old osf points to a problem and if so report
/* all ok! */