OLD | NEW |
1 // Copyright (c) 2014 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2014 The Native Client Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Emulates spawning/waiting process by asking JavaScript to do so. | 5 // Emulates spawning/waiting process by asking JavaScript to do so. |
6 | 6 |
7 // Include quoted spawn.h first so we can build in the presence of an installed | 7 // Include quoted spawn.h first so we can build in the presence of an installed |
8 // copy of nacl-spawn. | 8 // copy of nacl-spawn. |
9 #define IN_NACL_SPAWN_CC | 9 #define IN_NACL_SPAWN_CC |
10 #include "spawn.h" | 10 #include "spawn.h" |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 } else if (S_ISDIR(st.st_mode)) { | 347 } else if (S_ISDIR(st.st_mode)) { |
348 // TODO(bradnelson): Land nacl_io ioctl to support this. | 348 // TODO(bradnelson): Land nacl_io ioctl to support this. |
349 } else if (S_ISCHR(st.st_mode)) { | 349 } else if (S_ISCHR(st.st_mode)) { |
350 // Unsupported. | 350 // Unsupported. |
351 } else if (S_ISBLK(st.st_mode)) { | 351 } else if (S_ISBLK(st.st_mode)) { |
352 // Unsupported. | 352 // Unsupported. |
353 } else if (S_ISFIFO(st.st_mode)) { | 353 } else if (S_ISFIFO(st.st_mode)) { |
354 char entry[100]; | 354 char entry[100]; |
355 snprintf(entry, sizeof entry, | 355 snprintf(entry, sizeof entry, |
356 "NACL_SPAWN_FD_SETUP_%d=pipe:%d:%d:%d", count++, fd, | 356 "NACL_SPAWN_FD_SETUP_%d=pipe:%d:%d:%d", count++, fd, |
357 static_cast<int>(st.st_ino), st.st_rdev == O_WRONLY); | 357 static_cast<int>(st.st_ino), |
| 358 (st.st_rdev & O_WRONLY) == O_WRONLY); |
358 nspawn_array_appendstring(envs_var, entry); | 359 nspawn_array_appendstring(envs_var, entry); |
359 } else if (S_ISLNK(st.st_mode)) { | 360 } else if (S_ISLNK(st.st_mode)) { |
360 // Unsupported. | 361 // Unsupported. |
361 } else if (S_ISSOCK(st.st_mode)) { | 362 } else if (S_ISSOCK(st.st_mode)) { |
362 // Unsupported. | 363 // Unsupported. |
363 } | 364 } |
364 } | 365 } |
365 return 0; | 366 return 0; |
366 } | 367 } |
367 | 368 |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
679 if (data) { | 680 if (data) { |
680 *data = static_cast<char*>(malloc(result_len + 1)); | 681 *data = static_cast<char*>(malloc(result_len + 1)); |
681 assert(*data); | 682 assert(*data); |
682 memcpy(*data, result, result_len); | 683 memcpy(*data, result, result_len); |
683 (*data)[result_len] = '\0'; | 684 (*data)[result_len] = '\0'; |
684 } | 685 } |
685 nspawn_var_release(result_var); | 686 nspawn_var_release(result_var); |
686 nspawn_var_release(result_dict_var); | 687 nspawn_var_release(result_dict_var); |
687 } | 688 } |
688 | 689 |
689 // Create a javascript pipe. pipefd[0] will be the read end of the pipe | 690 // Same as above with flags. |
690 // and pipefd[1] the write end of the pipe. | 691 int nacl_spawn_pipe2(int pipefd[2], int flags) { |
691 int nacl_spawn_pipe(int pipefd[2]) { | |
692 if (pipefd == NULL) { | 692 if (pipefd == NULL) { |
693 errno = EFAULT; | 693 errno = EFAULT; |
694 return -1; | 694 return -1; |
695 } | 695 } |
696 | 696 |
697 struct PP_Var req_var = nspawn_dict_create(); | 697 struct PP_Var req_var = nspawn_dict_create(); |
698 nspawn_dict_setstring(req_var, "command", "nacl_apipe"); | 698 nspawn_dict_setstring(req_var, "command", "nacl_apipe"); |
699 | 699 |
700 struct PP_Var result_var = nspawn_send_request(req_var); | 700 struct PP_Var result_var = nspawn_send_request(req_var); |
701 int id = nspawn_dict_getint(result_var, "pipe_id"); | 701 int id = nspawn_dict_getint(result_var, "pipe_id"); |
702 nspawn_var_release(result_var); | 702 nspawn_var_release(result_var); |
703 | 703 |
704 int read_fd; | 704 int read_fd; |
705 int write_fd; | 705 int write_fd; |
706 char path[100]; | 706 char path[100]; |
707 sprintf(path, "/apipe/%d", id); | 707 sprintf(path, "/apipe/%d", id); |
708 read_fd = open(path, O_RDONLY); | 708 // The only flag supported is O_NONBLOCK. |
709 write_fd = open(path, O_WRONLY); | 709 // TODO(bradnelson): Drop this once nacl_io does the right thing: |
| 710 // https://bugs.chromium.org/p/webports/issues/detail?id=247 |
| 711 // https://bugs.chromium.org/p/webports/issues/detail?id=248 |
| 712 assert(flags == 0 || flags == O_NONBLOCK); |
| 713 read_fd = open(path, O_RDONLY | flags); |
| 714 write_fd = open(path, O_WRONLY | flags); |
710 if (read_fd < 0 || write_fd < 0) { | 715 if (read_fd < 0 || write_fd < 0) { |
711 if (read_fd >= 0) { | 716 if (read_fd >= 0) { |
712 close(read_fd); | 717 close(read_fd); |
713 } | 718 } |
714 if (write_fd >= 0) { | 719 if (write_fd >= 0) { |
715 close(write_fd); | 720 close(write_fd); |
716 } | 721 } |
717 return -1; | 722 return -1; |
718 } | 723 } |
719 pipefd[0] = read_fd; | 724 pipefd[0] = read_fd; |
720 pipefd[1] = write_fd; | 725 pipefd[1] = write_fd; |
721 | 726 |
722 return 0; | 727 return 0; |
723 } | 728 } |
724 | 729 |
| 730 // Create a javascript pipe. pipefd[0] will be the read end of the pipe |
| 731 // and pipefd[1] the write end of the pipe. |
| 732 int nacl_spawn_pipe(int pipefd[2]) { |
| 733 return nacl_spawn_pipe2(pipefd, 0); |
| 734 } |
| 735 |
725 void nacl_spawn_vfork_before(void) { | 736 void nacl_spawn_vfork_before(void) { |
726 assert(!vforking); | 737 assert(!vforking); |
727 vforking = 1; | 738 vforking = 1; |
728 stash_file_descriptors(); | 739 stash_file_descriptors(); |
729 } | 740 } |
730 | 741 |
731 pid_t nacl_spawn_vfork_after(int jmping) { | 742 pid_t nacl_spawn_vfork_after(int jmping) { |
732 if (jmping) { | 743 if (jmping) { |
733 unstash_file_descriptors(); | 744 unstash_file_descriptors(); |
734 vforking = 0; | 745 vforking = 0; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 } | 836 } |
826 | 837 |
827 int execlpe(const char *path, const char *arg, ...) { /* char* const envp[] */ | 838 int execlpe(const char *path, const char *arg, ...) { /* char* const envp[] */ |
828 NSPAWN_LOG("execlpe: %s", path); | 839 NSPAWN_LOG("execlpe: %s", path); |
829 VARG_TO_ARGV_ENVP; | 840 VARG_TO_ARGV_ENVP; |
830 // TODO(bradnelson): Limit path resolution to 'p' variants. | 841 // TODO(bradnelson): Limit path resolution to 'p' variants. |
831 return spawnve_impl(P_OVERLAY, path, argv, envp); | 842 return spawnve_impl(P_OVERLAY, path, argv, envp); |
832 } | 843 } |
833 | 844 |
834 }; // extern "C" | 845 }; // extern "C" |
OLD | NEW |