+
+/*
+ * gethostuuid
+ *
+ * Description: Get the host UUID from IOKit and return it to user space.
+ *
+ * Parameters: uuid_buf Pointer to buffer to receive UUID
+ * timeout Timespec for timout
+ *
+ * Returns: 0 Success
+ * EWOULDBLOCK Timeout is too short
+ * copyout:EFAULT Bad user buffer
+ *
+ * Notes: A timeout seems redundant, since if it's tolerable to not
+ * have a system UUID in hand, then why ask for one?
+ */
+int
+gethostuuid(struct proc *p, struct gethostuuid_args *uap, __unused int32_t *retval)
+{
+ kern_return_t kret;
+ int error;
+ mach_timespec_t mach_ts; /* for IOKit call */
+ __darwin_uuid_t uuid_kern; /* for IOKit call */
+
+ /* Convert the 32/64 bit timespec into a mach_timespec_t */
+ if ( proc_is64bit(p) ) {
+ struct user64_timespec ts;
+ error = copyin(uap->timeoutp, &ts, sizeof(ts));
+ if (error)
+ return (error);
+ mach_ts.tv_sec = ts.tv_sec;
+ mach_ts.tv_nsec = ts.tv_nsec;
+ } else {
+ struct user32_timespec ts;
+ error = copyin(uap->timeoutp, &ts, sizeof(ts) );
+ if (error)
+ return (error);
+ mach_ts.tv_sec = ts.tv_sec;
+ mach_ts.tv_nsec = ts.tv_nsec;
+ }
+
+ /* Call IOKit with the stack buffer to get the UUID */
+ kret = IOBSDGetPlatformUUID(uuid_kern, mach_ts);
+
+ /*
+ * If we get it, copy out the data to the user buffer; note that a
+ * uuid_t is an array of characters, so this is size invariant for
+ * 32 vs. 64 bit.
+ */
+ if (kret == KERN_SUCCESS) {
+ error = copyout(uuid_kern, uap->uuid_buf, sizeof(uuid_kern));
+ } else {
+ error = EWOULDBLOCK;
+ }
+
+ return (error);
+}