<commit_before>/* ISC license. */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <pwd.h>
#include <skalibs/config.h>
#include <skalibs/uint64.h>
#include <skalibs/uint.h>
#include <skalibs/gidstuff.h>
#include <skalibs/bytestr.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr2.h>
#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>
#include <skalibs/sgetopt.h>
#include <skalibs/skamisc.h>

#define USAGE "s6-linux-init-maker [ -c basedir ] [ -l tmpfsdir ] [ -b execline_bindir ] [ -u log_user ] [ -g early_getty_cmd ] [ -2 stage2_script ] [ -r ] [ -3 stage3_script ] [ -p initial_path ] [ -m initial_umask ] [ -t timestamp_style ] [ -d dev_style ] [ -e initial_envvar ... ] dir"
#define dieusage() strerr_dieusage(100, USAGE)
#define dienomem() strerr_diefu1sys(111, "stralloc_catb") ;

#define BANNER "*\n* init created by s6-linux-init-maker\n* see http://skarnet.org/software/s6-linux-init/\n*\n"

#define CRASH_SCRIPT \
"redirfd -r 0 /dev/console\n" \
"redirfd -w 1 /dev/console\n" \
"fdmove -c 2 1\n" \
"foreground { s6-echo -- " \
"\"s6-svscan crashed. Dropping to an interactive shell.\" }\n" \
"/bin/sh -i\n"

static char const *slashrun = "/run" ;
static char const *robase = "/etc/s6-linux-init" ;
static char const *init_script = "/etc/rc.init" ;
static char const *shutdown_script = "/etc/rc.shutdown" ;
static char const *bindir = "/bin" ;
static char const *initial_path = SKALIBS_DEFAULTPATH ;
static char const *early_getty = 0 ;
static uid_t uncaught_logs_uid ;
static gid_t uncaught_logs_gid ;
static unsigned int initial_umask = 022 ;
static unsigned int timestamp_style = 1 ;
static unsigned int slashdev_style = 2 ;
static int redirect_stage2 = 0 ;

typedef int writetobuf_func_t (buffer *) ;
typedef writetobuf_func_t *writetobuf_func_t_ref ;

static int put_shebang (buffer *b)
{
  return buffer_puts(b, "#!") >= 0
   && buffer_puts(b, bindir) >= 0
   && buffer_puts(b, "/execlineb -P\n\n") >= 0 ;
}

static int early_getty_script (buffer *b)
{
  return put_shebang(b)
   && buffer_puts(b, early_getty) >= 0
   && buffer_put(b, "\n", 1) >= 0 ;
}

static int crash_script (buffer *b)
{
  return put_shebang(b)
   && buffer_puts(b, CRASH_SCRIPT) ;
}

static int s6_svscan_log_script (buffer *b)
{
  unsigned int sabase = satmp.len ;
  char fmt[UINT64_FMT] ;
  if (!put_shebang(b)
   || buffer_puts(b,
    "redirfd -w 2 /dev/console\n"
    "redirfd -w 1 /dev/null\n"
    "redirfd -rnb 0 fifo\n"
    "s6-applyuidgid -u ") < 0
   || buffer_put(b, fmt, uint64_fmt(fmt, uncaught_logs_uid)) < 0
   || buffer_puts(b, " -g ") < 0
   || buffer_put(b, fmt, gid_fmt(fmt, uncaught_logs_gid)) < 0
   || buffer_puts(b, " --\ns6-log -bp -- ") < 0
   || buffer_puts(b, timestamp_style & 1 ? "t " : "") < 0
   || buffer_puts(b, timestamp_style & 2 ? "T " : "") < 0) return 0 ;
  if (!string_quote(&satmp, slashrun, str_len(slashrun))) return 0 ;
  if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0)
  {
    satmp.len = sabase ;
    return 0 ;
  }
  satmp.len = sabase ;
  if (buffer_puts(b, "/uncaught-logs\n") < 0) return 0 ;
  return 1 ;
}

