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 #pragma once
6 
7 #include <zircon/compiler.h>
8 #include <zircon/types.h>
9 
10 __BEGIN_CDECLS
11 
12 typedef struct async_dispatcher async_dispatcher_t; // From <lib/async/dispatcher.h>
13 
14 // These functions provide an implementation of the shared library loading
15 // service.  See system/fidl/fuchsia-ldsvc/ldsvc.fidl for a definition of the protocol.
16 //
17 // These implementations are compatible with |dl_set_loader_service| and are
18 // primarily used by devmgr, fshost, and appmgr to provide shared libraries to
19 // the processes they create.
20 //
21 // Terms:
22 //
23 // "loader service": A channel that speaks the protocol expected by
24 //     dl_set_loader_service(). The service behind the channel receives
25 //     load requests (e.g., "libhid.so") and returns VMOs that contain
26 //     the data associated with that name.
27 // "system loader service": A loader service, provided by the system,
28 //     that is shared by multiple processes.
29 
30 typedef struct loader_service loader_service_t;
31 
32 // Create a new file-system backed loader service capable of handling
33 // any number of clients.
34 //
35 // Requests will be processed on the given |async|. If |async| is NULL, this
36 // library will create a new thread and listen for requests on that thread.
37 zx_status_t loader_service_create_fs(async_dispatcher_t* dispatcher, loader_service_t** out);
38 
39 // Create a new file-descriptor backed loader service capable of handling any
40 // number of clients.
41 //
42 // Requests will be processed on the given |async|. If |async| is NULL, this
43 // library will create a new thread and listen for requests on that thread.
44 // Paths and objects will be loaded relative to |root_dir_fd| and data will be
45 // published relative to |data_sink_dir_fd|; the two file descriptors
46 // are consumed on success.
47 zx_status_t loader_service_create_fd(async_dispatcher_t* dispatcher,
48                                      int root_dir_fd,
49                                      int data_sink_dir_fd,
50                                      loader_service_t** out);
51 
52 // Returns a new dl_set_loader_service-compatible loader service channel.
53 zx_status_t loader_service_connect(loader_service_t* svc, zx_handle_t* out);
54 
55 // Same as connect except caller provides the channel endpoint (which
56 // is connected on success, closed on failure).
57 zx_status_t loader_service_attach(loader_service_t* svc, zx_handle_t channel);
58 
59 typedef struct loader_service_ops {
60     // attempt to load a shared library from suitable library paths.
61     zx_status_t (*load_object)(void* ctx, const char* name, zx_handle_t* vmo);
62 
63     // attempt to load a script interpreter or debug config file
64     zx_status_t (*load_abspath)(void* ctx, const char* path, zx_handle_t* vmo);
65 
66     // attempt to publish a data sink
67     // takes ownership of the provided vmo on both success and failure.
68     zx_status_t (*publish_data_sink)(void* ctx, const char* name, zx_handle_t vmo);
69 
70     // finalize the loader service (optional)
71     // called shortly before the loader service is destroyed
72     void (*finalizer)(void* ctx);
73 } loader_service_ops_t;
74 
75 // Create a loader service backed by custom loader ops.
76 //
77 // Requests will be processed on the given |async|. If |async| is NULL, this
78 // library will create a new thread and listen for requests on that thread.
79 zx_status_t loader_service_create(async_dispatcher_t* dispatcher,
80                                   const loader_service_ops_t* ops,
81                                   void* ctx,
82                                   loader_service_t** out);
83 
84 // After this function returns, |svc| will destroy itself once there are no
85 // longer any outstanding connections.
86 //
87 // The |finalizer| in |loader_service_ops_t| will be called shortly before |svc|
88 // destroys itself.
89 zx_status_t loader_service_release(loader_service_t* svc);
90 __END_CDECLS
91