summaryrefslogtreecommitdiff
path: root/gnomeos/yocto/recipies-core/ostree-init/ostree-init.c
blob: deeb330a7a92b3fc0213dcacf1c358b7f0fd5038 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* -*- c-file-style: "gnu" -*-
 * ostree-init.c - switch to new root directory and start init.
 * Copyright 2011 Colin Walters <walters@verbum.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#define _GNU_SOURCE
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <fcntl.h>
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <dirent.h>

#define INIT_PATH "/sbin/init"

static int
perrorv (const char *format, ...) __attribute__ ((format (printf, 1, 2)));

static int
perrorv (const char *format, ...)
{
  va_list args;
  char buf[PATH_MAX];
  char *p;

  p = strerror_r (errno, buf, sizeof (buf));

  va_start (args, format);

  vfprintf (stderr, format, args);
  fprintf (stderr, ": %s\n", p);
  fflush (stderr);

  va_end (args);

  sleep (3);
	
  return 0;
}

int main(int argc, char *argv[])
{
  FILE *cmdline_f = NULL;
  const char *ostree_root = NULL;
  const char *p = NULL;
  size_t bytes_read;
  size_t buf_size;
  size_t buf_used;
  char destpath[PATH_MAX];
  char *buf;
  struct stat stbuf;
  char **init_argv = NULL;
  int i;

  cmdline_f = fopen ("/proc/cmdline", "r");
  if (!cmdline_f)
    {
      perrorv ("Failed to open /proc/cmdline");
      return 1;
    }

  buf_size = 8;
  buf_used = 0;
  buf = malloc (buf_size);
  assert (buf);

  while ((bytes_read = fread (buf + buf_used, 1, buf_size - buf_used, cmdline_f)) > 0)
    {
      buf_used += bytes_read;
      if (buf_size == buf_used)
	{
	  buf_size *= 2;
	  buf = realloc (buf, buf_size);
	  assert (buf);
	}
    }
  if (bytes_read < 0)
    {
      perrorv ("Failed to read from /proc/cmdline");
      exit (1);
    }

  for (p = buf; *p; p += strlen (p) + 1)
    {
      if (!strcmp (p, "ostree="))
	{
	  ostree_root = p + strlen ("ostree=");
	  break;
	}
    }

  if (ostree_root)
    {
      snprintf (destpath, sizeof(destpath), "/ostree/%s", ostree_root);
      if (stat (destpath, &stbuf) < 0)
	{
	  perrorv ("Invalid ostree root '%s'", destpath);
	  exit (1);
	}

      snprintf (destpath, sizeof(destpath), "/ostree/%s/sysroot", ostree_root);
      if (mount ("/", destpath, NULL, MS_BIND, NULL) < 0)
	{
	  perrorv ("Failed to bind mount / to '%s'", destpath);
	  exit (1);
	}

      snprintf (destpath, sizeof(destpath), "/ostree/%s", ostree_root);
      if (chroot (destpath) < 0)
	{
	  perrorv ("failed to change root to '%s'", destpath);
	  exit (1);
	}

      if (chdir ("/") < 0)
	{
	  perrorv ("failed to chdir to subroot");
	  exit (1);
	}
      
    }
  else
    {
      fprintf (stderr, "No ostree= argument specified\n");
      exit (1);
    }

  init_argv = malloc (sizeof (char*)*(argc+1));
  init_argv[0] = INIT_PATH;
  for (i = 1; i < argc; i++)
    init_argv[i] = argv[i];
  init_argv[i] = NULL;
  
  execv (INIT_PATH, init_argv);
  perrorv ("Failed to exec init '%s'", INIT_PATH);
  exit (1);
}