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
|
/*
* Copyright (C) 2011,2013 Colin Walters <walters@verbum.org>
*
* SPDX-License-Identifier: LGPL-2.0+
*
* 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 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
*/
#ifndef __OSTREE_MOUNT_UTIL_H_
#define __OSTREE_MOUNT_UTIL_H_
#include <err.h>
#include <stdlib.h>
#include <sys/statvfs.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdbool.h>
#define INITRAMFS_MOUNT_VAR "/run/ostree/initramfs-mount-var"
#define _OSTREE_SYSROOT_READONLY_STAMP "/run/ostree-sysroot-ro.stamp"
static inline int
path_is_on_readonly_fs (const char *path)
{
struct statvfs stvfsbuf;
if (statvfs (path, &stvfsbuf) == -1)
err (EXIT_FAILURE, "statvfs(%s)", path);
return (stvfsbuf.f_flag & ST_RDONLY) != 0;
}
static inline char *
read_proc_cmdline (void)
{
FILE *f = fopen("/proc/cmdline", "r");
char *cmdline = NULL;
size_t len;
if (!f)
goto out;
/* Note that /proc/cmdline will not end in a newline, so getline
* will fail unelss we provide a length.
*/
if (getline (&cmdline, &len, f) < 0)
goto out;
/* ... but the length will be the size of the malloc buffer, not
* strlen(). Fix that.
*/
len = strlen (cmdline);
if (cmdline[len-1] == '\n')
cmdline[len-1] = '\0';
out:
if (f)
fclose (f);
return cmdline;
}
static inline char *
read_proc_cmdline_ostree (void)
{
char *cmdline = NULL;
const char *iter;
char *ret = NULL;
cmdline = read_proc_cmdline ();
if (!cmdline)
err (EXIT_FAILURE, "failed to read /proc/cmdline");
iter = cmdline;
while (iter != NULL)
{
const char *next = strchr (iter, ' ');
const char *next_nonspc = next;
while (next_nonspc && *next_nonspc == ' ')
next_nonspc += 1;
if (strncmp (iter, "ostree=", strlen ("ostree=")) == 0)
{
const char *start = iter + strlen ("ostree=");
if (next)
ret = strndup (start, next - start);
else
ret = strdup (start);
break;
}
iter = next_nonspc;
}
free (cmdline);
return ret;
}
/* This is an API for other projects to determine whether or not the
* currently running system is ostree-controlled.
*/
static inline void
touch_run_ostree (void)
{
int fd = open ("/run/ostree-booted", O_CREAT | O_WRONLY | O_NOCTTY | O_CLOEXEC, 0640);
/* We ignore failures here in case /run isn't mounted...not much we
* can do about that, but we don't want to fail.
*/
if (fd == -1)
return;
(void) close (fd);
}
#endif /* __OSTREE_MOUNT_UTIL_H_ */
|