Changeset 381
- Timestamp:
- 06/12/07 12:33:40 (2 years ago)
- Files:
-
- version_0/ext/cmain.cpp (modified) (1 diff)
- version_0/ext/ed.cpp (modified) (3 diffs)
- version_0/ext/ed.h (modified) (4 diffs)
- version_0/ext/em.cpp (modified) (2 diffs)
- version_0/ext/em.h (modified) (1 diff)
- version_0/ext/eventmachine.h (modified) (1 diff)
- version_0/ext/pipe.cpp (modified) (5 diffs)
- version_0/ext/rubymain.cpp (modified) (2 diffs)
- version_0/lib/eventmachine.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
version_0/ext/cmain.cpp
r361 r381 299 299 **********/ 300 300 301 extern "C" const char *evma_popen (const char *cmd , const char *mode)302 { 303 if (!EventMachine) 304 throw std::runtime_error ("not initialized"); 305 return EventMachine-> Popen (cmd, mode);301 extern "C" const char *evma_popen (const char *cmd) 302 { 303 if (!EventMachine) 304 throw std::runtime_error ("not initialized"); 305 return EventMachine->Socketpair (cmd); 306 306 } 307 307 version_0/ext/ed.cpp
r377 r381 1169 1169 int ConnectionDescriptor::GetCommInactivityTimeout (int *value) 1170 1170 { 1171 if (value) { 1172 *value = InactivityTimeout; 1173 return 1; 1174 } 1175 else { 1176 // TODO, extended logging, got bad parameter. 1177 return 0; 1178 } 1179 } 1180 1181 1182 /********************************************** 1183 ConnectionDescriptor::SetCommInactivityTimeout 1184 **********************************************/ 1185 1186 int ConnectionDescriptor::SetCommInactivityTimeout (int *value) 1187 { 1188 int out = 0; 1189 1190 if (value) { 1191 if ((*value==0) || (*value >= 2)) { 1192 // Replace the value and send the old one back to the caller. 1193 int v = *value; 1194 *value = InactivityTimeout; 1195 InactivityTimeout = v; 1196 out = 1; 1197 } 1198 else { 1199 // TODO, extended logging, got bad value. 1200 } 1201 } 1202 else { 1203 // TODO, extended logging, got bad parameter. 1204 } 1205 1206 return out; 1207 } 1208 1209 /******************************* 1210 DatagramDescriptor::GetPeername 1211 *******************************/ 1212 1213 bool DatagramDescriptor::GetPeername (struct sockaddr *s) 1214 { 1215 bool ok = false; 1216 if (s) { 1217 memset (s, 0, sizeof(struct sockaddr)); 1218 memcpy (s, &ReturnAddress, sizeof(ReturnAddress)); 1219 ok = true; 1220 } 1221 return ok; 1222 } 1223 1224 1225 1226 /******************************************** 1227 DatagramDescriptor::GetCommInactivityTimeout 1228 ********************************************/ 1229 1230 int DatagramDescriptor::GetCommInactivityTimeout (int *value) 1231 { 1171 1232 if (value) { 1172 1233 *value = InactivityTimeout; … … 1179 1240 } 1180 1241 1181 1182 /********************************************** 1183 ConnectionDescriptor::SetCommInactivityTimeout 1184 **********************************************/ 1185 1186 int ConnectionDescriptor::SetCommInactivityTimeout (int *value) 1242 /******************************************** 1243 DatagramDescriptor::SetCommInactivityTimeout 1244 ********************************************/ 1245 1246 int DatagramDescriptor::SetCommInactivityTimeout (int *value) 1187 1247 { 1188 1248 int out = 0; … … 1207 1267 } 1208 1268 1209 /*******************************1210 DatagramDescriptor::GetPeername1211 *******************************/1212 1213 bool DatagramDescriptor::GetPeername (struct sockaddr *s)1214 {1215 bool ok = false;1216 if (s) {1217 memset (s, 0, sizeof(struct sockaddr));1218 memcpy (s, &ReturnAddress, sizeof(ReturnAddress));1219 ok = true;1220 }1221 return ok;1222 }1223 1224 1225 1226 /********************************************1227 DatagramDescriptor::GetCommInactivityTimeout1228 ********************************************/1229 1230 int DatagramDescriptor::GetCommInactivityTimeout (int *value)1231 {1232 if (value) {1233 *value = InactivityTimeout;1234 return 1;1235 }1236 else {1237 // TODO, extended logging, got bad parameter.1238 return 0;1239 }1240 }1241 1242 /********************************************1243 DatagramDescriptor::SetCommInactivityTimeout1244 ********************************************/1245 1246 int DatagramDescriptor::SetCommInactivityTimeout (int *value)1247 {1248 int out = 0;1249 1250 if (value) {1251 if ((*value==0) || (*value >= 2)) {1252 // Replace the value and send the old one back to the caller.1253 int v = *value;1254 *value = InactivityTimeout;1255 InactivityTimeout = v;1256 out = 1;1257 }1258 else {1259 // TODO, extended logging, got bad value.1260 }1261 }1262 else {1263 // TODO, extended logging, got bad parameter.1264 }1265 1266 return out;1267 }1268 version_0/ext/ed.h
r377 r381 63 63 64 64 virtual bool GetPeername (struct sockaddr*) {return false;} 65 virtual bool GetSubprocessPid (pid_t*) {return false;} 65 66 66 67 virtual void StartTls() {} … … 264 265 { 265 266 public: 266 PipeDescriptor (FILE*, EventMachine_t*);267 PipeDescriptor (FILE*, pid_t, EventMachine_t*); 267 268 virtual ~PipeDescriptor(); 268 269 … … 276 277 int SendOutboundData (const char*, int); 277 278 virtual int GetOutboundDataSize() {return OutboundDataSize;} 279 280 virtual bool GetSubprocessPid (pid_t*); 278 281 279 282 protected: … … 295 298 int OutboundDataSize; 296 299 300 pid_t SubprocessPid; 301 297 302 private: 298 303 void _DispatchInboundData (const char *buffer, int size); version_0/ext/em.cpp
r379 r381 1296 1296 EventMachine_t::Popen 1297 1297 *********************/ 1298 1298 #if OBSOLETE 1299 1299 const char *EventMachine_t::Popen (const char *cmd, const char *mode) 1300 1300 { … … 1332 1332 #endif 1333 1333 } 1334 #endif // OBSOLETE 1335 1336 /************************** 1337 EventMachine_t::Socketpair 1338 **************************/ 1339 1340 const char *EventMachine_t::Socketpair (const char *cmd) 1341 { 1342 #ifdef OS_WIN32 1343 throw std::runtime_error ("socketpair is currently unavailable on this platform"); 1344 #endif 1345 1346 // The whole rest of this function is only compiled on Unix systems. 1347 // Eventually we need this functionality (or a full-duplex equivalent) on Windows. 1348 #ifdef OS_UNIX 1349 const char *output_binding = NULL; 1350 1351 int sv[2]; 1352 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, sv) < 0) 1353 return NULL; 1354 // from here, all early returns must close the pair of sockets. 1355 1356 // Set the socketpair nonblocking. Obviously DON'T set CLOEXEC. 1357 if (!SetSocketNonblocking (sv[0]) || !SetSocketNonblocking (sv[1])) { 1358 close (sv[0]); 1359 close (sv[1]); 1360 return NULL; 1361 } 1362 1363 pid_t f = fork(); 1364 if (f > 0) { 1365 close (sv[1]); 1366 PipeDescriptor *pd = new PipeDescriptor (fdopen(sv[0], "r+"), f, this); 1367 if (!pd) 1368 throw std::runtime_error ("unable to allocate pipe"); 1369 Add (pd); 1370 output_binding = pd->GetBinding().c_str(); 1371 } 1372 else if (f == 0) { 1373 close (sv[0]); 1374 close (0); 1375 dup2 (sv[1], STDIN_FILENO); 1376 close (sv[1]); 1377 dup2 (STDIN_FILENO, STDOUT_FILENO); 1378 execlp ("ls", "ls", "-l", (char*)NULL); 1379 exit (-1); // end the child process if the exec doesn't work. 1380 } 1381 else 1382 throw std::runtime_error ("no fork"); 1383 1384 return output_binding; 1385 #endif 1386 } 1334 1387 1335 1388 version_0/ext/em.h
r373 r381 72 72 const char *_OpenFileForWriting (const char*); 73 73 const char *Popen (const char*, const char*); 74 const char *Socketpair (const char*); 74 75 75 76 void Add (EventableDescriptor*); version_0/ext/eventmachine.h
r362 r381 58 58 59 59 const char *evma__write_file (const char *filename); 60 const char *evma_popen (const char *cmd , const char *mode);60 const char *evma_popen (const char *cmd); 61 61 62 62 int evma_set_rlimit_nofile (int n_files); version_0/ext/pipe.cpp
r376 r381 25 25 ******************************/ 26 26 27 PipeDescriptor::PipeDescriptor (FILE *fp, EventMachine_t *parent_em):27 PipeDescriptor::PipeDescriptor (FILE *fp, pid_t subpid, EventMachine_t *parent_em): 28 28 EventableDescriptor (fileno (fp), parent_em), 29 29 bReadAttemptedAfterClose (false), … … 31 31 InactivityTimeout (0), 32 32 MyStream (fp), 33 OutboundDataSize (0) 34 { 35 } 33 OutboundDataSize (0), 34 SubprocessPid (subpid) 35 { 36 #ifdef HAVE_EPOLL 37 EpollEvent.events = EPOLLIN; 38 #endif 39 } 40 36 41 37 42 /******************************* … … 188 193 OutboundPages.push_front (OutboundPage (buffer, len)); 189 194 } 195 #ifdef HAVE_EPOLL 196 EpollEvent.events = (EPOLLIN | (SelectForWrite() ? EPOLLOUT : 0)); 197 assert (MyEventMachine); 198 MyEventMachine->Modify (this); 199 #endif 190 200 } 191 201 else { … … 243 253 244 254 245 /******************************** *255 /******************************** 246 256 PipeDescriptor::SendOutboundData 247 257 ********************************/ … … 262 272 OutboundPages.push_back (OutboundPage (buffer, length)); 263 273 OutboundDataSize += length; 274 #ifdef HAVE_EPOLL 275 EpollEvent.events = (EPOLLIN | EPOLLOUT); 276 assert (MyEventMachine); 277 MyEventMachine->Modify (this); 278 #endif 264 279 return length; 265 280 } 266 281 282 /******************************** 283 PipeDescriptor::GetSubprocessPid 284 ********************************/ 285 286 bool PipeDescriptor::GetSubprocessPid (pid_t *pid) 287 { 288 bool ok = false; 289 if (pid && (SubprocessPid > 0)) { 290 *pid = SubprocessPid; 291 ok = true; 292 } 293 return ok; 294 } 295 version_0/ext/rubymain.cpp
r364 r381 313 313 **************/ 314 314 315 static VALUE t_invoke_popen (VALUE self, VALUE cmd , VALUE mode)316 { 317 const char *f = evma_popen (StringValuePtr(cmd) , StringValuePtr(mode));315 static VALUE t_invoke_popen (VALUE self, VALUE cmd) 316 { 317 const char *f = evma_popen (StringValuePtr(cmd)); 318 318 if (!f || !*f) { 319 319 char *err = strerror (errno); … … 399 399 rb_define_module_function (EmModule, "set_timer_quantum", (VALUE(*)(...))t_set_timer_quantum, 1); 400 400 rb_define_module_function (EmModule, "setuid_string", (VALUE(*)(...))t_setuid_string, 1); 401 rb_define_module_function (EmModule, "invoke_popen", (VALUE(*)(...))t_invoke_popen, 2);401 rb_define_module_function (EmModule, "invoke_popen", (VALUE(*)(...))t_invoke_popen, 1); 402 402 403 403 // Provisional: version_0/lib/eventmachine.rb
r373 r381 898 898 #-- 899 899 # 900 def self::popen cmd, mode="r",handler=nil900 def self::popen cmd, handler=nil 901 901 klass = if (handler and handler.is_a?(Class)) 902 902 handler … … 905 905 end 906 906 907 s = invoke_popen cmd , mode907 s = invoke_popen cmd 908 908 c = klass.new s 909 909 @conns[s] = c