Blame src/lib/check_recent_crash_file.c

Packit 8ea169
/*
Packit 8ea169
    Copyright (C) 2012	RedHat inc.
Packit 8ea169
Packit 8ea169
    This program is free software; you can redistribute it and/or modify
Packit 8ea169
    it under the terms of the GNU General Public License as published by
Packit 8ea169
    the Free Software Foundation; either version 2 of the License, or
Packit 8ea169
    (at your option) any later version.
Packit 8ea169
Packit 8ea169
    This program is distributed in the hope that it will be useful,
Packit 8ea169
    but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8ea169
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8ea169
    GNU General Public License for more details.
Packit 8ea169
Packit 8ea169
    You should have received a copy of the GNU General Public License along
Packit 8ea169
    with this program; if not, write to the Free Software Foundation, Inc.,
Packit 8ea169
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Packit 8ea169
*/
Packit 8ea169
#include "libabrt.h"
Packit 8ea169
Packit 8ea169
/* I want to use -Werror, but gcc-4.4 throws a curveball:
Packit 8ea169
 * "warning: ignoring return value of 'ftruncate', declared with attribute warn_unused_result"
Packit 8ea169
 * and (void) cast is not enough to shut it up! Oh God...
Packit 8ea169
 */
Packit 8ea169
#define IGNORE_RESULT(func_call) do { if (func_call) /* nothing */; } while (0)
Packit 8ea169
Packit 8ea169
int check_recent_crash_file(const char *filename, const char *executable)
Packit 8ea169
{
Packit 8ea169
    int fd = open(filename, O_RDWR | O_CREAT, 0600);
Packit 8ea169
    if (fd < 0)
Packit 8ea169
        return 0;
Packit 8ea169
Packit 8ea169
    int ex_len = strlen(executable);
Packit 8ea169
    struct stat sb;
Packit 8ea169
    int sz;
Packit 8ea169
Packit 8ea169
    fstat(fd, &sb); /* !paranoia. this can't fail. */
Packit 8ea169
    if (sb.st_size != 0 /* if it wasn't created by us just now... */
Packit 8ea169
     && (unsigned)(time(NULL) - sb.st_mtime) < 20 /* and is relatively new [is 20 sec ok?] */
Packit 8ea169
    ) {
Packit 8ea169
        char buf[ex_len + 2];
Packit 8ea169
        sz = read(fd, buf, ex_len + 1);
Packit 8ea169
        if (sz > 0)
Packit 8ea169
        {
Packit 8ea169
            buf[sz] = '\0';
Packit 8ea169
            if (strcmp(executable, buf) == 0)
Packit 8ea169
            {
Packit 8ea169
                close(fd);
Packit 8ea169
                return 1;
Packit 8ea169
            }
Packit 8ea169
        }
Packit 8ea169
        lseek(fd, 0, SEEK_SET);
Packit 8ea169
    }
Packit 8ea169
    sz = write(fd, executable, ex_len);
Packit 8ea169
    if (sz >= 0)
Packit 8ea169
        IGNORE_RESULT(ftruncate(fd, sz));
Packit 8ea169
Packit 8ea169
    close(fd);
Packit 8ea169
    return 0;
Packit 8ea169
}