Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / libraries / nacl_io / kernel_intercept.cc
1 // Copyright (c) 2012 The Chromium 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 "nacl_io/kernel_intercept.h"
6
7 #include <assert.h>
8 #include <errno.h>
9 #include <string.h>
10
11 #include "nacl_io/kernel_proxy.h"
12 #include "nacl_io/kernel_wrap.h"
13 #include "nacl_io/kernel_wrap_real.h"
14 #include "nacl_io/log.h"
15 #include "nacl_io/osmman.h"
16 #include "nacl_io/ossocket.h"
17 #include "nacl_io/ostime.h"
18 #include "nacl_io/pepper_interface.h"
19 #include "nacl_io/real_pepper_interface.h"
20
21 using namespace nacl_io;
22
23 #define ON_NOSYS_RETURN(x)    \
24   if (!ki_is_initialized()) { \
25     errno = ENOSYS;           \
26     return x;                 \
27   }
28
29 struct KernelInterceptState {
30   KernelProxy* kp;
31   PepperInterface* ppapi;
32   bool kp_owned;
33 };
34
35 static KernelInterceptState s_state;
36
37 // The the test code we want to be able to save the previous kernel
38 // proxy when intialising and restore it on uninit.
39 static KernelInterceptState s_saved_state;
40
41 int ki_push_state_for_testing() {
42   assert(s_saved_state.kp == NULL);
43   if (s_saved_state.kp != NULL)
44     return 1;
45   s_saved_state = s_state;
46   s_state.kp = NULL;
47   s_state.ppapi = NULL;
48   s_state.kp_owned = false;
49   return 0;
50 }
51
52 static void ki_pop_state() {
53   // Swap out the KernelProxy. This will normally reset the
54   // proxy to NULL, aside from in test code that has called
55   // ki_push_state_for_testing().
56   s_state = s_saved_state;
57   s_saved_state.kp = NULL;
58   s_saved_state.ppapi = NULL;
59   s_saved_state.kp_owned = false;
60 }
61
62 int ki_pop_state_for_testing() {
63   ki_pop_state();
64   return 0;
65 }
66
67 int ki_init(void* kp) {
68   LOG_TRACE("ki_init: %p", kp);
69   return ki_init_ppapi(kp, 0, NULL);
70 }
71
72 int ki_init_ppapi(void* kp,
73                   PP_Instance instance,
74                   PPB_GetInterface get_browser_interface) {
75   assert(!s_state.kp);
76   if (s_state.kp != NULL)
77     return 1;
78   PepperInterface* ppapi = NULL;
79   if (instance && get_browser_interface) {
80     ppapi = new RealPepperInterface(instance, get_browser_interface);
81     s_state.ppapi = ppapi;
82   }
83   int rtn = ki_init_interface(kp, ppapi);
84   return rtn;
85 }
86
87 int ki_init_interface(void* kp, void* pepper_interface) {
88   LOG_TRACE("ki_init_interface: %p %p", kp, pepper_interface);
89   assert(!s_state.kp);
90   if (s_state.kp != NULL)
91     return 1;
92   PepperInterface* ppapi = static_cast<PepperInterface*>(pepper_interface);
93   kernel_wrap_init();
94
95   if (kp == NULL) {
96     s_state.kp = new KernelProxy();
97     s_state.kp_owned = true;
98   } else {
99     s_state.kp = static_cast<KernelProxy*>(kp);
100     s_state.kp_owned = false;
101   }
102
103   if (s_state.kp->Init(ppapi) != 0)
104     return 1;
105
106   return 0;
107 }
108
109 int ki_is_initialized() {
110   return s_state.kp != NULL;
111 }
112
113 int ki_uninit() {
114   LOG_TRACE("ki_uninit");
115   assert(s_state.kp);
116   if (s_state.kp == NULL)
117     return 1;
118
119   if (s_saved_state.kp == NULL)
120     kernel_wrap_uninit();
121
122   // If we are going to delete the KernelProxy don't do it
123   // until we've swapped it out.
124   KernelInterceptState state_to_delete = s_state;
125
126   ki_pop_state();
127
128   if (state_to_delete.kp_owned)
129     delete state_to_delete.kp;
130
131   delete state_to_delete.ppapi;
132   return 0;
133 }
134
135 nacl_io::KernelProxy* ki_get_proxy() {
136   return s_state.kp;
137 }
138
139 int ki_chdir(const char* path) {
140   ON_NOSYS_RETURN(-1);
141   return s_state.kp->chdir(path);
142 }
143
144 void ki_exit(int status) {
145   if (ki_is_initialized())
146     s_state.kp->exit(status);
147
148   _real_exit(status);
149 }
150
151 char* ki_getcwd(char* buf, size_t size) {
152   // gtest uses getcwd in a static initializer and expects it to always
153   // succeed.  If we haven't initialized kernel-intercept yet, then try
154   // the IRT's getcwd, and fall back to just returning ".".
155   if (!ki_is_initialized()) {
156     int rtn = _real_getcwd(buf, size);
157     if (rtn != 0) {
158       if (rtn == ENOSYS) {
159         buf[0] = '.';
160         buf[1] = 0;
161       } else {
162         errno = rtn;
163         return NULL;
164       }
165     }
166     return buf;
167   }
168   return s_state.kp->getcwd(buf, size);
169 }
170
171 char* ki_getwd(char* buf) {
172   ON_NOSYS_RETURN(NULL);
173   return s_state.kp->getwd(buf);
174 }
175
176 int ki_dup(int oldfd) {
177   ON_NOSYS_RETURN(-1);
178   return s_state.kp->dup(oldfd);
179 }
180
181 int ki_dup2(int oldfd, int newfd) {
182   ON_NOSYS_RETURN(-1);
183   return s_state.kp->dup2(oldfd, newfd);
184 }
185
186 int ki_chmod(const char* path, mode_t mode) {
187   ON_NOSYS_RETURN(-1);
188   return s_state.kp->chmod(path, mode);
189 }
190
191 int ki_fchdir(int fd) {
192   ON_NOSYS_RETURN(-1);
193   return s_state.kp->fchdir(fd);
194 }
195
196 int ki_fchmod(int fd, mode_t mode) {
197   ON_NOSYS_RETURN(-1);
198   return s_state.kp->fchmod(fd, mode);
199 }
200
201 int ki_stat(const char* path, struct stat* buf) {
202   ON_NOSYS_RETURN(-1);
203   return s_state.kp->stat(path, buf);
204 }
205
206 int ki_mkdir(const char* path, mode_t mode) {
207   ON_NOSYS_RETURN(-1);
208   return s_state.kp->mkdir(path, mode);
209 }
210
211 int ki_rmdir(const char* path) {
212   ON_NOSYS_RETURN(-1);
213   return s_state.kp->rmdir(path);
214 }
215
216 int ki_mount(const char* source,
217              const char* target,
218              const char* filesystemtype,
219              unsigned long mountflags,
220              const void* data) {
221   ON_NOSYS_RETURN(-1);
222   return s_state.kp->mount(source, target, filesystemtype, mountflags, data);
223 }
224
225 int ki_umount(const char* path) {
226   ON_NOSYS_RETURN(-1);
227   return s_state.kp->umount(path);
228 }
229
230 int ki_open(const char* path, int oflag, mode_t mode) {
231   ON_NOSYS_RETURN(-1);
232   return s_state.kp->open(path, oflag, mode);
233 }
234
235 int ki_pipe(int pipefds[2]) {
236   ON_NOSYS_RETURN(-1);
237   return s_state.kp->pipe(pipefds);
238 }
239
240 ssize_t ki_read(int fd, void* buf, size_t nbyte) {
241   ON_NOSYS_RETURN(-1);
242   return s_state.kp->read(fd, buf, nbyte);
243 }
244
245 ssize_t ki_write(int fd, const void* buf, size_t nbyte) {
246   ON_NOSYS_RETURN(-1);
247   return s_state.kp->write(fd, buf, nbyte);
248 }
249
250 int ki_fstat(int fd, struct stat* buf) {
251   ON_NOSYS_RETURN(-1);
252   return s_state.kp->fstat(fd, buf);
253 }
254
255 int ki_getdents(int fd, void* buf, unsigned int count) {
256   ON_NOSYS_RETURN(-1);
257   return s_state.kp->getdents(fd, buf, count);
258 }
259
260 int ki_ftruncate(int fd, off_t length) {
261   ON_NOSYS_RETURN(-1);
262   return s_state.kp->ftruncate(fd, length);
263 }
264
265 int ki_fsync(int fd) {
266   ON_NOSYS_RETURN(-1);
267   return s_state.kp->fsync(fd);
268 }
269
270 int ki_fdatasync(int fd) {
271   ON_NOSYS_RETURN(-1);
272   return s_state.kp->fdatasync(fd);
273 }
274
275 int ki_isatty(int fd) {
276   ON_NOSYS_RETURN(0);
277   return s_state.kp->isatty(fd);
278 }
279
280 int ki_close(int fd) {
281   ON_NOSYS_RETURN(-1);
282   return s_state.kp->close(fd);
283 }
284
285 off_t ki_lseek(int fd, off_t offset, int whence) {
286   ON_NOSYS_RETURN(-1);
287   return s_state.kp->lseek(fd, offset, whence);
288 }
289
290 int ki_remove(const char* path) {
291   ON_NOSYS_RETURN(-1);
292   return s_state.kp->remove(path);
293 }
294
295 int ki_unlink(const char* path) {
296   ON_NOSYS_RETURN(-1);
297   return s_state.kp->unlink(path);
298 }
299
300 int ki_truncate(const char* path, off_t length) {
301   ON_NOSYS_RETURN(-1);
302   return s_state.kp->truncate(path, length);
303 }
304
305 int ki_lstat(const char* path, struct stat* buf) {
306   ON_NOSYS_RETURN(-1);
307   return s_state.kp->lstat(path, buf);
308 }
309
310 int ki_link(const char* oldpath, const char* newpath) {
311   ON_NOSYS_RETURN(-1);
312   return s_state.kp->link(oldpath, newpath);
313 }
314
315 int ki_rename(const char* path, const char* newpath) {
316   ON_NOSYS_RETURN(-1);
317   return s_state.kp->rename(path, newpath);
318 }
319
320 int ki_symlink(const char* oldpath, const char* newpath) {
321   ON_NOSYS_RETURN(-1);
322   return s_state.kp->symlink(oldpath, newpath);
323 }
324
325 int ki_access(const char* path, int amode) {
326   ON_NOSYS_RETURN(-1);
327   return s_state.kp->access(path, amode);
328 }
329
330 int ki_readlink(const char* path, char* buf, size_t count) {
331   ON_NOSYS_RETURN(-1);
332   return s_state.kp->readlink(path, buf, count);
333 }
334
335 int ki_utimes(const char* path, const struct timeval times[2]) {
336   ON_NOSYS_RETURN(-1);
337   // Implement in terms of utimens.
338   if (!times) {
339     return s_state.kp->utimens(path, NULL);
340   }
341
342   struct timespec ts[2];
343   ts[0].tv_sec = times[0].tv_sec;
344   ts[0].tv_nsec = times[0].tv_usec * 1000;
345   ts[1].tv_sec = times[1].tv_sec;
346   ts[1].tv_nsec = times[1].tv_usec * 1000;
347   return s_state.kp->utimens(path, ts);
348 }
349
350 int ki_futimes(int fd, const struct timeval times[2]) {
351   ON_NOSYS_RETURN(-1);
352   // Implement in terms of futimens.
353   if (!times) {
354     return s_state.kp->futimens(fd, NULL);
355   }
356
357   struct timespec ts[2];
358   ts[0].tv_sec = times[0].tv_sec;
359   ts[0].tv_nsec = times[0].tv_usec * 1000;
360   ts[1].tv_sec = times[1].tv_sec;
361   ts[1].tv_nsec = times[1].tv_usec * 1000;
362   return s_state.kp->futimens(fd, ts);
363 }
364
365 void* ki_mmap(void* addr,
366               size_t length,
367               int prot,
368               int flags,
369               int fd,
370               off_t offset) {
371   ON_NOSYS_RETURN(MAP_FAILED);
372   return s_state.kp->mmap(addr, length, prot, flags, fd, offset);
373 }
374
375 int ki_munmap(void* addr, size_t length) {
376   ON_NOSYS_RETURN(-1);
377   return s_state.kp->munmap(addr, length);
378 }
379
380 int ki_open_resource(const char* file) {
381   ON_NOSYS_RETURN(-1);
382   return s_state.kp->open_resource(file);
383 }
384
385 int ki_fcntl(int d, int request, va_list args) {
386   ON_NOSYS_RETURN(-1);
387   return s_state.kp->fcntl(d, request, args);
388 }
389
390 int ki_ioctl(int d, int request, va_list args) {
391   ON_NOSYS_RETURN(-1);
392   return s_state.kp->ioctl(d, request, args);
393 }
394
395 int ki_chown(const char* path, uid_t owner, gid_t group) {
396   ON_NOSYS_RETURN(-1);
397   return s_state.kp->chown(path, owner, group);
398 }
399
400 int ki_fchown(int fd, uid_t owner, gid_t group) {
401   ON_NOSYS_RETURN(-1);
402   return s_state.kp->fchown(fd, owner, group);
403 }
404
405 int ki_lchown(const char* path, uid_t owner, gid_t group) {
406   ON_NOSYS_RETURN(-1);
407   return s_state.kp->lchown(path, owner, group);
408 }
409
410 int ki_utime(const char* filename, const struct utimbuf* times) {
411   ON_NOSYS_RETURN(-1);
412   // Implement in terms of utimens.
413   if (!times) {
414     return s_state.kp->utimens(filename, NULL);
415   }
416
417   struct timespec ts[2];
418   ts[0].tv_sec = times->actime;
419   ts[0].tv_nsec = 0;
420   ts[1].tv_sec = times->modtime;
421   ts[1].tv_nsec = 0;
422   return s_state.kp->utimens(filename, ts);
423 }
424
425 int ki_futimens(int fd, const struct timespec times[2]) {
426   ON_NOSYS_RETURN(-1);
427   return s_state.kp->futimens(fd, times);
428 }
429
430 mode_t ki_umask(mode_t mask) {
431   ON_NOSYS_RETURN(0);
432   return s_state.kp->umask(mask);
433 }
434
435 int ki_poll(struct pollfd* fds, nfds_t nfds, int timeout) {
436   return s_state.kp->poll(fds, nfds, timeout);
437 }
438
439 int ki_select(int nfds,
440               fd_set* readfds,
441               fd_set* writefds,
442               fd_set* exceptfds,
443               struct timeval* timeout) {
444   return s_state.kp->select(nfds, readfds, writefds, exceptfds, timeout);
445 }
446
447 int ki_tcflush(int fd, int queue_selector) {
448   ON_NOSYS_RETURN(-1);
449   return s_state.kp->tcflush(fd, queue_selector);
450 }
451
452 int ki_tcgetattr(int fd, struct termios* termios_p) {
453   ON_NOSYS_RETURN(-1);
454   return s_state.kp->tcgetattr(fd, termios_p);
455 }
456
457 int ki_tcsetattr(int fd,
458                  int optional_actions,
459                  const struct termios* termios_p) {
460   ON_NOSYS_RETURN(-1);
461   return s_state.kp->tcsetattr(fd, optional_actions, termios_p);
462 }
463
464 int ki_kill(pid_t pid, int sig) {
465   ON_NOSYS_RETURN(-1);
466   return s_state.kp->kill(pid, sig);
467 }
468
469 int ki_killpg(pid_t pid, int sig) {
470   errno = ENOSYS;
471   return -1;
472 }
473
474 int ki_sigaction(int signum,
475                  const struct sigaction* action,
476                  struct sigaction* oaction) {
477   ON_NOSYS_RETURN(-1);
478   return s_state.kp->sigaction(signum, action, oaction);
479 }
480
481 int ki_sigpause(int sigmask) {
482   errno = ENOSYS;
483   return -1;
484 }
485
486 int ki_sigpending(sigset_t* set) {
487   errno = ENOSYS;
488   return -1;
489 }
490
491 int ki_sigsuspend(const sigset_t* set) {
492   errno = ENOSYS;
493   return -1;
494 }
495
496 sighandler_t ki_signal(int signum, sighandler_t handler) {
497   return ki_sigset(signum, handler);
498 }
499
500 sighandler_t ki_sigset(int signum, sighandler_t handler) {
501   ON_NOSYS_RETURN(SIG_ERR);
502   // Implement sigset(2) in terms of sigaction(2).
503   struct sigaction action;
504   struct sigaction oaction;
505   memset(&action, 0, sizeof(action));
506   memset(&oaction, 0, sizeof(oaction));
507   action.sa_handler = handler;
508   int rtn = s_state.kp->sigaction(signum, &action, &oaction);
509   if (rtn)
510     return SIG_ERR;
511   return oaction.sa_handler;
512 }
513
514 #ifdef PROVIDES_SOCKET_API
515 // Socket Functions
516 int ki_accept(int fd, struct sockaddr* addr, socklen_t* len) {
517   ON_NOSYS_RETURN(-1);
518   return s_state.kp->accept(fd, addr, len);
519 }
520
521 int ki_bind(int fd, const struct sockaddr* addr, socklen_t len) {
522   ON_NOSYS_RETURN(-1);
523   return s_state.kp->bind(fd, addr, len);
524 }
525
526 int ki_connect(int fd, const struct sockaddr* addr, socklen_t len) {
527   ON_NOSYS_RETURN(-1);
528   return s_state.kp->connect(fd, addr, len);
529 }
530
531 struct hostent* ki_gethostbyname(const char* name) {
532   ON_NOSYS_RETURN(NULL);
533   return s_state.kp->gethostbyname(name);
534 }
535
536 int ki_getnameinfo(const struct sockaddr *sa,
537                    socklen_t salen,
538                    char *host,
539                    size_t hostlen,
540                    char *serv,
541                    size_t servlen,
542                    unsigned int flags) {
543   ON_NOSYS_RETURN(EAI_SYSTEM);
544   return s_state.kp->getnameinfo(sa, salen, host, hostlen, serv, servlen,
545                                  flags);
546 }
547
548 int ki_getaddrinfo(const char* node,
549                    const char* service,
550                    const struct addrinfo* hints,
551                    struct addrinfo** res) {
552   ON_NOSYS_RETURN(EAI_SYSTEM);
553   return s_state.kp->getaddrinfo(node, service, hints, res);
554 }
555
556 void ki_freeaddrinfo(struct addrinfo* res) {
557   s_state.kp->freeaddrinfo(res);
558 }
559
560 int ki_getpeername(int fd, struct sockaddr* addr, socklen_t* len) {
561   ON_NOSYS_RETURN(-1);
562   return s_state.kp->getpeername(fd, addr, len);
563 }
564
565 int ki_getsockname(int fd, struct sockaddr* addr, socklen_t* len) {
566   ON_NOSYS_RETURN(-1);
567   return s_state.kp->getsockname(fd, addr, len);
568 }
569
570 int ki_getsockopt(int fd, int lvl, int optname, void* optval, socklen_t* len) {
571   ON_NOSYS_RETURN(-1);
572   return s_state.kp->getsockopt(fd, lvl, optname, optval, len);
573 }
574
575 int ki_listen(int fd, int backlog) {
576   ON_NOSYS_RETURN(-1);
577   return s_state.kp->listen(fd, backlog);
578 }
579
580 ssize_t ki_recv(int fd, void* buf, size_t len, int flags) {
581   ON_NOSYS_RETURN(-1);
582   return s_state.kp->recv(fd, buf, len, flags);
583 }
584
585 ssize_t ki_recvfrom(int fd,
586                     void* buf,
587                     size_t len,
588                     int flags,
589                     struct sockaddr* addr,
590                     socklen_t* addrlen) {
591   ON_NOSYS_RETURN(-1);
592   return s_state.kp->recvfrom(fd, buf, len, flags, addr, addrlen);
593 }
594
595 ssize_t ki_recvmsg(int fd, struct msghdr* msg, int flags) {
596   ON_NOSYS_RETURN(-1);
597   return s_state.kp->recvmsg(fd, msg, flags);
598 }
599
600 ssize_t ki_send(int fd, const void* buf, size_t len, int flags) {
601   ON_NOSYS_RETURN(-1);
602   return s_state.kp->send(fd, buf, len, flags);
603 }
604
605 ssize_t ki_sendto(int fd,
606                   const void* buf,
607                   size_t len,
608                   int flags,
609                   const struct sockaddr* addr,
610                   socklen_t addrlen) {
611   ON_NOSYS_RETURN(-1);
612   return s_state.kp->sendto(fd, buf, len, flags, addr, addrlen);
613 }
614
615 ssize_t ki_sendmsg(int fd, const struct msghdr* msg, int flags) {
616   ON_NOSYS_RETURN(-1);
617   return s_state.kp->sendmsg(fd, msg, flags);
618 }
619
620 int ki_setsockopt(int fd,
621                   int lvl,
622                   int optname,
623                   const void* optval,
624                   socklen_t len) {
625   ON_NOSYS_RETURN(-1);
626   return s_state.kp->setsockopt(fd, lvl, optname, optval, len);
627 }
628
629 int ki_shutdown(int fd, int how) {
630   ON_NOSYS_RETURN(-1);
631   return s_state.kp->shutdown(fd, how);
632 }
633
634 int ki_socket(int domain, int type, int protocol) {
635   ON_NOSYS_RETURN(-1);
636   return s_state.kp->socket(domain, type, protocol);
637 }
638
639 int ki_socketpair(int domain, int type, int protocol, int* sv) {
640   ON_NOSYS_RETURN(-1);
641   return s_state.kp->socketpair(domain, type, protocol, sv);
642 }
643 #endif  // PROVIDES_SOCKET_API