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 <fuchsia/sysinfo/c/fidl.h>
8 #include <lib/fdio/util.h>
9 #include <zircon/status.h>
10 #include <zircon/syscalls.h>
11 
12 #include <errno.h>
13 #include <fcntl.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17 
get_root_resource(zx_handle_t * root_resource)18 zx_status_t get_root_resource(zx_handle_t* root_resource) {
19     int fd = open("/dev/misc/sysinfo", O_RDWR);
20     if (fd < 0) {
21         fprintf(stderr, "ERROR: Cannot open sysinfo: %s (%d)\n",
22                 strerror(errno), errno);
23         return ZX_ERR_NOT_FOUND;
24     }
25 
26     zx_handle_t channel;
27     zx_status_t status = fdio_get_service_handle(fd, &channel);
28     if (status != ZX_OK) {
29         fprintf(stderr, "ERROR: Cannot obtain sysinfo channel: %s (%d)\n",
30                 zx_status_get_string(status), status);
31         return status;
32     }
33 
34     zx_status_t fidl_status = fuchsia_sysinfo_DeviceGetRootResource(channel, &status, root_resource);
35     zx_handle_close(channel);
36 
37     if (fidl_status != ZX_OK) {
38         fprintf(stderr, "ERROR: Cannot obtain root resource: %s (%d)\n",
39                 zx_status_get_string(fidl_status), fidl_status);
40         return fidl_status;
41     } else if (status != ZX_OK) {
42         fprintf(stderr, "ERROR: Cannot obtain root resource: %s (%d)\n",
43                 zx_status_get_string(status), status);
44         return status;
45     }
46 
47     return ZX_OK;
48 }
49