Blame pqos/common.c

Packit Service 8a4b7a
/*
Packit Service 8a4b7a
 * BSD LICENSE
Packit Service 8a4b7a
 *
Packit Service 8a4b7a
 * Copyright(c) 2019-2020 Intel Corporation. All rights reserved.
Packit Service 8a4b7a
 * All rights reserved.
Packit Service 8a4b7a
 *
Packit Service 8a4b7a
 * Redistribution and use in source and binary forms, with or without
Packit Service 8a4b7a
 * modification, are permitted provided that the following conditions
Packit Service 8a4b7a
 * are met:
Packit Service 8a4b7a
 *
Packit Service 8a4b7a
 *   * Redistributions of source code must retain the above copyright
Packit Service 8a4b7a
 *     notice, this list of conditions and the following disclaimer.
Packit Service 8a4b7a
 *   * Redistributions in binary form must reproduce the above copyright
Packit Service 8a4b7a
 *     notice, this list of conditions and the following disclaimer in
Packit Service 8a4b7a
 *     the documentation and/or other materials provided with the
Packit Service 8a4b7a
 *     distribution.
Packit Service 8a4b7a
 *   * Neither the name of Intel Corporation nor the names of its
Packit Service 8a4b7a
 *     contributors may be used to endorse or promote products derived
Packit Service 8a4b7a
 *     from this software without specific prior written permission.
Packit Service 8a4b7a
 *
Packit Service 8a4b7a
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 8a4b7a
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 8a4b7a
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit Service 8a4b7a
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit Service 8a4b7a
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit Service 8a4b7a
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit Service 8a4b7a
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 8a4b7a
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 8a4b7a
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 8a4b7a
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit Service 8a4b7a
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.O
Packit Service 8a4b7a
 *
Packit Service 8a4b7a
 */
Packit Service 8a4b7a
Packit Service 8a4b7a
#include <stdio.h>
Packit Service 8a4b7a
#include <sys/stat.h>
Packit Service 8a4b7a
#include <fcntl.h>
Packit Service 8a4b7a
#include <unistd.h>
Packit Service 8a4b7a
Packit Service 8a4b7a
#include "common.h"
Packit Service 8a4b7a
Packit Service 8a4b7a
FILE *
Packit Service 8a4b7a
pqos_fopen(const char *name, const char *mode)
Packit Service 8a4b7a
{
Packit Service 8a4b7a
        int fd;
Packit Service 8a4b7a
        FILE *stream = NULL;
Packit Service 8a4b7a
        struct stat lstat_val;
Packit Service 8a4b7a
        struct stat fstat_val;
Packit Service 8a4b7a
Packit Service 8a4b7a
        stream = fopen(name, mode);
Packit Service 8a4b7a
        if (stream == NULL)
Packit Service 8a4b7a
                return stream;
Packit Service 8a4b7a
Packit Service 8a4b7a
        fd = fileno(stream);
Packit Service 8a4b7a
        if (fd == -1)
Packit Service 8a4b7a
                goto pqos_fopen_error;
Packit Service 8a4b7a
Packit Service 8a4b7a
        /* collect any link info about the file */
Packit Service 8a4b7a
        if (lstat(name, &lstat_val) == -1)
Packit Service 8a4b7a
                goto pqos_fopen_error;
Packit Service 8a4b7a
Packit Service 8a4b7a
        /* collect info about the opened file */
Packit Service 8a4b7a
        if (fstat(fd, &fstat_val) == -1)
Packit Service 8a4b7a
                goto pqos_fopen_error;
Packit Service 8a4b7a
Packit Service 8a4b7a
        /* we should not have followed a symbolic link */
Packit Service 8a4b7a
        if (lstat_val.st_mode != fstat_val.st_mode ||
Packit Service 8a4b7a
            lstat_val.st_ino != fstat_val.st_ino ||
Packit Service 8a4b7a
            lstat_val.st_dev != fstat_val.st_dev) {
Packit Service 8a4b7a
                printf("File %s is a symlink\n", name);
Packit Service 8a4b7a
                goto pqos_fopen_error;
Packit Service 8a4b7a
        }
Packit Service 8a4b7a
Packit Service 8a4b7a
        return stream;
Packit Service 8a4b7a
Packit Service 8a4b7a
pqos_fopen_error:
Packit Service 8a4b7a
        if (stream != NULL)
Packit Service 8a4b7a
                fclose(stream);
Packit Service 8a4b7a
Packit Service 8a4b7a
        return NULL;
Packit Service 8a4b7a
}
Packit Service 8a4b7a
Packit Service 8a4b7a
int
Packit Service 8a4b7a
pqos_open(const char *pathname, int flags, mode_t mode)
Packit Service 8a4b7a
{
Packit Service 8a4b7a
        int fd;
Packit Service 8a4b7a
        struct stat lstat_val;
Packit Service 8a4b7a
        struct stat fstat_val;
Packit Service 8a4b7a
Packit Service 8a4b7a
        /* open the file */
Packit Service 8a4b7a
        fd = open(pathname, flags, mode);
Packit Service 8a4b7a
        if (fd == -1)
Packit Service 8a4b7a
                return fd;
Packit Service 8a4b7a
Packit Service 8a4b7a
        /* collect any link info about the file */
Packit Service 8a4b7a
        if (lstat(pathname, &lstat_val) == -1)
Packit Service 8a4b7a
                goto pqos_open_error;
Packit Service 8a4b7a
Packit Service 8a4b7a
        /* collect info about the opened file */
Packit Service 8a4b7a
        if (fstat(fd, &fstat_val) == -1)
Packit Service 8a4b7a
                goto pqos_open_error;
Packit Service 8a4b7a
Packit Service 8a4b7a
        /* we should not have followed a symbolic link */
Packit Service 8a4b7a
        if (lstat_val.st_mode != fstat_val.st_mode ||
Packit Service 8a4b7a
            lstat_val.st_ino != fstat_val.st_ino ||
Packit Service 8a4b7a
            lstat_val.st_dev != fstat_val.st_dev) {
Packit Service 8a4b7a
                printf("File %s is a symlink\n", pathname);
Packit Service 8a4b7a
                goto pqos_open_error;
Packit Service 8a4b7a
        }
Packit Service 8a4b7a
Packit Service 8a4b7a
        return fd;
Packit Service 8a4b7a
Packit Service 8a4b7a
pqos_open_error:
Packit Service 8a4b7a
        if (fd != -1)
Packit Service 8a4b7a
                close(fd);
Packit Service 8a4b7a
Packit Service 8a4b7a
        return -1;
Packit Service 8a4b7a
}