static int finish_script (buffer *b)
{
  unsigned int sabase = satmp.len ;
  if (buffer_puts(b, "#!") < 0
   || buffer_puts(b, bindir) < 0
   || buffer_puts(b, "/execlineb -S0\n\n"
    "cd /\nredirfd -w 2 /dev/console\nfdmove -c 1 2\nif { s6-svc -X -- ") < 0
   || !string_quote(&satmp, slashrun, str_len(slashrun))) return 0 ;
  if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ;
  satmp.len = sabase ;
  if (buffer_puts(b, "/service/s6-svscan-log }\nwait -r -- { }\n") < 0
   || !string_quote(&satmp, shutdown_script, str_len(shutdown_script))) return 0 ;
  if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ;
  satmp.len = sabase ;
  if (buffer_puts(b, " ${@}\n") < 0) return 0 ;
  return 1 ;<commit_msg> replace if with foreground in finish script<commit_after>/* ISC license. */

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <pwd.h>
#include <skalibs/config.h>
#include <skalibs/uint64.h>
#include <skalibs/uint.h>
#include <skalibs/gidstuff.h>
#include <skalibs/bytestr.h>
#include <skalibs/allreadwrite.h>
#include <skalibs/buffer.h>
#include <skalibs/strerr2.h>
#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>
#include <skalibs/sgetopt.h>
#include <skalibs/skamisc.h>

#define USAGE "s6-linux-init-maker [ -c basedir ] [ -l tmpfsdir ] [ -b execline_bindir ] [ -u log_user ] [ -g early_getty_cmd ] [ -2 stage2_script ] [ -r ] [ -3 stage3_script ] [ -p initial_path ] [ -m initial_umask ] [ -t timestamp_style ] [ -d dev_style ] [ -e initial_envvar ... ] dir"
#define dieusage() strerr_dieusage(100, USAGE)
#define dienomem() strerr_diefu1sys(111, "stralloc_catb") ;

#define BANNER "*\n* init created by s6-linux-init-maker\n* see http://skarnet.org/software/s6-linux-init/\n*\n"

#define CRASH_SCRIPT \
"redirfd -r 0 /dev/console\n" \
"redirfd -w 1 /dev/console\n" \
"fdmove -c 2 1\n" \
"foreground { s6-echo -- " \
"\"s6-svscan crashed. Dropping to an interactive shell.\" }\n" \
"/bin/sh -i\n"

static char const *slashrun = "/run" ;
static char const *robase = "/etc/s6-linux-init" ;
static char const *init_script = "/etc/rc.init" ;
static char const *shutdown_script = "/etc/rc.shutdown" ;
static char const *bindir = "/bin" ;
static char const *initial_path = SKALIBS_DEFAULTPATH ;
static char const *early_getty = 0 ;
static uid_t uncaught_logs_uid ;
static gid_t uncaught_logs_gid ;
static unsigned int initial_umask = 022 ;
static unsigned int timestamp_style = 1 ;
static unsigned int slashdev_style = 2 ;
static int redirect_stage2 = 0 ;

typedef int writetobuf_func_t (buffer *) ;
typedef writetobuf_func_t *writetobuf_func_t_ref ;

static int put_shebang (buffer *b)
{
  return buffer_puts(b, "#!") >= 0
   && buffer_puts(b, bindir) >= 0
   && buffer_puts(b, "/execlineb -P\n\n") >= 0 ;
}

static int early_getty_script (buffer *b)
{
  return put_shebang(b)
   && buffer_puts(b, early_getty) >= 0
   && buffer_put(b, "\n", 1) >= 0 ;
}

static int crash_script (buffer *b)
{
  return put_shebang(b)
   && buffer_puts(b, CRASH_SCRIPT) ;
}

static int s6_svscan_log_script (buffer *b)
{
  unsigned int sabase = satmp.len ;
  char fmt[UINT64_FMT] ;
  if (!put_shebang(b)
   || buffer_puts(b,
    "redirfd -w 2 /dev/console\n"
    "redirfd -w 1 /dev/null\n"
    "redirfd -rnb 0 fifo\n"
    "s6-applyuidgid -u ") < 0
   || buffer_put(b, fmt, uint64_fmt(fmt, uncaught_logs_uid)) < 0
   || buffer_puts(b, " -g ") < 0
   || buffer_put(b, fmt, gid_fmt(fmt, uncaught_logs_gid)) < 0
   || buffer_puts(b, " --\ns6-log -bp -- ") < 0
   || buffer_puts(b, timestamp_style & 1 ? "t " : "") < 0
   || buffer_puts(b, timestamp_style & 2 ? "T " : "") < 0) return 0 ;
  if (!string_quote(&satmp, slashrun, str_len(slashrun))) return 0 ;
  if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0)
  {
    satmp.len = sabase ;
    return 0 ;
  }
  satmp.len = sabase ;
  if (buffer_puts(b, "/uncaught-logs\n") < 0) return 0 ;
  return 1 ;
}

static int finish_script (buffer *b)
{
  unsigned int sabase = satmp.len ;
  if (buffer_puts(b, "#!") < 0
   || buffer_puts(b, bindir) < 0
   || buffer_puts(b, "/execlineb -S0\n\n"
    "cd /\nredirfd -w 2 /dev/console\nfdmove -c 1 2\nforeground { s6-svc -X -- ") < 0
   || !string_quote(&satmp, slashrun, str_len(slashrun))) return 0 ;
  if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ;
  satmp.len = sabase ;
  if (buffer_puts(b, "/service/s6-svscan-log }\nunexport ?\nwait -r -- { }\n") < 0
   || !string_quote(&satmp, shutdown_script, str_len(shutdown_script))) return 0 ;
  if (buffer_put(b, satmp.s + sabase, satmp.len - sabase) < 0) goto err ;
  satmp.len = sabase ;
  if (buffer_puts(b, " ${@}\n") < 0) return 0 ;
  return 1 ;
{'fraction_non_alphanumeric': 0.16554183114733223, 'fraction_numerical': 0.020629058958359862, 'mean_word_length': 3.576923076923077, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 47, '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': '1857827', 'n_tokens_mistral': 3442, 'n_tokens_neox': 3270, 'n_words': 758}