<commit_before>/*
 * Copyright © 2014 Red Hat, Inc
 *
 * This program 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 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       <NAME> <<EMAIL>>
 */

#include "config.h"

#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include "libgsystem.h"
#include "libglnx/libglnx.h"

#include "xdg-app-builtins.h"
#include "xdg-app-utils.h"
#include "xdg-app-dbus.h"
#include "xdg-app-run.h"


static GOptionEntry options[] = {
  { NULL }
};

static gboolean
write_to_file (int fd, const char *content, ssize_t len)
{
  ssize_t res;

  while (len > 0)
    {
      res = write (fd, content, len);
      if (res < 0 && errno == EINTR)
	continue;
      if (res <= 0)
	return FALSE;
      len -= res;
      content += res;
    }

  return TRUE;
}

static gboolean
write_file (const char *path, const char *content)
{
  int fd;
  gboolean res;
  int errsv;

  fd = open (path, O_RDWR | O_CLOEXEC, 0);
  if (fd == -1)
    return FALSE;

  res = TRUE;
  if (content)
    res = write_to_file (fd, content, strlen (content));

  errsv = errno;
  close (fd);
  errno = errsv;

  return res;
}

static uid_t uid;
static gid_t gid;

static void
child_setup (gpointer user_data)
{
#ifndef DISABLE_USERNS
  g_autofree char *uid_map = NULL;
  g_autofree char *gid_map = NULL;
        
  /* Work around user namespace devpts issue by creating a new
     userspace and map our uid like the helper does */

  if (unshare (CLONE_NEWUSER))
    {
      g_warning ("Can't unshare user namespace: %s", strerror (errno));
      return;
    }

  uid_map = g_strdup_printf ("%d 0 1\n", uid);
  if (!write_file ("/proc/self/uid_map", uid_map))
    g_warning ("setting up uid map");

  gid_map = g_strdup_printf ("%d 0 1\n", gid);
  if (!write_file ("/proc/self/gid_map", gid_map))
    g_warning ("setting up gid map");
<commit_msg>app: Fix warning if building with --disable-userns<commit_after>/*
 * Copyright © 2014 Red Hat, Inc
 *
 * This program 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 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       <NAME> <<EMAIL>>
 */

#include "config.h"

#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include "libgsystem.h"
#include "libglnx/libglnx.h"

#include "xdg-app-builtins.h"
#include "xdg-app-utils.h"
#include "xdg-app-dbus.h"
#include "xdg-app-run.h"


static GOptionEntry options[] = {
  { NULL }
};

#ifndef DISABLE_USERNS
static gboolean
write_to_file (int fd, const char *content, ssize_t len)
{
  ssize_t res;

  while (len > 0)
    {
      res = write (fd, content, len);
      if (res < 0 && errno == EINTR)
	continue;
      if (res <= 0)
	return FALSE;
      len -= res;
      content += res;
    }

  return TRUE;
}

static gboolean
write_file (const char *path, const char *content)
{
  int fd;
  gboolean res;
  int errsv;

  fd = open (path, O_RDWR | O_CLOEXEC, 0);
  if (fd == -1)
    return FALSE;

  res = TRUE;
  if (content)
    res = write_to_file (fd, content, strlen (content));

  errsv = errno;
  close (fd);
  errno = errsv;

  return res;
}
#endif

static uid_t uid;
static gid_t gid;

static void
child_setup (gpointer user_data)
{
#ifndef DISABLE_USERNS
  g_autofree char *uid_map = NULL;
  g_autofree char *gid_map = NULL;
        
  /* Work around user namespace devpts issue by creating a new
     userspace and map our uid like the helper does */

  if (unshare (CLONE_NEWUSER))
    {
      g_warning ("Can't unshare user namespace: %s", strerror (errno));
      return;
    }

  uid_map = g_strdup_printf ("%d 0 1\n", uid);
  if (!write_file ("/proc/self/uid_map", uid_map))
    g_warning ("setting up uid map");

  gid_map = g_strdup_printf ("%d 0 1\n", gid);
  if (!write_file ("/proc/self/gid_map", gid_map))
    g_warning ("setting up gid map");

{'fraction_non_alphanumeric': 0.11692185325114336, 'fraction_numerical': 0.005567707297673493, 'mean_word_length': 3.2447257383966246, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6173016', 'n_tokens_mistral': 1778, 'n_tokens_neox': 1713, 'n_words': 619}