<commit_before>/*
 * libvirt-sandbox-init-common.c: common guest startup
 *
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <config.h>

#include <libvirt-sandbox/libvirt-sandbox-config-all.h>
#include <glib/gi18n.h>

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_CAPNG
#include <cap-ng.h>
#endif
#include <pwd.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <pty.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <poll.h>
#include <termios.h>
#include <unistd.h>
#include <limits.h>
#include <grp.h>

#include "libvirt-sandbox-rpcpacket.h"

static gboolean debug = FALSE;
static gboolean verbose = FALSE;
static int sigwrite;

#define ATTR_UNUSED __attribute__((__unused__))

static void sig_child(int sig ATTR_UNUSED)
{
    char ignore = '1';
    if (write(sigwrite, &ignore, 1) != 1)
        abort();
}

static gboolean setup_disk_tags(void) {
    FILE *fp;
    gboolean ret = FALSE;
    static char line[1024];
    if (debug)
        fprintf(stderr, "libvirt-sandbox-init-common: %s: populate /dev/disk/by-tag/\n",
                __func__);
    fp = fopen(SANDBOXCONFIGDIR "/disks.cfg", "r");
    if (fp == NULL) {
        fprintf(stderr, "libvirt-sandbox-init-common: %s: cannot open " SANDBOXCONFIGDIR "/disks.cfg: %s\n",
                __func__, strerror(errno));

        goto cleanup;
    }
    if (g_mkdir_with_parents("/dev/disk/by-tag",0755) < 0) {
       fprintf(stderr, "libvirt-sandbox-init-common: %s: cannot create the directory: %s\n",
               __func__, strerror(errno));

       goto cleanup;
    }
    while (fgets(line, sizeof line, fp)) {
        char *tag = line;
        char *device = strchr(tag, '\t');
        gchar *path = NULL;
        *device = '\0';
        device++;
        char *tmp = strchr(device, '\n');
        *tmp = '\0';
        path = g_strdup_printf("/dev/disk/by-tag/%s", tag);

        if (debug)
            fprintf(stderr, "libvirt-sandbox-init-common: %s: %s -> %s\n",
                    __func__, path, device);

        if (symlink(device, path) < 0) {
            fprintf(stderr, "libvirt-sandbox-init-common: %s: cannot create symlink %s -> %s: %s\n",
                    __func__, path, device, strerror(errno));
            g_free(path);
            goto cleanup;
        }

        g_free(path);
    }
    ret = TRUE;
 cleanup:
    fclose(fp);
    return ret;
}

static int
start_shell(void)
{
    pid_t pid;

    const gchar *devname = "/dev/ttyS1";
    if (access(devname, R_OK|W_OK) < 0) {
        devname = "/dev/tty2";
    }

    if (debug)
        fprintf(stderr, "libvirt-sandbox-init-common: starting shell %s\n", devname);

    pid = fork();
    if (pid < 0) {
        fprintf(stderr, "libvirt-sandbox-init-common: %s: cannot fork: %s\n",
                __func__, strerror(errno));
        return -1;
    }

    if (pid == 0) {
        const char *shellargv[] = { "/bin/sh", NULL };
        setsid();

        int tty = open(devname, O_RDWR);

        if (tty < 0) {
            fprintf(stderr, "libvirt-sandbox-init-common: %s: cannot open %s: %s\n",
                    __func__, devname, strerror(errno));
            exit(EXIT_FAILURE);
        }

        dup2(tty, STDIN_FILENO);
        dup2(tty, STDOUT_FILENO);
        dup2(tty, STDERR_FILENO);

        close(tty);

        execv(shellargv[0], (char**)shellargv);
        fprintf(stderr, "libvirt-sandbox-init-common: %s: cannot execute %s: %s\n",
                __func__, shellargv[0], strerror(errno));
        exit(EXIT_FAILURE);
    }

    return 0;
}


static gboolean start_dhcp(const gchar *devname, GError **error)
{
    const gchar *argv[] = { "/sbin/dhclient", "-v", "--no-pid", devname, NULL };
    gboolean ret;
    sigset_t newset;
    sigset_t oldset;

    sigemptyset(&newset);
    sigaddset(&newset, SIGHUP);

    sigprocmask(SIG_BLOCK, &newset, &oldset);
    ret = g_spawn_sync(NULL, (gchar**)argv, NULL, 0,
                       NULL, NULL, NULL, NULL, NULL, error);
    sigprocmask(SIG_SETMASK, &oldset, NULL);

    return ret;
}<commit_msg>Only show dhclient output in debug mode.<commit_after>/*
 * libvirt-sandbox-init-common.c: common guest startup
 *
 * Copyright (C) 2011 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Author: <NAME> <<EMAIL>>
 */

#include <config.h>

#include <libvirt-sandbox/libvirt-sandbox-config-all.h>
#include <glib/gi18n.h>

