Blame src/container-exception-logger.c

Packit 62eeb0
/*
Packit 62eeb0
 * Copyright (C) 2018  ABRT team
Packit 62eeb0
 * Copyright (C) 2018  RedHat Inc
Packit 62eeb0
 *
Packit 62eeb0
 * This program is free software; you can redistribute it and/or modify
Packit 62eeb0
 * it under the terms of the GNU General Public License as published by
Packit 62eeb0
 * the Free Software Foundation; either version 2 of the License, or
Packit 62eeb0
 * (at your option) any later version.
Packit 62eeb0
 *
Packit 62eeb0
 * This program is distributed in the hope that it will be useful,
Packit 62eeb0
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 62eeb0
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 62eeb0
 * GNU General Public License for more details.
Packit 62eeb0
 */
Packit 62eeb0
Packit 62eeb0
#include <stdio.h>
Packit 62eeb0
#include <unistd.h>
Packit 62eeb0
#include <sys/types.h>
Packit 62eeb0
#include <string.h>
Packit 62eeb0
Packit 62eeb0
#define INIT_PROC_STDERR_FD_PATH "/proc/1/fd/2"
Packit 62eeb0
#define DEFAULT_TAG "container-exception-logger"
Packit 62eeb0
Packit 62eeb0
int main(int argc, char *argv[])
Packit 62eeb0
{
Packit 62eeb0
    const char *program_usage_string =
Packit Service 4487e9
        "Usage: abrt-container-logger [--no-tag | --tag STRING | --help]"
Packit 62eeb0
        "\n"
Packit 62eeb0
        "\nThe tool reads from standard input and writes to '"INIT_PROC_STDERR_FD_PATH"'";
Packit 62eeb0
Packit 62eeb0
    int opt_notag = 0;
Packit 62eeb0
    const char * opt_tag = NULL;
Packit 62eeb0
    if (argc > 1)
Packit 62eeb0
    {
Packit 62eeb0
        for (int i = 1; i < argc; ++i)
Packit 62eeb0
        {
Packit 62eeb0
            if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
Packit 62eeb0
            {
Packit 62eeb0
                fprintf(stderr, "%s\n", program_usage_string);
Packit 62eeb0
                return 1;
Packit 62eeb0
            }
Packit 62eeb0
            else if (strcmp(argv[i], "--no-tag") == 0)
Packit 62eeb0
            {
Packit 62eeb0
                opt_notag = 1;
Packit 62eeb0
            }
Packit 62eeb0
            else if (strcmp(argv[i], "--tag") == 0
Packit 62eeb0
                     && i + 1 < argc
Packit 62eeb0
                     && argv[i + 1][0] != '-')
Packit 62eeb0
            {
Packit 62eeb0
                opt_tag = argv[++i];
Packit 62eeb0
            }
Packit 62eeb0
            else
Packit 62eeb0
            {
Packit 62eeb0
                fprintf(stderr, "%s\n", program_usage_string);
Packit 62eeb0
                return 1;
Packit 62eeb0
            }
Packit 62eeb0
        }
Packit 62eeb0
    }
Packit 62eeb0
Packit 62eeb0
    FILE *f = fopen(INIT_PROC_STDERR_FD_PATH, "w");
Packit 62eeb0
    if (f == NULL)
Packit 62eeb0
    {
Packit Service 4487e9
        perror("Failed to open '"INIT_PROC_STDERR_FD_PATH"' as root");
Packit Service 4487e9
Packit Service 4487e9
        /* Try to open the 'INIT_PROC_STDERR_FD_PATH' as normal user because of
Packit Service 4487e9
           https://github.com/minishift/minishift/issues/2058
Packit Service 4487e9
        */
Packit Service 4487e9
        if (seteuid(getuid()) == 0)
Packit Service 4487e9
        {
Packit Service 4487e9
            f = fopen(INIT_PROC_STDERR_FD_PATH, "w");
Packit Service 4487e9
            if (f == NULL)
Packit Service 4487e9
            {
Packit Service 4487e9
                perror("Failed to open '"INIT_PROC_STDERR_FD_PATH"' as user");
Packit Service 4487e9
                return 2;
Packit Service 4487e9
            }
Packit Service 4487e9
        }
Packit Service 4487e9
        else
Packit Service 4487e9
        {
Packit Service 4487e9
            perror("Failed to setuid");
Packit Service 4487e9
            return 3;
Packit Service 4487e9
        }
Packit 62eeb0
    }
Packit 62eeb0
Packit 62eeb0
    setvbuf (f, NULL, _IONBF, 0);
Packit 62eeb0
Packit 62eeb0
    if (!opt_notag)
Packit 62eeb0
        fprintf(f, "%s - ", opt_tag ? opt_tag : DEFAULT_TAG);
Packit 62eeb0
Packit 62eeb0
    char buffer[BUFSIZ];
Packit 62eeb0
    ssize_t bytes_read = 0;
Packit 62eeb0
Packit 62eeb0
    while (1)
Packit 62eeb0
    {
Packit 62eeb0
        bytes_read = read(0, buffer, sizeof buffer);
Packit 62eeb0
        if (bytes_read <= 0)
Packit 62eeb0
            break;
Packit 62eeb0
Packit 62eeb0
        if (fwrite(buffer, bytes_read, 1, f) != 1)
Packit 62eeb0
        {
Packit 62eeb0
            perror("Failed to write to '"INIT_PROC_STDERR_FD_PATH"'");
Packit 62eeb0
            fclose(f);
Packit Service 4487e9
            return 4;
Packit 62eeb0
        }
Packit 62eeb0
    }
Packit 62eeb0
    fclose(f);
Packit 62eeb0
Packit 62eeb0
    return 0;
Packit 62eeb0
}