<commit_before>
#define _GNU_SOURCE

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <sys/stat.h>

#define CHILD_STACK_BYTES   (1024*1024)
#define NET_NAMESPACE_PATH  "/var/run/netns/ns0"
#define EXEC_PATH           "/bin/sh"
#define HOST_NAME           "my-container"
#define ROOT_FILESYSTEM     "/var/aufs/mnt"
#define PROC_FILESYSTEM     "/proc"

int child_fn(void *arg);

int main(int argc, char * const argv[])
{
  void *child_stack = malloc(CHILD_STACK_BYTES);

  pid_t child_pid = clone(child_fn, child_stack + CHILD_STACK_BYTES, 
      CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWUTS | SIGCHLD, NULL);
  if (child_pid == -1) {
    perror("error cloning process");
    exit(EXIT_FAILURE);
  }

  waitpid(child_pid, NULL, 0);
  exit(EXIT_SUCCESS);
}

int child_fn(void *arg)
{
  char * const argv[2] = { EXEC_PATH, NULL };

  char * proc_mnt = malloc(strlen(ROOT_FILESYSTEM) + strlen(PROC_FILESYSTEM) + 1);
  strncpy(proc_mnt, ROOT_FILESYSTEM, strlen(ROOT_FILESYSTEM) + 1);
  strncat(proc_mnt, PROC_FILESYSTEM, strlen(PROC_FILESYSTEM) + 1);

  int ns = open(NET_NAMESPACE_PATH, O_RDONLY);
  if (ns == -1) {
    perror("error opening network namespace handle");
    exit(EXIT_FAILURE);
  }

  if (setns(ns, CLONE_NEWNET) != 0) {
    perror("error setting network namespace");
    exit(EXIT_FAILURE);
  }

  if (close(ns) != 0) {
    perror("error closing network namespace handle");
  }
  
  if (mount(NULL, PROC_FILESYSTEM, NULL, MS_PRIVATE, NULL) != 0) {
    perror("error unsharing proc filesystem");
    exit(EXIT_FAILURE);
  }

  if (mount(NULL, ROOT_FILESYSTEM, NULL, MS_PRIVATE, NULL) != 0) {
    perror("error unsharing root filesystem");
  }

  if (mount("proc", proc_mnt, "proc", 0, NULL) != 0) {
    perror("error mounting proc filesystem");
    exit(EXIT_FAILURE);
  }

  if (chroot(ROOT_FILESYSTEM) != 0) {
    perror("error changing root filesystem");
    exit(EXIT_FAILURE);
  }<commit_msg>use dstring to create proc mount point path<commit_after>
#define _GNU_SOURCE

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <sys/stat.h>

#include "dstring.h"

#define CHILD_STACK_BYTES   (1024*1024)
#define NET_NAMESPACE_PATH  "/var/run/netns/ns0"
#define EXEC_PATH           "/bin/sh"
#define HOST_NAME           "my-container"
#define AUFS_LAYERS_PATH    "/var/aufs/layers"
#define AUFS_CONFIG_PATH    "/var/aufs/config"
#define AUFS_CONTAINER_PATH "/var/aufs/container"

#define ROOT_MOUNT_POINT    "/var/aufs/mnt"
#define PROC_PATH           "/proc"

int child_fn(void *arg);

int main(int argc, char * const argv[])
{
  void *child_stack = malloc(CHILD_STACK_BYTES);

  pid_t child_pid = clone(child_fn, child_stack + CHILD_STACK_BYTES, 
      CLONE_NEWPID | CLONE_NEWNS | CLONE_NEWUTS | SIGCHLD, NULL);
  if (child_pid == -1) {
    perror("error cloning process");
    exit(EXIT_FAILURE);
  }

  waitpid(child_pid, NULL, 0);
  exit(EXIT_SUCCESS);
}

int child_fn(void *arg)
{
  char * const argv[2] = { EXEC_PATH, NULL };

  int ns = open(NET_NAMESPACE_PATH, O_RDONLY);
  if (ns == -1) {
    perror("error opening network namespace handle");
    exit(EXIT_FAILURE);
  }

  if (setns(ns, CLONE_NEWNET) != 0) {
    perror("error setting network namespace");
    exit(EXIT_FAILURE);
  }

  if (close(ns) != 0) {
    perror("error closing network namespace handle");
  }
  
  if (mount(NULL, PROC_PATH, NULL, MS_PRIVATE, NULL) != 0) {
    perror("error unsharing proc filesystem");
    exit(EXIT_FAILURE);
  }

  if (mount(NULL, ROOT_MOUNT_POINT, NULL, MS_PRIVATE, NULL) != 0) {
    perror("error unsharing root filesystem");
  }

  String *proc_mnt = dstr_init(ROOT_MOUNT_POINT);
  dstr_append(proc_mnt, PROC_PATH);

  if (mount("proc", dstr_text(proc_mnt), "proc", 0, NULL) != 0) {
    perror("error mounting proc filesystem");
    exit(EXIT_FAILURE);
  }
  dstr_free(proc_mnt);

  if (chroot(ROOT_MOUNT_POINT) != 0) {
    perror("error changing root filesystem");
    exit(EXIT_FAILURE);
  }
{'fraction_non_alphanumeric': 0.14336324988190835, 'fraction_numerical': 0.010155880963627776, 'mean_word_length': 4.065789473684211, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '24559', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1628, 'n_words': 351}