authorgravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2021-05-11 22:03:57-04:00
committergravatar for michael.dusan@gmail.comMichael Dusan <michael.dusan@gmail.com> 2021-05-11 22:05:08-04:00
log30ace64fc4881e1799dfdbf2b034dc3b5a29f1d1
treeae86bee1bcee7baf02e4539bfece55c99a2791c4
parente3dd4dc91d65702c085dac9cc4d85aebfa308dfa

bsd: detect os version

- netbsd, dragonly: use sysctlbyname - openbsd: use sysctl - updated default semver ranges

3 files changed, 218 insertions(+), 28 deletions(-)

lib/std/os/bits/openbsd.zig+141-9
......@@ -298,15 +298,6 @@ pub const AI_NUMERICSERV = 16;
298298/// only if any address is assigned
299299pub const AI_ADDRCONFIG = 64;
300300
301pub const CTL_KERN = 1;
302pub const CTL_DEBUG = 5;
303pub const CTL_HW = 6;
304
305pub const KERN_PROC_ARGS = 55;
306pub const KERN_PROC_ARGV = 1;
307
308pub const HW_NCPUONLINE = 25;
309
310301pub const PATH_MAX = 1024;
311302
312303pub const STDIN_FILENO = 0;
......@@ -1237,3 +1228,144 @@ pub const POLLNORM = POLLRDNORM;
12371228pub const POLLWRNORM = POLLOUT;
12381229pub const POLLRDBAND = 0x0080;
12391230pub const POLLWRBAND = 0x0100;
1231
1232// sysctl mib
1233pub const CTL_UNSPEC = 0;
1234pub const CTL_KERN = 1;
1235pub const CTL_VM = 2;
1236pub const CTL_FS = 3;
1237pub const CTL_NET = 4;
1238pub const CTL_DEBUG = 5;
1239pub const CTL_HW = 6;
1240pub const CTL_MACHDEP = 7;
1241
1242pub const CTL_DDB = 9;
1243pub const CTL_VFS = 10;
1244
1245pub const KERN_OSTYPE = 1;
1246pub const KERN_OSRELEASE = 2;
1247pub const KERN_OSREV = 3;
1248pub const KERN_VERSION = 4;
1249pub const KERN_MAXVNODES = 5;
1250pub const KERN_MAXPROC = 6;
1251pub const KERN_MAXFILES = 7;
1252pub const KERN_ARGMAX = 8;
1253pub const KERN_SECURELVL = 9;
1254pub const KERN_HOSTNAME = 10;
1255pub const KERN_HOSTID = 11;
1256pub const KERN_CLOCKRATE = 12;
1257
1258pub const KERN_PROF = 16;
1259pub const KERN_POSIX1 = 17;
1260pub const KERN_NGROUPS = 18;
1261pub const KERN_JOB_CONTROL = 19;
1262pub const KERN_SAVED_IDS = 20;
1263pub const KERN_BOOTTIME = 21;
1264pub const KERN_DOMAINNAME = 22;
1265pub const KERN_MAXPARTITIONS = 23;
1266pub const KERN_RAWPARTITION = 24;
1267pub const KERN_MAXTHREAD = 25;
1268pub const KERN_NTHREADS = 26;
1269pub const KERN_OSVERSION = 27;
1270pub const KERN_SOMAXCONN = 28;
1271pub const KERN_SOMINCONN = 29;
1272
1273pub const KERN_NOSUIDCOREDUMP = 32;
1274pub const KERN_FSYNC = 33;
1275pub const KERN_SYSVMSG = 34;
1276pub const KERN_SYSVSEM = 35;
1277pub const KERN_SYSVSHM = 36;
1278
1279pub const KERN_MSGBUFSIZE = 38;
1280pub const KERN_MALLOCSTATS = 39;
1281pub const KERN_CPTIME = 40;
1282pub const KERN_NCHSTATS = 41;
1283pub const KERN_FORKSTAT = 42;
1284pub const KERN_NSELCOLL = 43;
1285pub const KERN_TTY = 44;
1286pub const KERN_CCPU = 45;
1287pub const KERN_FSCALE = 46;
1288pub const KERN_NPROCS = 47;
1289pub const KERN_MSGBUF = 48;
1290pub const KERN_POOL = 49;
1291pub const KERN_STACKGAPRANDOM = 50;
1292pub const KERN_SYSVIPC_INFO = 51;
1293pub const KERN_ALLOWKMEM = 52;
1294pub const KERN_WITNESSWATCH = 53;
1295pub const KERN_SPLASSERT = 54;
1296pub const KERN_PROC_ARGS = 55;
1297pub const KERN_NFILES = 56;
1298pub const KERN_TTYCOUNT = 57;
1299pub const KERN_NUMVNODES = 58;
1300pub const KERN_MBSTAT = 59;
1301pub const KERN_WITNESS = 60;
1302pub const KERN_SEMINFO = 61;
1303pub const KERN_SHMINFO = 62;
1304pub const KERN_INTRCNT = 63;
1305pub const KERN_WATCHDOG = 64;
1306pub const KERN_ALLOWDT = 65;
1307pub const KERN_PROC = 66;
1308pub const KERN_MAXCLUSTERS = 67;
1309pub const KERN_EVCOUNT = 68;
1310pub const KERN_TIMECOUNTER = 69;
1311pub const KERN_MAXLOCKSPERUID = 70;
1312pub const KERN_CPTIME2 = 71;
1313pub const KERN_CACHEPCT = 72;
1314pub const KERN_FILE = 73;
1315pub const KERN_WXABORT = 74;
1316pub const KERN_CONSDEV = 75;
1317pub const KERN_NETLIVELOCKS = 76;
1318pub const KERN_POOL_DEBUG = 77;
1319pub const KERN_PROC_CWD = 78;
1320pub const KERN_PROC_NOBROADCASTKILL = 79;
1321pub const KERN_PROC_VMMAP = 80;
1322pub const KERN_GLOBAL_PTRACE = 81;
1323pub const KERN_CONSBUFSIZE = 82;
1324pub const KERN_CONSBUF = 83;
1325pub const KERN_AUDIO = 84;
1326pub const KERN_CPUSTATS = 85;
1327pub const KERN_PFSTATUS = 86;
1328pub const KERN_TIMEOUT_STATS = 87;
1329pub const KERN_UTC_OFFSET = 88;
1330pub const KERN_VIDEO = 89;
1331
1332pub const HW_MACHINE = 1;
1333pub const HW_MODEL = 2;
1334pub const HW_NCPU = 3;
1335pub const HW_BYTEORDER = 4;
1336pub const HW_PHYSMEM = 5;
1337pub const HW_USERMEM = 6;
1338pub const HW_PAGESIZE = 7;
1339pub const HW_DISKNAMES = 8;
1340pub const HW_DISKSTATS = 9;
1341pub const HW_DISKCOUNT = 10;
1342pub const HW_SENSORS = 11;
1343pub const HW_CPUSPEED = 12;
1344pub const HW_SETPERF = 13;
1345pub const HW_VENDOR = 14;
1346pub const HW_PRODUCT = 15;
1347pub const HW_VERSION = 16;
1348pub const HW_SERIALNO = 17;
1349pub const HW_UUID = 18;
1350pub const HW_PHYSMEM64 = 19;
1351pub const HW_USERMEM64 = 20;
1352pub const HW_NCPUFOUND = 21;
1353pub const HW_ALLOWPOWERDOWN = 22;
1354pub const HW_PERFPOLICY = 23;
1355pub const HW_SMT = 24;
1356pub const HW_NCPUONLINE = 25;
1357
1358pub const KERN_PROC_ALL = 0;
1359pub const KERN_PROC_PID = 1;
1360pub const KERN_PROC_PGRP = 2;
1361pub const KERN_PROC_SESSION = 3;
1362pub const KERN_PROC_TTY = 4;
1363pub const KERN_PROC_UID = 5;
1364pub const KERN_PROC_RUID = 6;
1365pub const KERN_PROC_KTHREAD = 7;
1366pub const KERN_PROC_SHOW_THREADS = 0x40000000;
1367
1368pub const KERN_PROC_ARGV = 1;
1369pub const KERN_PROC_NARGV = 2;
1370pub const KERN_PROC_ENV = 3;
1371pub const KERN_PROC_NENV = 4;
lib/std/target.zig+4-4
......@@ -267,7 +267,7 @@ pub const Target = struct {
267267 .macos => return .{
268268 .semver = .{
269269 .min = .{ .major = 10, .minor = 13 },
270 .max = .{ .major = 11, .minor = 1 },
270 .max = .{ .major = 11, .minor = 2 },
271271 },
272272 },
273273 .ios => return .{
......@@ -291,19 +291,19 @@ pub const Target = struct {
291291 .netbsd => return .{
292292 .semver = .{
293293 .min = .{ .major = 8, .minor = 0 },
294 .max = .{ .major = 9, .minor = 0 },
294 .max = .{ .major = 9, .minor = 1 },
295295 },
296296 },
297297 .openbsd => return .{
298298 .semver = .{
299299 .min = .{ .major = 6, .minor = 8 },
300 .max = .{ .major = 6, .minor = 8 },
300 .max = .{ .major = 6, .minor = 9 },
301301 },
302302 },
303303 .dragonfly => return .{
304304 .semver = .{
305305 .min = .{ .major = 5, .minor = 8 },
306 .max = .{ .major = 5, .minor = 8 },
306 .max = .{ .major = 6, .minor = 0 },
307307 },
308308 },
309309
lib/std/zig/system.zig+73-15
......@@ -257,28 +257,86 @@ pub const NativeTargetInfo = struct {
257257 os.version_range.windows.max = detected_version;
258258 },
259259 .macos => try macos.detect(&os),
260 .freebsd => {
261 var osreldate: u32 = undefined;
262 var len: usize = undefined;
260 .freebsd, .netbsd, .dragonfly => {
261 const key = switch (Target.current.os.tag) {
262 .freebsd => "kern.osreldate",
263 .netbsd, .dragonfly => "kern.osrevision",
264 else => unreachable,
265 };
266 var value: u32 = undefined;
267 var len: usize = @sizeOf(@TypeOf(value));
268
269 std.os.sysctlbynameZ(key, &value, &len, null, 0) catch |err| switch (err) {
270 error.NameTooLong => unreachable, // constant, known good value
271 error.PermissionDenied => unreachable, // only when setting values,
272 error.SystemResources => unreachable, // memory already on the stack
273 error.UnknownName => unreachable, // constant, known good value
274 error.Unexpected => return error.OSVersionDetectionFail,
275 };
276
277 switch (Target.current.os.tag) {
278 .freebsd => {
279 // https://www.freebsd.org/doc/en_US.ISO8859-1/books/porters-handbook/versions.html
280 // Major * 100,000 has been convention since FreeBSD 2.2 (1997)
281 // Minor * 1(0),000 summed has been convention since FreeBSD 2.2 (1997)
282 // e.g. 492101 = 4.11-STABLE = 4.(9+2)
283 const major = value / 100_000;
284 const minor1 = value % 100_000 / 10_000; // usually 0 since 5.1
285 const minor2 = value % 10_000 / 1_000; // 0 before 5.1, minor version since
286 const patch = value % 1_000;
287 os.version_range.semver.min = .{ .major = major, .minor = minor1 + minor2, .patch = patch };
288 os.version_range.semver.max = os.version_range.semver.min;
289 },
290 .netbsd => {
291 // #define __NetBSD_Version__ MMmmrrpp00
292 //
293 // M = major version
294 // m = minor version; a minor number of 99 indicates current.
295 // r = 0 (*)
296 // p = patchlevel
297 const major = value / 100_000_000;
298 const minor = value % 100_000_000 / 1_000_000;
299 const patch = value % 10_000 / 100;
300 os.version_range.semver.min = .{ .major = major, .minor = minor, .patch = patch };
301 os.version_range.semver.max = os.version_range.semver.min;
302 },
303 .dragonfly => {
304 // https://github.com/DragonFlyBSD/DragonFlyBSD/blob/cb2cde83771754aeef9bb3251ee48959138dec87/Makefile.inc1#L15-L17
305 // flat base10 format: Mmmmpp
306 // M = major
307 // m = minor; odd-numbers indicate current dev branch
308 // p = patch
309 const major = value / 100_000;
310 const minor = value % 100_000 / 100;
311 const patch = value % 100;
312 os.version_range.semver.min = .{ .major = major, .minor = minor, .patch = patch };
313 os.version_range.semver.max = os.version_range.semver.min;
314 },
315 else => unreachable,
316 }
317 },
318 .openbsd => {
319 const mib: [2]c_int = [_]c_int{
320 std.os.CTL_KERN,
321 std.os.KERN_OSRELEASE,
322 };
323 var buf: [64]u8 = undefined;
324 var len: usize = buf.len;
263325
264 std.os.sysctlbynameZ("kern.osreldate", &osreldate, &len, null, 0) catch |err| switch (err) {
326 std.os.sysctl(&mib, &buf, &len, null, 0) catch |err| switch (err) {
265327 error.NameTooLong => unreachable, // constant, known good value
266328 error.PermissionDenied => unreachable, // only when setting values,
267329 error.SystemResources => unreachable, // memory already on the stack
268330 error.UnknownName => unreachable, // constant, known good value
269 error.Unexpected => unreachable, // EFAULT: stack should be safe, EISDIR/ENOTDIR: constant, known good value
331 error.Unexpected => return error.OSVersionDetectionFail,
270332 };
271333
272 // https://www.freebsd.org/doc/en_US.ISO8859-1/books/porters-handbook/versions.html
273 // Major * 100,000 has been convention since FreeBSD 2.2 (1997)
274 // Minor * 1(0),000 summed has been convention since FreeBSD 2.2 (1997)
275 // e.g. 492101 = 4.11-STABLE = 4.(9+2)
276 const major = osreldate / 100_000;
277 const minor1 = osreldate % 100_000 / 10_000; // usually 0 since 5.1
278 const minor2 = osreldate % 10_000 / 1_000; // 0 before 5.1, minor version since
279 const patch = osreldate % 1_000;
280 os.version_range.semver.min = .{ .major = major, .minor = minor1 + minor2, .patch = patch };
281 os.version_range.semver.max = .{ .major = major, .minor = minor1 + minor2, .patch = patch };
334 if (std.builtin.Version.parse(buf[0 .. len - 1])) |ver| {
335 os.version_range.semver.min = ver;
336 os.version_range.semver.max = ver;
337 } else |err| {
338 return error.OSVersionDetectionFail;
339 }
282340 },
283341 else => {
284342 // Unimplemented, fall back to default version range.