#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_CAPNG
#include <cap-ng.h>
#endif
#include <pwd.h>
#include <string.h>
#include <errno.h>
#include <sys/wait.h>
#include <pty.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <poll.h>
#include <termios.h>
#include <unistd.h>
#include <limits.h>
#include <grp.h>

#include "libvirt-sandbox-rpcpacket.h"

static gboolean debug = FALSE;
static gboolean verbose = FALSE;
static int sigwrite;

#define ATTR_UNUSED __attribute__((__unused__))

static void sig_child(int sig ATTR_UNUSED)
{
    char ignore = '1';
    if (write(sigwrite, &ignore, 1) != 1)
        abort();
}

static gboolean setup_disk_tags(void) {
    FILE *fp;
    gboolean ret = FALSE;
    static char line[1024];
    if (debug)
        fprintf(stderr, "libvirt-sandbox-init-common: %s: populate /dev/disk/by-tag/\n",
                __func__);
    fp = fopen(SANDBOXCONFIGDIR "/disks.cfg", "r");
    if (fp == NULL) {
        fprintf(stderr, "libvirt-sandbox-init-common: %s: cannot open " SANDBOXCONFIGDIR "/disks.cfg: %s\n",
                __func__, strerror(errno));

        goto cleanup;
    }
    if (g_mkdir_with_parents("/dev/disk/by-tag",0755) < 0) {
       fprintf(stderr, "libvirt-sandbox-init-common: %s: cannot create the directory: %s\n",
               __func__, strerror(errno));

       goto cleanup;
    }
    while (fgets(line, sizeof line, fp)) {
        char *tag = line;
        char *device = strchr(tag, '\t');
        gchar *path = NULL;
        *device = '\0';
        device++;
        char *tmp = strchr(device, '\n');
        *tmp = '\0';
        path = g_strdup_printf("/dev/disk/by-tag/%s", tag);

        if (debug)
            fprintf(stderr, "libvirt-sandbox-init-common: %s: %s -> %s\n",
                    __func__, path, device);

        if (symlink(device, path) < 0) {
            fprintf(stderr, "libvirt-sandbox-init-common: %s: cannot create symlink %s -> %s: %s\n",
                    __func__, path, device, strerror(errno));
            g_free(path);
            goto cleanup;
        }

        g_free(path);
    }
    ret = TRUE;
 cleanup:
    fclose(fp);
    return ret;
}

static int
start_shell(void)
{
    pid_t pid;

    const gchar *devname = "/dev/ttyS1";
    if (access(devname, R_OK|W_OK) < 0) {
        devname = "/dev/tty2";
    }

    if (debug)
        fprintf(stderr, "libvirt-sandbox-init-common: starting shell %s\n", devname);

    pid = fork();
    if (pid < 0) {
        fprintf(stderr, "libvirt-sandbox-init-common: %s: cannot fork: %s\n",
                __func__, strerror(errno));
        return -1;
    }

    if (pid == 0) {
        const char *shellargv[] = { "/bin/sh", NULL };
        setsid();

        int tty = open(devname, O_RDWR);

        if (tty < 0) {
            fprintf(stderr, "libvirt-sandbox-init-common: %s: cannot open %s: %s\n",
                    __func__, devname, strerror(errno));
            exit(EXIT_FAILURE);
        }

        dup2(tty, STDIN_FILENO);
        dup2(tty, STDOUT_FILENO);
        dup2(tty, STDERR_FILENO);

        close(tty);

        execv(shellargv[0], (char**)shellargv);
        fprintf(stderr, "libvirt-sandbox-init-common: %s: cannot execute %s: %s\n",
                __func__, shellargv[0], strerror(errno));
        exit(EXIT_FAILURE);
    }

    return 0;
}


static gboolean start_dhcp(const gchar *devname, GError **error)
{
    const gchar *argv[5];
    size_t nargs = 0;
    gboolean ret;
    sigset_t newset;
    sigset_t oldset;

    argv[nargs++] = "/sbin/dhclient";
    if (debug)
        argv[nargs++] = "-v";
    argv[nargs++] = "--no-pid";
    argv[nargs++] = devname;
    argv[nargs++] = NULL;

    sigemptyset(&newset);
    sigaddset(&newset, SIGHUP);

    sigprocmask(SIG_BLOCK, &newset, &oldset);
    ret = g_spawn_sync(NULL, (gchar**)argv, NULL, 0,
                       NULL, NULL, NULL, NULL, NULL, error);
    sigprocmask(SIG_SETMASK, &oldset, NULL);

    return ret;
}
{'fraction_non_alphanumeric': 0.14078800769776156, 'fraction_numerical': 0.009926060974374557, 'mean_word_length': 2.5749456915278786, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4246739', 'n_tokens_mistral': 3467, 'n_tokens_neox': 3346, 'n_words': 928}