Skip to content

Commit

Permalink
fwupd port for BSD distros
Browse files Browse the repository at this point in the history
Signed-off-by: Norbert Kamiński <norbert.kaminski@3mdeb.com>
  • Loading branch information
Asiderr authored and hughsie committed Mar 19, 2021
1 parent a5966f7 commit 508bb99
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 24 deletions.
35 changes: 22 additions & 13 deletions libfwupd/fwupd-common.c
Expand Up @@ -152,31 +152,40 @@ fwupd_checksum_get_best (GPtrArray *checksums)
GHashTable *
fwupd_get_os_release (GError **error)
{
GHashTable *hash;
const gchar *filename = NULL;
const gchar *paths[] = { "/etc/os-release", "/usr/lib/os-release", NULL };
g_autofree gchar *buf = NULL;
g_auto(GStrv) lines = NULL;
g_autoptr(GHashTable) hash = NULL;

g_return_val_if_fail (error == NULL || *error == NULL, NULL);

/* TODO: Read the Windows version */
#ifdef _WIN32
hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert (hash,
g_strdup("OS"),
g_strdup("Windows"));
return hash;
#endif

/* find the correct file */
for (guint i = 0; paths[i] != NULL; i++) {
if (g_file_test (paths[i], G_FILE_TEST_EXISTS)) {
filename = paths[i];
break;
}
}

hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
if (filename == NULL) {
#if defined(_WIN32)
/* TODO: Read the Windows version */
g_hash_table_insert (hash,
g_strdup ("OS"),
g_strdup ("Windows"));
#elif defined(__NetBSD__)
g_hash_table_insert (hash,
g_strdup ("OS"),
g_strdup ("NetBSD"));
#elif defined(__OpenBSD__)
g_hash_table_insert (hash,
g_strdup ("OS"),
g_strdup ("OpenBSD"));
#endif
if (g_hash_table_size (hash) > 0)
return g_steal_pointer (&hash);
g_set_error_literal (error,
FWUPD_ERROR,
FWUPD_ERROR_READ,
Expand All @@ -187,7 +196,6 @@ fwupd_get_os_release (GError **error)
/* load each line */
if (!g_file_get_contents (filename, &buf, NULL, error))
return NULL;
hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
lines = g_strsplit (buf, "\n", -1);
for (guint i = 0; lines[i] != NULL; i++) {
gsize len, off = 0;
Expand All @@ -210,7 +218,7 @@ fwupd_get_os_release (GError **error)
g_strdup (split[0]),
g_strndup (split[1] + off, len));
}
return hash;
return g_steal_pointer (&hash);
}

static gchar *
Expand Down Expand Up @@ -346,7 +354,7 @@ fwupd_build_machine_id (const gchar *salt, GError **error)
{
const gchar *fn = NULL;
g_autofree gchar *buf = NULL;
g_auto(GStrv) fns = g_new0 (gchar *, 5);
g_auto(GStrv) fns = g_new0 (gchar *, 6);
g_autoptr(GChecksum) csum = NULL;
gsize sz = 0;

Expand All @@ -358,6 +366,7 @@ fwupd_build_machine_id (const gchar *salt, GError **error)
fns[1] = g_build_filename (FWUPD_LOCALSTATEDIR, "lib", "dbus", "machine-id", NULL);
fns[2] = g_strdup ("/etc/machine-id");
fns[3] = g_strdup ("/var/lib/dbus/machine-id");
fns[4] = g_strdup ("/var/db/dbus/machine-id");
for (guint i = 0; fns[i] != NULL; i++) {
if (g_file_test (fns[i], G_FILE_TEST_EXISTS)) {
fn = fns[i];
Expand Down
1 change: 1 addition & 0 deletions libfwupd/meson.build
Expand Up @@ -154,6 +154,7 @@ if get_option('introspection')
input: fwupd_gir[0],
output: 'fwupd.map',
command: [
python3,
join_paths(meson.source_root(), 'contrib', 'generate-version-script.py'),
'LIBFWUPD',
'@INPUT@',
Expand Down
2 changes: 1 addition & 1 deletion libfwupdplugin/fu-common.c
Expand Up @@ -2573,7 +2573,7 @@ fu_byte_array_align_up (GByteArray *array, guint8 alignment, guint8 data)
gboolean
fu_common_kernel_locked_down (void)
{
#ifndef _WIN32
#ifdef __linux__
gsize len = 0;
g_autofree gchar *dir = fu_common_get_path (FU_PATH_KIND_SYSFSDIR_SECURITY);
g_autofree gchar *fname = g_build_filename (dir, "lockdown", NULL);
Expand Down
12 changes: 6 additions & 6 deletions libfwupdplugin/fu-efivar.c
Expand Up @@ -10,7 +10,7 @@
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#ifndef _WIN32
#ifdef __linux__
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <gio/gunixinputstream.h>
Expand Down Expand Up @@ -49,7 +49,7 @@ fu_efivar_get_filename (const gchar *guid, const gchar *name)
gboolean
fu_efivar_supported (GError **error)
{
#ifndef _WIN32
#ifdef __linux__
g_autofree gchar *efivardir = fu_efivar_get_path ();
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
if (!g_file_test (efivardir, G_FILE_TEST_IS_DIR)) {
Expand All @@ -71,7 +71,7 @@ fu_efivar_supported (GError **error)
#endif
}

#ifndef _WIN32
#ifdef __linux__
static gboolean
fu_efivar_set_immutable_fd (int fd,
gboolean value,
Expand Down Expand Up @@ -135,7 +135,7 @@ fu_efivar_set_immutable (const gchar *fn,
gboolean *value_old,
GError **error)
{
#ifndef _WIN32
#ifdef __linux__
gint fd;
g_autoptr(GInputStream) istr = NULL;

Expand Down Expand Up @@ -298,7 +298,7 @@ gboolean
fu_efivar_get_data (const gchar *guid, const gchar *name, guint8 **data,
gsize *data_sz, guint32 *attr, GError **error)
{
#ifndef _WIN32
#ifdef __linux__
gssize attr_sz;
gssize data_sz_tmp;
guint32 attr_tmp;
Expand Down Expand Up @@ -546,7 +546,7 @@ gboolean
fu_efivar_set_data (const gchar *guid, const gchar *name, const guint8 *data,
gsize sz, guint32 attr, GError **error)
{
#ifndef _WIN32
#ifdef __linux__
int fd;
int open_wflags;
gboolean was_immutable;
Expand Down
2 changes: 1 addition & 1 deletion libfwupdplugin/fu-udev-device.c
Expand Up @@ -1669,7 +1669,7 @@ gboolean
fu_udev_device_write_sysfs (FuUdevDevice *self, const gchar *attribute,
const gchar *val, GError **error)
{
#ifndef _WIN32
#ifdef __linux__
ssize_t n;
int r;
int fd;
Expand Down
1 change: 1 addition & 0 deletions libfwupdplugin/meson.build
Expand Up @@ -220,6 +220,7 @@ if get_option('introspection') and get_option('gusb')
input: fwupdplugin_gir[0],
output: 'fwupdplugin.map',
command: [
python3,
join_paths(meson.source_root(), 'contrib', 'generate-version-script.py'),
'LIBFWUPDPLUGIN',
'@INPUT@',
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Expand Up @@ -242,7 +242,7 @@ if build_daemon
warning('Polkit is disabled, the daemon will allow ALL client actions')
endif
udevdir = get_option('udevdir')
if udevdir == ''
if udevdir == '' and host_machine.system() == 'linux'
udev = dependency('udev')
udevdir = udev.get_pkgconfig_variable('udevdir')
endif
Expand Down
2 changes: 1 addition & 1 deletion src/fu-engine.c
Expand Up @@ -6358,7 +6358,7 @@ fu_engine_load (FuEngine *self, FuEngineLoadFlags flags, GError **error)
guint backend_cnt = 0;
g_autoptr(GPtrArray) checksums_approved = NULL;
g_autoptr(GPtrArray) checksums_blocked = NULL;
#ifndef _WIN32
#ifdef __linux__
g_autoptr(GError) error_local = NULL;
#endif

Expand Down
2 changes: 2 additions & 0 deletions src/fu-main.c
Expand Up @@ -2041,8 +2041,10 @@ main (int argc, char *argv[])
else if (timed_exit)
g_timeout_add_seconds (5, fu_main_timed_exit_cb, priv->loop);

#ifdef __linux__
/* drop heap except one page */
malloc_trim (4096);
#endif

/* wait */
g_message ("Daemon ready for requests (locale %s)", g_getenv ("LANG"));
Expand Down
2 changes: 1 addition & 1 deletion src/fu-self-test.c
Expand Up @@ -583,7 +583,7 @@ fu_engine_requirements_device_func (gconstpointer user_data)
" <firmware compare=\"ge\" version=\"1.2.3\"/>"
" <firmware compare=\"eq\" version=\"4.5.6\">bootloader</firmware>"
" <firmware compare=\"regex\" version=\"USB:0xFFFF|DMI:Lenovo\">vendor-id</firmware>"
#ifndef _WIN32
#ifdef __linux__
" <id compare=\"ge\" version=\"4.0.0\">org.kernel</id>"
#endif
" </requires>"
Expand Down

0 comments on commit 508bb99

Please sign in to comment.