-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathutils.c
255 lines (220 loc) · 8.23 KB
/
utils.c
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Copyright (C) 2016 Veertu Inc,
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 or
* (at your option) version 3 of the License.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#import <CoreServices/CoreServices.h>
#include <IOKit/IOKitLib.h>
#include "utils.h"
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <time.h>
#include <sys/param.h>
#include <IOKit/IOBSD.h>
#include <IOKit/storage/IOCDMedia.h>
#include <IOKit/storage/IODVDMedia.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/IOCDTypes.h>
#include <IOKit/storage/IOMediaBSDClient.h>
#include <paths.h>
#include <sys/mount.h>
char conf_name[16][256];
static int dev_index = 0;
static int cur_index = 0;
void set_current_conf_name(char *new_conf_name)
{
printf("new conf name is %s\n", new_conf_name);
strcpy(conf_name[dev_index++], new_conf_name);
}
char *get_current_conf_name()
{
return conf_name[cur_index++];
}
ssize_t readLine(int fd, void *buffer, size_t n)
{
ssize_t numRead; /* # of bytes fetched by last read() */
size_t totRead; /* Total bytes read so far */
char *buf;
char ch;
if (n <= 0 || buffer == NULL) {
errno = EINVAL;
return -1;
}
buf = buffer; /* No pointer arithmetic on "void *" */
totRead = 0;
for (;;) {
numRead = read(fd, &ch, 1);
if (numRead == -1) {
if (errno == EINTR) /* Interrupted --> restart read() */
continue;
else
return -1; /* Some other error */
} else if (numRead == 0) { /* EOF */
if (totRead == 0) /* No bytes read; return 0 */
return 0;
else /* Some bytes read; add '\0' */
break;
} else { /* 'numRead' must be 1 if we get here */
if (totRead < n - 1) { /* Discard > (n - 1) bytes */
totRead++;
*buf++ = ch;
}
if (ch == '\n')
break;
}
}
*buf = '\0';
return totRead;
}
void generate_macaddr(uint8_t *macaddr)
{
macaddr[0] = 'v';
macaddr[1] = 'm';
macaddr[2] = 'x';
srandom((unsigned int)time(NULL));
macaddr[3] = random();
macaddr[4] = random();
macaddr[5] = random();
}
char *macaddr_to_string(uint8_t *macaddr, char *buf)
{
sprintf(buf, "%02x:%02x:%02x:%02x:%02x:%02x", macaddr[0], macaddr[1], macaddr[2],
macaddr[3], macaddr[4], macaddr[5]);
return buf;
}
/*
void get_platform_uuid(char *buf, int bufSize)
{
io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/");
CFStringRef uuidCf = (CFStringRef) IORegistryEntryCreateCFProperty(ioRegistryRoot, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
IOObjectRelease(ioRegistryRoot);
CFStringGetCString(uuidCf, buf, bufSize, kCFStringEncodingMacRoman);
CFRelease(uuidCf);
}
*/
static bool FindEjectableCDMedia(io_iterator_t *mediaIterator, const char *media_type)
{
mach_port_t masterPort;
kern_return_t kernResult;
CFMutableDictionaryRef classesToMatch;
kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort );
if ( kernResult != KERN_SUCCESS )
return false;
// CD media are instances of class kIOCDMediaClass.
classesToMatch = IOServiceMatching(media_type);
if (!classesToMatch)
return false;
else
{
// Each IOMedia object has a property with key kIOMediaEjectableKey
// which is true if the media is indeed ejectable. So add this
// property to the CFDictionary for matching.
CFDictionarySetValue(classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue );
}
kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator);
return (kernResult == KERN_SUCCESS);
}
static bool GetDeviceProperty(io_object_t nextMedia, CFStringRef prop, char *deviceFilePath, CFIndex maxPathSize)
{
kern_return_t kernResult = KERN_FAILURE;
*deviceFilePath = '\0';
if (nextMedia)
{
CFTypeRef deviceFilePathAsCFString;
deviceFilePathAsCFString = IORegistryEntryCreateCFProperty(
nextMedia, prop,
kCFAllocatorDefault, 0 );
*deviceFilePath = '\0';
if ( deviceFilePathAsCFString )
{
size_t devPathLength;
strcpy(deviceFilePath, _PATH_DEV );
// Add "r" before the BSD node name from the I/O Registry
// to specify the raw disk node. The raw disk node receives
// I/O requests directly and does not go through the
// buffer cache.
strcat( deviceFilePath, "r");
devPathLength = strlen( deviceFilePath );
if (CFStringGetCString(deviceFilePathAsCFString,
deviceFilePath + devPathLength,
maxPathSize - devPathLength,
kCFStringEncodingASCII)) {
kernResult = KERN_SUCCESS;
}
CFRelease( deviceFilePathAsCFString );
}
}
return (kernResult == KERN_SUCCESS);
}
static const char *GetFileSystemStatusFromMount(const char *mount)
{
struct statfs * mountList;
int mountListCount;
int mountListIndex;
char cmount[MAXPATHLEN] = {0};
static char cdrom[] = "CD-ROM";
// skip 'r' in /dev/rdiskXXX
memset(cmount, 0, sizeof(cmount));
const char *p = strchr(mount+1, '/');
if (!p)
return NULL;
strncpy(cmount, mount, p - mount + 1);
strcat(cmount, p + 2);
mountListCount = getmntinfo(&mountList, MNT_NOWAIT);
for (mountListIndex = 0; mountListIndex < mountListCount; mountListIndex++) {
if (strcmp(mountList[mountListIndex].f_mntfromname, cmount) == 0) {
return mountList[mountListIndex].f_mntonname;
}
}
return cdrom;
}
CFMutableArrayRef get_cdroms()
{
io_iterator_t mediaIterator;
io_object_t nextMedia;
char deviceFilePath[MAXPATHLEN];
char* media_types[] = {kIOCDMediaClass, kIODVDMediaClass};
CFMutableArrayRef anArray = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);
for (int i = 0; i < 2; i++) {
if (FindEjectableCDMedia(&mediaIterator, media_types[i])) {
while ((nextMedia = IOIteratorNext(mediaIterator))) {
if (GetDeviceProperty(nextMedia, CFSTR(kIOBSDNameKey), deviceFilePath, sizeof(deviceFilePath))) {
const char *mnt_path = GetFileSystemStatusFromMount(deviceFilePath);
CFMutableDictionaryRef d = CFDictionaryCreateMutable(NULL, 0, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionaryAddValue(d, CFSTR("dev"),
CFStringCreateWithCString(NULL, deviceFilePath, kCFStringEncodingASCII));
CFDictionaryAddValue(d, CFSTR("mnt"),
CFStringCreateWithCString(NULL, mnt_path, kCFStringEncodingASCII));
CFArrayAppendValue(anArray, d);
}
IOObjectRelease(nextMedia);
}
}
IOObjectRelease(mediaIterator);
}
return anArray;
}
bool osx_is_sierra()
{
SInt32 major, minor, bugfix;
Gestalt(gestaltSystemVersionMajor, &major);
Gestalt(gestaltSystemVersionMinor, &minor);
Gestalt(gestaltSystemVersionBugFix, &bugfix);
if (major >= 10 && minor >= 12)
return true;
return false;
}