]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/install+setup/install/hw.c
eb52f889d6e207bdb9d3e16ba0c3838960587944
1 /*#############################################################################
3 # IPFire - An Open Source Firewall Distribution #
4 # Copyright (C) 2014 IPFire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
26 #include <sys/mount.h>
31 struct hw
* hw_init() {
32 struct hw
* hw
= malloc(sizeof(*hw
));
36 hw
->udev
= udev_new();
38 fprintf(stderr
, "Could not create udev instance\n");
45 void hw_free(struct hw
* hw
) {
52 static int strstartswith(const char* a
, const char* b
) {
53 return (strncmp(a
, b
, strlen(b
)) == 0);
56 int hw_mount(const char* source
, const char* target
, int flags
) {
57 return mount(source
, target
, "iso9660", flags
, NULL
);
60 int hw_umount(const char* target
) {
61 return umount2(target
, MNT_DETACH
);
64 static int hw_test_source_medium(const char* path
) {
65 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, MS_RDONLY
);
67 // If the source could not be mounted we
72 // Check if the test file exists.
73 ret
= access(SOURCE_TEST_FILE
, F_OK
);
75 // Umount the test device.
76 hw_umount(SOURCE_MOUNT_PATH
);
81 char* hw_find_source_medium(struct hw
* hw
) {
84 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
86 udev_enumerate_add_match_subsystem(enumerate
, "block");
87 udev_enumerate_scan_devices(enumerate
);
89 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
91 struct udev_list_entry
* dev_list_entry
;
92 udev_list_entry_foreach(dev_list_entry
, devices
) {
93 const char* path
= udev_list_entry_get_name(dev_list_entry
);
94 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
96 const char* dev_path
= udev_device_get_devnode(dev
);
98 // Skip everything what we cannot work with
99 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
100 strstartswith(dev_path
, "/dev/ram"))
103 if (hw_test_source_medium(dev_path
)) {
104 ret
= strdup(dev_path
);
107 udev_device_unref(dev
);
109 // If a suitable device was found the search will end.
114 udev_enumerate_unref(enumerate
);