File: arch-freebsd.c

package info (click to toggle)
diskscan 0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,656 kB
  • sloc: ansic: 11,136; python: 338; xml: 138; sh: 41; makefile: 34
file content (115 lines) | stat: -rw-r--r-- 2,385 bytes parent folder | download | duplicates (6)
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
#include "arch.h"

#include <sys/disk.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>

#include "arch-posix.c"
#include <net/if.h>

int disk_dev_identify(disk_dev_t *dev, char *vendor, char *model, char *fw_rev, char *serial, bool *is_ata, unsigned char *ata_buf, unsigned *ata_buf_len)
{
	(void)dev;
	strcpy(vendor, "UNKNOWN");
	strcpy(model, "UNKNOWN");
	strcpy(fw_rev, "UNKN");
	strcpy(serial, "UNKNOWN");
	*is_ata = 0;
	*ata_buf_len = 0;
	*ata_buf = 0;
	return 0;
}

int disk_dev_read_cap(disk_dev_t *dev, uint64_t *size_bytes, uint64_t *sector_size)
{
	if (ioctl(dev->fd, DIOCGMEDIASIZE, size_bytes) < 0) {
		return -1;
	}

	if (ioctl(dev->fd, DIOCGSECTORSIZE, sector_size) < 0) {
		return -1;
	}

	return 0;
}

disk_mount_e disk_dev_mount_state(const char *path)
{
	int num_mounts;
	struct statfs *mntbuf;
	disk_mount_e last_state;
	int i;

	num_mounts = getmntinfo(&mntbuf, MNT_WAIT);
	if (num_mounts == 0) {
		ERROR("Failed to get the mount information, errno=%d", errno);
		return DISK_MOUNTED_RW;
	}

	last_state = DISK_NOT_MOUNTED;
	for (i = 0; i < num_mounts; i++) {
		struct statfs *mnt = &mntbuf[i];

		if (strncmp(path, mnt->f_mntfromname, strlen(path)) == 0) {
			disk_mount_e cur_state = DISK_NOT_MOUNTED;
			if (mnt->f_flags == MNT_RDONLY)
				cur_state = DISK_MOUNTED_RO;
			else
				cur_state = DISK_MOUNTED_RW;

			if (cur_state > last_state)
				last_state = cur_state;
		}
	}

	return last_state;
}
void mac_read(unsigned char *buf, int len)
{
	struct ifreq ifr;
	struct ifconf ifc;
	char data[1024];
	int success = 0;

	buf[0] = 0;

	int sock = socket(AF_INET, SOCK_DGRAM, 0);
	if (sock == -1) {
		return;
	};

	ifc.ifc_len = sizeof(data);
	ifc.ifc_buf = data;
	if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) {
		/* handle error */
		goto Exit;
	}

	struct ifreq* it = ifc.ifc_req;
	const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));

	for (; it != end; ++it) {
		strcpy(ifr.ifr_name, it->ifr_name);
		if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
			if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback
				if (ioctl(sock, SIOCGIFMAC, &ifr) == 0) {
					success = 1;
					break;
				}
			}
		}
		else { /* handle error */ }
	}

	if (success) {
		memcpy(buf, ifr.ifr_ifru.ifru_data, len >= 6 ? 6 : len);
	} else {
		memset(buf, 0, len);
	}

Exit:
	close(sock);
}