| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2014 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 'use strict'; |
| 8 |
| 7 /** | 9 /** |
| 8 * PipeServer manages a set of anonymous pipes. | 10 * PipeServer manages a set of anonymous pipes. |
| 9 */ | 11 */ |
| 10 function PipeServer() { | 12 function PipeServer() { |
| 11 // Start id for anonymous pipes. | 13 // Start id for anonymous pipes. |
| 12 this.anonymousPipeId = 1; | 14 this.anonymousPipeId = 1; |
| 13 | 15 |
| 14 // Status of anonymous pipes. | 16 // Status of anonymous pipes. |
| 15 this.anonymousPipes = {}; | 17 this.anonymousPipes = {}; |
| 16 } | 18 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 31 writers: {}, | 33 writers: {}, |
| 32 readsPending: [], | 34 readsPending: [], |
| 33 writesPending: [], | 35 writesPending: [], |
| 34 }; | 36 }; |
| 35 var pipe = this.anonymousPipes[id]; | 37 var pipe = this.anonymousPipes[id]; |
| 36 pipe.readers[src.pid] = null; | 38 pipe.readers[src.pid] = null; |
| 37 pipe.writers[src.pid] = null; | 39 pipe.writers[src.pid] = null; |
| 38 reply({ | 40 reply({ |
| 39 pipe_id: id, | 41 pipe_id: id, |
| 40 }); | 42 }); |
| 41 } | 43 }; |
| 42 | 44 |
| 43 /** | 45 /** |
| 44 * Handle an anonymous pipe write call. | 46 * Handle an anonymous pipe write call. |
| 45 */ | 47 */ |
| 46 PipeServer.prototype.handleMessageAPipeWrite = function( | 48 PipeServer.prototype.handleMessageAPipeWrite = function( |
| 47 msg, reply, src) { | 49 msg, reply, src) { |
| 48 var id = msg.pipe_id; | 50 var id = msg.pipe_id; |
| 49 var data = msg.data; | 51 var data = msg.data; |
| 50 var dataInitialSize = data.byteLength; | 52 var dataInitialSize = data.byteLength; |
| 51 if (!(id in this.anonymousPipes && | 53 if (!(id in this.anonymousPipes && |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 reply: reply, | 87 reply: reply, |
| 86 replied: replied, | 88 replied: replied, |
| 87 pid: src.pid, | 89 pid: src.pid, |
| 88 }); | 90 }); |
| 89 } else { | 91 } else { |
| 90 reply({ | 92 reply({ |
| 91 count: this.EPIPE, | 93 count: this.EPIPE, |
| 92 }); | 94 }); |
| 93 } | 95 } |
| 94 } | 96 } |
| 95 } | 97 }; |
| 96 | 98 |
| 97 /** | 99 /** |
| 98 * Handle an anonymous pipe read call. | 100 * Handle an anonymous pipe read call. |
| 99 */ | 101 */ |
| 100 PipeServer.prototype.handleMessageAPipeRead = function( | 102 PipeServer.prototype.handleMessageAPipeRead = function( |
| 101 msg, reply, src) { | 103 msg, reply, src) { |
| 102 var id = msg.pipe_id; | 104 var id = msg.pipe_id; |
| 103 var count = msg.count; | 105 var count = msg.count; |
| 104 if (count === 0 || | 106 if (count === 0 || |
| 105 !(id in this.anonymousPipes && | 107 !(id in this.anonymousPipes && |
| (...skipping 29 matching lines...) Expand all Loading... |
| 135 count: count, | 137 count: count, |
| 136 reply: reply, | 138 reply: reply, |
| 137 pid: src.pid, | 139 pid: src.pid, |
| 138 }); | 140 }); |
| 139 } else { | 141 } else { |
| 140 reply({ | 142 reply({ |
| 141 data: new ArrayBuffer(0), | 143 data: new ArrayBuffer(0), |
| 142 }); | 144 }); |
| 143 } | 145 } |
| 144 } | 146 } |
| 145 } | 147 }; |
| 146 | 148 |
| 147 /** | 149 /** |
| 148 * Close handle to an anonymous pipe for a process. | 150 * Close handle to an anonymous pipe for a process. |
| 149 */ | 151 */ |
| 150 PipeServer.prototype.closeAPipe = function(pid, pipeId, writer) { | 152 PipeServer.prototype.closeAPipe = function(pid, pipeId, writer) { |
| 151 if (pipeId in this.anonymousPipes) { | 153 if (pipeId in this.anonymousPipes) { |
| 152 var pipe = this.anonymousPipes[pipeId]; | 154 var pipe = this.anonymousPipes[pipeId]; |
| 153 if (writer) { | 155 if (writer) { |
| 154 delete pipe.writers[pid]; | 156 delete pipe.writers[pid]; |
| 155 } else { | 157 } else { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 169 } else if (Object.keys(pipe.readers).length === 0) { | 171 } else if (Object.keys(pipe.readers).length === 0) { |
| 170 for (var i = 0; i < pipe.writesPending.length; i++) { | 172 for (var i = 0; i < pipe.writesPending.length; i++) { |
| 171 var item = pipe.writesPending[i]; | 173 var item = pipe.writesPending[i]; |
| 172 item.reply({ | 174 item.reply({ |
| 173 count: this.EPIPE, | 175 count: this.EPIPE, |
| 174 }); | 176 }); |
| 175 } | 177 } |
| 176 pipe.writesPending = []; | 178 pipe.writesPending = []; |
| 177 } | 179 } |
| 178 } | 180 } |
| 179 } | 181 }; |
| 180 | 182 |
| 181 /** | 183 /** |
| 182 * Handle an anonymous pipe close call. | 184 * Handle an anonymous pipe close call. |
| 183 */ | 185 */ |
| 184 PipeServer.prototype.handleMessageAPipeClose = function( | 186 PipeServer.prototype.handleMessageAPipeClose = function( |
| 185 msg, reply, src) { | 187 msg, reply, src) { |
| 186 this.closeAPipe(src.pid, msg.pipe_id, msg.writer); | 188 this.closeAPipe(src.pid, msg.pipe_id, msg.writer); |
| 187 reply({ | 189 reply({ |
| 188 result: 0, | 190 result: 0, |
| 189 }); | 191 }); |
| 190 } | 192 }; |
| 191 | 193 |
| 192 | 194 |
| 193 /** | 195 /** |
| 194 * Add spawned pipe entries. | 196 * Add spawned pipe entries. |
| 195 * @params {Object} Dictionary of environment variables passed to process. | 197 * @params {Object} Dictionary of environment variables passed to process. |
| 196 */ | 198 */ |
| 197 PipeServer.prototype.addProcessPipes = function(pid, params) { | 199 PipeServer.prototype.addProcessPipes = function(pid, params) { |
| 198 var fdCount = 0; | 200 var fdCount = 0; |
| 199 for (;;fdCount++) { | 201 for (;;fdCount++) { |
| 200 var entry = 'NACL_SPAWN_FD_SETUP_' + fdCount; | 202 var entry = 'NACL_SPAWN_FD_SETUP_' + fdCount; |
| 201 if (!(entry in params)) | 203 if (!(entry in params)) |
| 202 break; | 204 break; |
| 203 // Pull out a a pipe spec of the form: | 205 // Pull out a a pipe spec of the form: |
| 204 // pipe:<fd number>:<pipe-id>:<is-writer> | 206 // pipe:<fd number>:<pipe-id>:<is-writer> |
| 205 var m = params[entry].match(/pipe:([0-9]+):([0-9]+):([0-9]+)/); | 207 var m = params[entry].match(/pipe:([0-9]+):([0-9]+):([0-9]+)/); |
| 206 if (m) { | 208 if (m) { |
| 207 var fd = parseInt(m[1]); | 209 var fd = parseInt(m[1]); |
| 208 var id = parseInt(m[2]); | 210 var id = parseInt(m[2]); |
| 209 var isWriter = parseInt(m[3]); | 211 var isWriter = parseInt(m[3]); |
| 210 if (id in this.anonymousPipes) { | 212 if (id in this.anonymousPipes) { |
| 211 var pipe = this.anonymousPipes[id]; | 213 var pipe = this.anonymousPipes[id]; |
| 212 if (isWriter) { | 214 if (isWriter) { |
| 213 pipe.writers[pid] = null; | 215 pipe.writers[pid] = null; |
| 214 } else { | 216 } else { |
| 215 pipe.readers[pid] = null; | 217 pipe.readers[pid] = null; |
| 216 } | 218 } |
| 217 } | 219 } |
| 218 } | 220 } |
| 219 } | 221 } |
| 220 } | 222 }; |
| 221 | 223 |
| 222 | 224 |
| 223 /** | 225 /** |
| 224 * Add spawned pipe entries. | 226 * Add spawned pipe entries. |
| 225 * @params {Object} Dictionary of environment variables passed to process. | 227 * @params {Object} Dictionary of environment variables passed to process. |
| 226 */ | 228 */ |
| 227 PipeServer.prototype.deleteProcess = function(pid) { | 229 PipeServer.prototype.deleteProcess = function(pid) { |
| 228 for (var pipeId in this.anonymousPipes) { | 230 for (var pipeId in this.anonymousPipes) { |
| 229 var pipe = this.anonymousPipes[pipeId]; | 231 var pipe = this.anonymousPipes[pipeId]; |
| 230 if (pid in pipe.readers) { | 232 if (pid in pipe.readers) { |
| 231 this.closeAPipe(pid, pipeId, false); | 233 this.closeAPipe(pid, pipeId, false); |
| 232 } | 234 } |
| 233 if (pid in pipe.writers) { | 235 if (pid in pipe.writers) { |
| 234 this.closeAPipe(pid, pipeId, true); | 236 this.closeAPipe(pid, pipeId, true); |
| 235 } | 237 } |
| 236 } | 238 } |
| 237 } | 239 }; |
| OLD | NEW |