-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split ostree-mount-util.h and ostree-mount-util-static.h
- Loading branch information
1 parent
5593616
commit fda9919
Showing
7 changed files
with
144 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters