Skip to content

Commit

Permalink
Split ostree-mount-util.h and ostree-mount-util-static.h
Browse files Browse the repository at this point in the history
  • Loading branch information
ericcurtin committed Jul 20, 2023
1 parent 5593616 commit fda9919
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 88 deletions.
2 changes: 1 addition & 1 deletion Makefile-switchroot.am
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ systemdsystemgenerator_PROGRAMS = ostree-system-generator
GITIGNOREFILES += $(systemdsystemgenerator_PROGRAMS)
ostree_system_generator_SOURCES = src/switchroot/ostree-mount-util.h \
src/switchroot/ostree-system-generator.c
ostree_system_generator_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/libglnx -I$(srcdir)/src/libostree
ostree_system_generator_CPPFLAGS = $(AM_CPPFLAGS) -I$(srcdir)/libglnx -I$(srcdir)/src/libostree -I$(srcdir)/src/libotcore -I$(srcdir)/src/libotutil
ostree_system_generator_CFLAGS = $(AM_CFLAGS) $(OT_INTERNAL_GIO_UNIX_CFLAGS)
ostree_system_generator_LDADD = $(AM_LDFLAGS) libglnx.la libostree-1.la $(OT_INTERNAL_GIO_UNIX_LIBS)

Expand Down
13 changes: 13 additions & 0 deletions src/libotcore/otcore.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,16 @@ gboolean otcore_validate_ed25519_signature (GBytes *data, GBytes *pubkey, GBytes
#define OTCORE_RUN_BOOTED_KEY_COMPOSEFS_SIGNATURE "composefs.signed"
// This key will be present if the sysroot-ro flag was found
#define OTCORE_RUN_BOOTED_KEY_SYSROOT_RO "sysroot-ro"

typedef enum
{
OTCORE_BOOT_TYPE_OSTREE,
OTCORE_BOOT_TYPE_ANDROID,
} OtCoreBootType;

typedef struct
{
OtCoreBootType type;
char *ostree_link;
char android_boot_slot; // If type == ANDROID, this will be either 'a' or 'b'
} OtCoreBootEntry;
121 changes: 121 additions & 0 deletions src/switchroot/ostree-mount-util-static.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (C) 2023 Eric Curtin <ericcurtin17@gmail.com>
*
* 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, see <https://www.gnu.org/licenses/>.
*
*/

#ifndef __OSTREE_MOUNT_UTIL_STATIC_H_
#define __OSTREE_MOUNT_UTIL_STATIC_H_

#include <err.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/statvfs.h>
#include <unistd.h>

#define INITRAMFS_MOUNT_VAR "/run/ostree/initramfs-mount-var"
#define _OSTREE_SYSROOT_READONLY_STAMP "/run/ostree-sysroot-ro.stamp"

#define autofree __attribute__ ((cleanup (cleanup_free)))

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 void
cleanup_free (void *p)
{
void **pp = (void **)p;
free (*pp);
}

static inline char *
read_proc_cmdline_key (const char *key)
{
char *cmdline = NULL;
const char *iter;
char *ret = NULL;
size_t key_len = strlen (key);

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, key, key_len) == 0 && iter[key_len] == '=')
{
const char *start = iter + key_len + 1;
if (next)
ret = strndup (start, next - start);
else
ret = strdup (start);
break;
}
iter = next_nonspc;
}

free (cmdline);
return ret;
}

#endif /* __OSTREE_MOUNT_UTIL_STATIC_H_ */
87 changes: 2 additions & 85 deletions src/switchroot/ostree-mount-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,92 +32,9 @@
#include <sys/statvfs.h>
#include <unistd.h>

#define INITRAMFS_MOUNT_VAR "/run/ostree/initramfs-mount-var"
#define _OSTREE_SYSROOT_READONLY_STAMP "/run/ostree-sysroot-ro.stamp"
#define _OSTREE_COMPOSEFS_ROOT_STAMP "/run/ostree-composefs-root.stamp"

#define autofree __attribute__ ((cleanup (cleanup_free)))

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);
#include "ostree-mount-util-static.h"

if (cmdline[len - 1] == '\n')
cmdline[len - 1] = '\0';
out:
if (f)
fclose (f);
return cmdline;
}

static inline void
cleanup_free (void *p)
{
void **pp = (void **)p;
free (*pp);
}

static inline char *
read_proc_cmdline_key (const char *key)
{
char *cmdline = NULL;
const char *iter;
char *ret = NULL;
size_t key_len = strlen (key);

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, key, key_len) == 0 && iter[key_len] == '=')
{
const char *start = iter + key_len + 1;
if (next)
ret = strndup (start, next - start);
else
ret = strdup (start);
break;
}
iter = next_nonspc;
}

free (cmdline);
return ret;
}
#define _OSTREE_COMPOSEFS_ROOT_STAMP "/run/ostree-composefs-root.stamp"

static inline char *
get_aboot_root_slot (const char *slot_suffix)
Expand Down
4 changes: 2 additions & 2 deletions src/switchroot/ostree-prepare-root-static.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
// A temporary mount point
#define TMP_SYSROOT "/sysroot.tmp"

#include "ostree-mount-util.h"
#include "ostree-mount-util-static.h"

static inline bool
sysroot_is_configured_ro (const char *sysroot)
Expand Down Expand Up @@ -116,7 +116,7 @@ resolve_deploy_path (const char *root_mountpoint)
char destpath[PATH_MAX];
struct stat stbuf;
char *deploy_path;
autofree char *ostree_target = get_ostree_target ();
autofree char *ostree_target = read_proc_cmdline_key ("ostree");

if (snprintf (destpath, sizeof (destpath), "%s/%s", root_mountpoint, ostree_target) < 0)
err (EXIT_FAILURE, "failed to assemble ostree target path");
Expand Down
2 changes: 2 additions & 0 deletions src/switchroot/ostree-prepare-root.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ resolve_deploy_path (const char *root_mountpoint)
char destpath[PATH_MAX];
struct stat stbuf;
char *deploy_path;
OtCoreBootEntry ot_core_boot_entry = { OTCORE_BOOT_TYPE_ANDROID, "dsaj", 'a' };
ot_core_boot_entry.ostree_link = "jda";
autofree char *ostree_target = get_ostree_target ();
if (!ostree_target)
errx (EXIT_FAILURE, "No ostree= cmdline");
Expand Down
3 changes: 3 additions & 0 deletions src/switchroot/ostree-system-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "ostree-cmd-private.h"
#include "ostree-mount-util.h"

#include "otcore.h"

static const char *arg_dest = "/tmp";
static const char *arg_dest_late = "/tmp";

Expand Down Expand Up @@ -63,6 +65,7 @@ main (int argc, char *argv[])
* exit so that we don't error, but at the same time work where switchroot
* is PID 1 (and so hasn't created /run/ostree-booted).
*/
OtCoreBootEntry ot_core_boot_entry = { OTCORE_BOOT_TYPE_OSTREE, "", ' ' };
autofree char *ostree_target = get_ostree_target ();
if (!ostree_target)
exit (EXIT_SUCCESS);
Expand Down

0 comments on commit fda9919

Please sign in to comment.