This repository has been archived by the owner on May 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
bz2-stub-libnv.c
186 lines (150 loc) · 5.52 KB
/
bz2-stub-libnv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/* Copyright 2016 Google Inc. All Rights Reserved.
*
* Use of this source code is governed by the bzip2
* license that can be found in the LICENSE file. */
#include <sys/types.h>
#include <sys/socket.h>
#include <assert.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <nv.h>
#include "rpc-util.h"
int _rpc_verbose = 4; /* smaller number => more verbose */
int _rpc_indent = 0;
static const char *g_exe_file = "./bz2-driver-libnv";
static int g_exe_fd = -1; /* File descriptor to driver executable */
/* Before main(), get an FD for the driver program, so that it is still
accessible even if the application enters a sandbox. */
void __attribute__((constructor)) _stub_construct(void) {
g_exe_fd = OpenDriver(g_exe_file);
}
struct DriverConnection {
/* Child process ID for the driver process */
pid_t pid;
/* Socket pair for communcation with driver process */
int socket_fds[2];
} g_conn;
static struct DriverConnection *CreateConnection(void) {
struct DriverConnection *conn = &g_conn;
/* Create socket for communication with child */
conn->socket_fds[0] = -1;
conn->socket_fds[1] = -1;
int rc = socketpair(AF_UNIX, SOCK_STREAM, 0, conn->socket_fds);
if (rc < 0) {
error_("failed to open sockets, errno=%d (%s)", errno, strerror(errno));
return NULL;
}
api_("CreateConnection(g_exe_fd=%d, '%s')", g_exe_fd, g_exe_file);
conn->pid = fork();
if (conn->pid < 0) {
error_("failed to fork, errno=%d (%s)", errno, strerror(errno));
return NULL;
}
if (conn->pid == 0) {
/* Child process: run the driver */
RunDriver(g_exe_fd, g_exe_file, conn->socket_fds[1]);
}
return conn;
}
static void DestroyConnection(struct DriverConnection *conn) {
int ii;
api_("DestroyConnection(conn=%p {pid=%d })", conn, conn->pid);
for (ii = 0; ii < 2; ii ++) {
if (conn->socket_fds[ii] >= 0) {
verbose_("close socket_fds[%d]= %d", ii, conn->socket_fds[ii]);
close(conn->socket_fds[ii]);
conn->socket_fds[ii] = -1;
}
}
if (conn->pid > 0) {
TerminateChild(conn->pid);
conn->pid = 0;
}
}
/*****************************************************************************/
/* Everything above here is generic, and would be useful for any remoted API */
/*****************************************************************************/
/* RPC-Forwarding versions of libbz2 entrypoints */
int BZ2_bzCompressStream(int ifd, int ofd, int blockSize100k, int verbosity, int workFactor) {
static const char *cmd = "BZ2_bzCompressStream";
struct DriverConnection *conn = CreateConnection();
nvlist_t *nvl;
nvl = nvlist_create(0);
nvlist_add_string(nvl, "cmd", cmd);
nvlist_add_descriptor(nvl, "ifd", ifd);
nvlist_add_descriptor(nvl, "ofd", ofd);
nvlist_add_number(nvl, "blockSize100k", (uint64_t)blockSize100k);
nvlist_add_number(nvl, "verbosity", (uint64_t)verbosity);
nvlist_add_number(nvl, "workFactor", (uint64_t)workFactor);
api_("%s(%d, %d, %d, %d, %d) =>", cmd, ifd, ofd, blockSize100k, verbosity, workFactor);
nvl = nvlist_xfer(conn->socket_fds[0], nvl, 0);
assert (nvl != NULL);
int retval = nvlist_get_number(nvl, "retval");
api_("%s(%d, %d, %d, %d, %d) return %d <=", cmd, ifd, ofd, blockSize100k, verbosity, workFactor, retval);
nvlist_destroy(nvl);
DestroyConnection(conn);
return retval;
}
int BZ2_bzDecompressStream(int ifd, int ofd, int verbosity, int small) {
static const char *cmd = "BZ2_bzDecompressStream";
struct DriverConnection *conn = CreateConnection();
nvlist_t *nvl;
nvl = nvlist_create(0);
nvlist_add_string(nvl, "cmd", cmd);
nvlist_add_descriptor(nvl, "ifd", ifd);
nvlist_add_descriptor(nvl, "ofd", ofd);
nvlist_add_number(nvl, "verbosity", (uint64_t)verbosity);
nvlist_add_number(nvl, "small", (uint64_t)small);
api_("%s(%d, %d, %d, %d) =>", cmd, ifd, ofd, verbosity, small);
nvl = nvlist_xfer(conn->socket_fds[0], nvl, 0);
assert (nvl != NULL);
int retval = nvlist_get_number(nvl, "retval");
api_("%s(%d, %d, %d, %d) return %d <=", cmd, ifd, ofd, verbosity, small, retval);
nvlist_destroy(nvl);
DestroyConnection(conn);
return retval;
}
int BZ2_bzTestStream(int ifd, int verbosity, int small) {
static const char *cmd = "BZ2_bzTestStream";
struct DriverConnection *conn = CreateConnection();
nvlist_t *nvl;
nvl = nvlist_create(0);
nvlist_add_string(nvl, "cmd", cmd);
nvlist_add_descriptor(nvl, "ifd", ifd);
nvlist_add_number(nvl, "verbosity", (uint64_t)verbosity);
nvlist_add_number(nvl, "small", (uint64_t)small);
api_("%s(%d, %d, %d) =>", cmd, ifd, verbosity, small);
nvl = nvlist_xfer(conn->socket_fds[0], nvl, 0);
assert (nvl != NULL);
int retval = nvlist_get_number(nvl, "retval");
api_("%s(%d, %d, %d) return %d <=", cmd, ifd, verbosity, small, retval);
nvlist_destroy(nvl);
DestroyConnection(conn);
return retval;
}
const char *BZ2_bzlibVersion(void) {
static const char *cmd = "BZ2_bzlibVersion";
static const char *saved_version = NULL;
if (saved_version) {
api_("%s() return '%s' <= (saved)", cmd, saved_version);
return saved_version;
}
struct DriverConnection *conn = CreateConnection();
nvlist_t *nvl;
nvl = nvlist_create(0);
nvlist_add_string(nvl, "cmd", cmd);
api_("%s() =>", cmd);
nvl = nvlist_xfer(conn->socket_fds[0], nvl, 0);
assert (nvl != NULL);
const char *retval = nvlist_get_string(nvl, "retval");
api_("%s() return '%s' <=", cmd, retval);
saved_version = strdup(retval);
nvlist_destroy(nvl);
DestroyConnection(conn);
return saved_version;
}