1// Copyright 2017 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "resources.h"
6
7#include <zircon/device/sysinfo.h>
8#include <zircon/status.h>
9
10#include <errno.h>
11#include <fcntl.h>
12#include <stdio.h>
13#include <string.h>
14#include <unistd.h>
15
16zx_status_t get_root_resource(zx_handle_t* root_resource) {
17    int fd = open("/dev/misc/sysinfo", O_RDWR);
18    if (fd < 0) {
19        fprintf(stderr, "ERROR: Cannot open sysinfo: %s (%d)\n",
20                strerror(errno), errno);
21        return ZX_ERR_NOT_FOUND;
22    }
23
24    ssize_t n = ioctl_sysinfo_get_root_resource(fd, root_resource);
25    close(fd);
26    if (n != sizeof(*root_resource)) {
27        if (n < 0) {
28            fprintf(stderr, "ERROR: Cannot obtain root resource: %s (%zd)\n",
29                    zx_status_get_string(n), n);
30            return (zx_status_t)n;
31        } else {
32            fprintf(stderr, "ERROR: Cannot obtain root resource (%zd != %zd)\n",
33                    n, sizeof(root_resource));
34            return ZX_ERR_NOT_FOUND;
35        }
36    }
37    return ZX_OK;
38}
39