| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler, Virtual Machine, and Synthesis Engine |
| 4 | |
| 5 | Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved. |
| 6 | http://chuck.stanford.edu/ |
| 7 | http://chuck.cs.princeton.edu/ |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the dual-license terms of EITHER the MIT License OR the GNU |
| 11 | General Public License (the latter as published by the Free Software |
| 12 | Foundation; either version 2 of the License or, at your option, any |
| 13 | later version). |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful and/or |
| 16 | interesting, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | MIT Licence and/or the GNU General Public License for details. |
| 19 | |
| 20 | You should have received a copy of the MIT License and the GNU General |
| 21 | Public License (GPL) along with this program; a copy of the GPL can also |
| 22 | be obtained by writing to the Free Software Foundation, Inc., 59 Temple |
| 23 | Place, Suite 330, Boston, MA 02111-1307 U.S.A. |
| 24 | -----------------------------------------------------------------------------*/ |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | // file: chuck_otf.cpp |
| 28 | // desc: on-the-fly programming utilities |
| 29 | // |
| 30 | // author: Ge Wang ([email protected] | [email protected]) |
| 31 | // date: Autumn 2004 |
| 32 | //----------------------------------------------------------------------------- |
| 33 | #include "chuck_otf.h" |
| 34 | #include "chuck_compile.h" |
| 35 | #include "chuck_errmsg.h" |
| 36 | #include "chuck.h" |
| 37 | #include "util_math.h" |
| 38 | #include "util_thread.h" |
| 39 | #include "util_string.h" |
| 40 | #include "util_platforms.h" |
| 41 | |
| 42 | #include <stdio.h> |
| 43 | #include <string.h> |
| 44 | #include <sys/types.h> |
| 45 | #include <sys/stat.h> |
| 46 | #include <signal.h> |
| 47 | #include <time.h> |
| 48 | |
| 49 | #ifndef __PLATFORM_WINDOWS__ |
| 50 | #include <unistd.h> |
| 51 | // linux 64-bit byte ordering | 1.5.1.5 |
| 52 | #if defined(__PLATFORM_LINUX__) |
| 53 | #include <endian.h> |
| 54 | #ifndef htonll |
| 55 | #define htonll htobe64 |
| 56 | #endif |
| 57 | #ifndef ntohll |
| 58 | #define ntohll be64toh |
| 59 | #endif |
| 60 | #endif |
| 61 | #else // 2022 QTSIN |
| 62 | #include <winsock2.h> |
| 63 | #include <ws2tcpip.h> |
| 64 | #include <windows.h> |
| 65 | #endif |
| 66 | |
| 67 | using namespace std; |
| 68 | |
| 69 | // log level |
| 70 | t_CKUINT g_otf_log = CK_LOG_INFO; |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | //----------------------------------------------------------------------------- |
| 76 | // name: otf_hton( ) |
| 77 | // desc: host to network endian byte-ordering |
| 78 | //----------------------------------------------------------------------------- |
| 79 | void otf_hton( OTF_Net_Msg * msg ) |
| 80 | { |
| 81 | msg->header = htonll( msg->header ); |
| 82 | msg->type = htonll( msg->type ); |
| 83 | msg->param = htonll( msg->param ); |
| 84 | msg->param2 = htonll( msg->param2 ); |
| 85 | msg->param3 = htonll( msg->param3 ); |
| 86 | msg->length = htonll( msg->length ); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | //----------------------------------------------------------------------------- |
| 93 | // name: otf_ntoh( ) |
| 94 | // desc: network to host endian byte-ordering |
| 95 | //----------------------------------------------------------------------------- |
| 96 | void otf_ntoh( OTF_Net_Msg * msg ) |
| 97 | { |
| 98 | msg->header = ntohll( msg->header ); |
| 99 | msg->type = ntohll( msg->type ); |
| 100 | msg->param = ntohll( msg->param ); |
| 101 | msg->param2 = ntohll( msg->param2 ); |
| 102 | msg->param3 = ntohll( msg->param3 ); |
| 103 | msg->length = ntohll( msg->length ); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | //----------------------------------------------------------------------------- |
| 110 | // name: recv_file() |
| 111 | // desc: receive a file over network |
| 112 | //----------------------------------------------------------------------------- |
| 113 | FILE * recv_file( const OTF_Net_Msg & msg, ck_socket sock ) |
| 114 | { |
| 115 | OTF_Net_Msg buf; |
| 116 | |
| 117 | // make a temp file |
| 118 | FILE * fd = ck_tmpfile(); |
| 119 | // check if successful |
| 120 | if( !fd ) |
| 121 | { |
| 122 | EM_error2( 0, "OTF nable to create temp file, skipping..." ); |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | do { |
| 127 | // msg |
| 128 | if( !ck_recv( sock, (char *)&buf, sizeof(buf) ) ) goto error; |
| 129 | // network to host byte ordering |
| 130 | otf_ntoh( &buf ); |
| 131 | // write |
| 132 | fwrite( buf.buffer, sizeof(char), buf.length, fd ); |
| 133 | // while there is more |
| 134 | }while( buf.param2 > 0 ); |
| 135 | |
| 136 | // check for error |
| 137 | if( buf.param2 == CK_NET_ERROR ) |
| 138 | { |
| 139 | // this was a problem |
| 140 | EM_log( CK_LOG_INFO, "(via otf): received error flag, dropping..." ); |
| 141 | // done |
| 142 | goto error; |
| 143 | } |
| 144 | |
| 145 | // return file descriptor |
| 146 | return fd; |
| 147 | |
| 148 | error: |
| 149 | // close file descriptor |
| 150 | fclose( fd ); |
| 151 | // zero it out |
| 152 | fd = NULL; |
| 153 | return NULL; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | //----------------------------------------------------------------------------- |
| 160 | // name: otf_process_msg() |
| 161 | // desc: process incoming OTF mes from network |
| 162 | // FYI previous to 1.5.1.3, this was also called by Machine.add() etc... |
| 163 | // however, but no longer, as of 1.5.1.3 Machine directly calls in VM, |
| 164 | // removing dependency on OTF and the socket code |
| 165 | //----------------------------------------------------------------------------- |
| 166 | t_CKUINT otf_process_msg( Chuck_VM * vm, Chuck_Compiler * compiler, |
| 167 | OTF_Net_Msg * msg, t_CKBOOL immediate, void * data ) |
| 168 | { |
| 169 | Chuck_Msg * cmd = new Chuck_Msg; |
| 170 | Chuck_VM_Code * code = NULL; |
| 171 | FILE * fd = NULL; |
| 172 | t_CKUINT ret = 0; |
| 173 | |
| 174 | // CK_FPRINTF_STDERR( "UDP message recv...\n" ); |
| 175 | if( msg->type == CK_MSG_ADD || msg->type == CK_MSG_REPLACE ) |
| 176 | { |
| 177 | string filename; |
| 178 | vector<string> args; |
| 179 | |
| 180 | // parse out command line arguments |
| 181 | if( !extract_args( msg->buffer, filename, args ) ) |
| 182 | { |
| 183 | // error |
| 184 | EM_error2( 0, "malformed filename with argument list..." ); |
| 185 | EM_error2( 0, " --> '%s'", msg->buffer ); |
| 186 | CK_SAFE_DELETE(cmd); |
| 187 | goto cleanup; |
| 188 | } |
| 189 | |
| 190 | // copy de-escaped filename |
| 191 | strcpy( msg->buffer, filename.c_str() ); |
| 192 | // copy args if needed |
| 193 | if( args.size() > 0 ) |
| 194 | { |
| 195 | cmd->args = new vector<string>; |
| 196 | *(cmd->args) = args; |
| 197 | } |
| 198 | |
| 199 | // see if entire file is on the way |
| 200 | if( msg->param2 && msg->param2 != CK_NET_ERROR ) |
| 201 | { |
| 202 | fd = recv_file( *msg, (ck_socket)data ); |
| 203 | if( !fd ) |
| 204 | { |
| 205 | EM_error2( 0, "incoming source transfer '%s' failed...", |
| 206 | mini(msg->buffer) ); |
| 207 | CK_SAFE_DELETE(cmd); |
| 208 | goto cleanup; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | // construct full path to be associated with the file so me.sourceDir() works |
| 213 | // (added 1.3.5.2) |
| 214 | std::string full_path = normalize_filepath_append_ck( msg->buffer ); |
| 215 | |
| 216 | // construct a target to be compiled | 1.5.4.0 (ge) |
| 217 | Chuck_CompileTarget * target = new Chuck_CompileTarget(); |
| 218 | // set file descriptor |
| 219 | target->fd2parse = fd; |
| 220 | // set fields |
| 221 | target->filename = msg->buffer; |
| 222 | target->absolutePath = full_path; |
| 223 | |
| 224 | // parse, type-check, and emit; compiler will memory-manage target (no need to delete here) |
| 225 | if( !compiler->compile( target ) ) |
| 226 | { |
| 227 | CK_SAFE_DELETE(cmd); |
| 228 | goto cleanup; |
| 229 | } |
| 230 | |
| 231 | // get the code |
| 232 | code = compiler->output(); |
| 233 | // (re) name it | 1.5.0.5 (ge) update from '+=' to '=' |
| 234 | code->name = string(msg->buffer); |
| 235 | |
| 236 | // set the flags for the command |
| 237 | cmd->type = msg->type; |
| 238 | cmd->code = code; |
| 239 | // copy param |
| 240 | if( cmd->type == CK_MSG_REPLACE ) |
| 241 | cmd->param = msg->param; |
| 242 | |
| 243 | // interpret param3 | 1.5.1.5 (nshaheed & ge) |
| 244 | if( msg->param3 ) |
| 245 | { |
| 246 | // flag this for always-add replace |
| 247 | cmd->alwaysAdd = TRUE; |
| 248 | // if add message |
| 249 | if( msg->type == CK_MSG_ADD ) |
| 250 | { |
| 251 | // rewrite as an always-add replace message |
| 252 | cmd->type = CK_MSG_REPLACE; |
| 253 | // copy the param for replace |
| 254 | cmd->param = msg->param; |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | else if( msg->type == CK_MSG_STATUS || msg->type == CK_MSG_REMOVE || msg->type == CK_MSG_REMOVEALL |
| 259 | || msg->type == CK_MSG_EXIT || msg->type == CK_MSG_TIME || msg->type == CK_MSG_RESET_ID |
| 260 | || msg->type == CK_MSG_CLEARVM ) |
| 261 | { |
| 262 | cmd->type = msg->type; |
| 263 | cmd->param = msg->param; |
| 264 | } |
| 265 | else if( msg->type == CK_MSG_ABORT ) |
| 266 | { |
| 267 | // halt and clear current shred |
| 268 | vm->abort_current_shred(); |
| 269 | // short circuit |
| 270 | ret = 1; |
| 271 | CK_SAFE_DELETE(cmd); |
| 272 | goto cleanup; |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | EM_error2( 0, "unrecognized incoming command from network: '%li'", cmd->type ); |
| 277 | CK_SAFE_DELETE(cmd); |
| 278 | goto cleanup; |
| 279 | } |
| 280 | |
| 281 | // immediate |
| 282 | if( immediate ) |
| 283 | { |
| 284 | // process message immediately |
| 285 | ret = vm->process_msg( cmd ); |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | // queue message |
| 290 | vm->queue_msg( cmd, 1 ); |
| 291 | // return code |
| 292 | ret = 1; |
| 293 | } |
| 294 | |
| 295 | cleanup: |
| 296 | // close file handle |
| 297 | // if( fd ) fclose( fd ); |
| 298 | // 1.5.4.1 (ge) not needed, file handle should be cleaned up as part of compile-target |
| 299 | |
| 300 | return ret; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | //----------------------------------------------------------------------------- |
| 307 | // name: otf_send_file() |
| 308 | // desc: send a chuck program over network |
| 309 | //----------------------------------------------------------------------------- |
| 310 | t_CKINT otf_send_file( const char * fname, OTF_Net_Msg & msg, const char * op, |
| 311 | ck_socket dest ) |
| 312 | { |
| 313 | FILE * fd = NULL; |
| 314 | struct stat fs; |
| 315 | string filename; |
| 316 | vector<string> args; |
| 317 | string buf; |
| 318 | t_CKUINT amountRead = 0; |
| 319 | |
| 320 | // use this to send | 1.5.1.5 |
| 321 | OTF_Net_Msg msg2send; |
| 322 | |
| 323 | // parse out command line arguments |
| 324 | if( !extract_args( fname, filename, args ) ) |
| 325 | { |
| 326 | // error |
| 327 | EM_error2( 0, "malformed filename + argument list..." ); |
| 328 | EM_error2( 0, " --> '%s'", fname ); |
| 329 | return FALSE; |
| 330 | } |
| 331 | |
| 332 | // filename and any args |
| 333 | strcpy( msg.buffer, fname ); |
| 334 | |
| 335 | // test it |
| 336 | buf = filename; |
| 337 | fd = ck_openFileAutoExt( buf, ".ck" ); |
| 338 | if( !fd ) |
| 339 | { |
| 340 | EM_error2( 0, "cannot open file '%s' for [%s]...", filename.c_str(), op ); |
| 341 | return FALSE; |
| 342 | } |
| 343 | |
| 344 | // make a target |
| 345 | Chuck_CompileTarget * target = new Chuck_CompileTarget(); |
| 346 | target->fd2parse = fd; |
| 347 | target->filename = filename; |
| 348 | |
| 349 | // set current target |
| 350 | EM_setCurrentTarget( target ); |
| 351 | |
| 352 | // check to see if at least parses |
| 353 | if( !chuck_parse( target ) ) |
| 354 | { |
| 355 | // error message |
| 356 | EM_error2( 0, "(parse error) skipping file '%s' for [%s]...", filename.c_str(), op ); |
| 357 | // reset parser (clean up) | 1.5.1.5 |
| 358 | reset_parse(); |
| 359 | // clean up target, including closing the fd |
| 360 | CK_SAFE_DELETE( target ); |
| 361 | return FALSE; |
| 362 | } |
| 363 | // reset parser (clean up) | 1.5.1.5 |
| 364 | reset_parse(); |
| 365 | |
| 366 | // stat it |
| 367 | memset( &fs, 0, sizeof(fs) ); |
| 368 | stat( buf.c_str(), &fs ); |
| 369 | fseek( fd, 0, SEEK_SET ); |
| 370 | |
| 371 | // log |
| 372 | EM_log( CK_LOG_INFO, "sending TCP file %s, size=%d", filename.c_str(), (t_CKUINT)fs.st_size ); |
| 373 | EM_pushlog(); |
| 374 | |
| 375 | // send the first packet |
| 376 | msg.param2 = (t_CKUINT)fs.st_size; |
| 377 | msg.length = 0; |
| 378 | // copy it (to preserve the endianness of the original msg) |
| 379 | msg2send = msg; |
| 380 | // host to network byte ordering |
| 381 | otf_hton( &msg2send ); |
| 382 | // go |
| 383 | ck_send( dest, (char *)&msg2send, sizeof(msg2send) ); |
| 384 | |
| 385 | // send the whole thing |
| 386 | t_CKUINT left = (t_CKUINT)fs.st_size; |
| 387 | while( left ) |
| 388 | { |
| 389 | // log |
| 390 | EM_log( CK_LOG_FINER, "%03d bytes left...", left ); |
| 391 | // amount to send |
| 392 | msg.length = left > CK_NET_BUFFER_SIZE ? CK_NET_BUFFER_SIZE : left; |
| 393 | // read |
| 394 | amountRead = fread( msg.buffer, sizeof(char), msg.length, fd ); |
| 395 | // check for error |
| 396 | if( !amountRead ) |
| 397 | { |
| 398 | // error encountered |
| 399 | EM_error2( 0, "error while reading '%s'...", filename.c_str() ); |
| 400 | // mark done |
| 401 | left = 0; |
| 402 | // error flag |
| 403 | msg.param2 = CK_NET_ERROR; |
| 404 | } |
| 405 | else |
| 406 | { |
| 407 | // amount left |
| 408 | left -= amountRead; |
| 409 | // what's left |
| 410 | msg.param2 = left; |
| 411 | } |
| 412 | |
| 413 | // log |
| 414 | EM_log( CK_LOG_FINER, "sending buffer %03d length %03d...", amountRead, msg.length ); |
| 415 | // copy it (to preserve the endianness of the original msg) |
| 416 | msg2send = msg; |
| 417 | // host to network byte order |
| 418 | otf_hton( &msg2send ); |
| 419 | // send it |
| 420 | ck_send( dest, (char *)&msg2send, sizeof(msg2send) ); |
| 421 | } |
| 422 | |
| 423 | // pop log |
| 424 | EM_poplog(); |
| 425 | |
| 426 | // clean up target, including closing the fd |
| 427 | CK_SAFE_DELETE( target ); |
| 428 | |
| 429 | return TRUE; |
| 430 | } |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | //----------------------------------------------------------------------------- |
| 436 | // name: otf_send_connect() |
| 437 | // desc: ... |
| 438 | //----------------------------------------------------------------------------- |
| 439 | ck_socket otf_send_connect( const char * host, t_CKINT port ) |
| 440 | { |
| 441 | // log |
| 442 | EM_log( CK_LOG_INFO, "otf connect: %s:%i", host, port ); |
| 443 | |
| 444 | ck_socket sock = ck_tcp_create( 0 ); |
| 445 | if( !sock ) |
| 446 | { |
| 447 | EM_error2( 0, "cannot open socket to send command..." ); |
| 448 | return NULL; |
| 449 | } |
| 450 | |
| 451 | if( strcmp( host, "127.0.0.1" ) ) |
| 452 | EM_log( g_otf_log, "connecting to %s on port %i via TCP...", host, port ); |
| 453 | |
| 454 | if( !ck_connect( sock, host, (int)port ) ) |
| 455 | { |
| 456 | EM_error2( 0, "cannot open TCP socket on %s:%i...", host, port ); |
| 457 | ck_close( sock ); |
| 458 | return NULL; |
| 459 | } |
| 460 | |
| 461 | ck_send_timeout( sock, 0, 2000000 ); |
| 462 | |
| 463 | return sock; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | |
| 468 | |
| 469 | //----------------------------------------------------------------------------- |
| 470 | // name: otf_send_cmd() |
| 471 | // desc: parse for OTF commands from args and send as OTF messages |
| 472 | //----------------------------------------------------------------------------- |
| 473 | t_CKINT otf_send_cmd( t_CKINT argc, const char ** argv, t_CKINT & i, |
| 474 | const char * host, t_CKINT port, t_CKINT * is_otf ) |
| 475 | { |
| 476 | OTF_Net_Msg msg; |
| 477 | // REFACTOR-2017 TODO: wat is global sigpipe mode |
| 478 | // g_sigpipe_mode = 1; |
| 479 | int tasks_total = 0, tasks_done = 0; |
| 480 | ck_socket dest = NULL; |
| 481 | if( is_otf ) *is_otf = TRUE; |
| 482 | |
| 483 | // log |
| 484 | EM_log( CK_LOG_INFO, "examining otf command '%s'...", argv[i] ); |
| 485 | |
| 486 | if( !strcmp( argv[i], "--add" ) || !strcmp( argv[i], "+" ) ) |
| 487 | { |
| 488 | if( ++i >= argc ) |
| 489 | { |
| 490 | EM_error2( 0, "not enough arguments following [add]..." ); |
| 491 | goto error; |
| 492 | } |
| 493 | |
| 494 | if( !(dest = otf_send_connect( host, port )) ) return 0; |
| 495 | EM_pushlog(); |
| 496 | do { |
| 497 | // log |
| 498 | EM_log( CK_LOG_INFO, "sending file:args '%s' for ADD...", mini(argv[i]) ); |
| 499 | msg.type = CK_MSG_ADD; |
| 500 | msg.param = 1; |
| 501 | tasks_done += otf_send_file( argv[i], msg, "add", dest ); |
| 502 | tasks_total++; |
| 503 | } while( ++i < argc ); |
| 504 | // log |
| 505 | EM_poplog(); |
| 506 | |
| 507 | if( !tasks_done ) |
| 508 | goto error; |
| 509 | } |
| 510 | else if( !strcmp( argv[i], "--remove" ) || !strcmp( argv[i], "-" ) ) |
| 511 | { |
| 512 | if( ++i >= argc ) |
| 513 | { |
| 514 | EM_error2( 0, "not enough arguments following [remove]..." ); |
| 515 | goto error; |
| 516 | } |
| 517 | |
| 518 | if( !(dest = otf_send_connect( host, port )) ) return 0; |
| 519 | EM_pushlog(); |
| 520 | do { |
| 521 | // log |
| 522 | EM_log( CK_LOG_INFO, "requesting REMOVE of shred '%i'..." ); |
| 523 | // set param |
| 524 | msg.param = ck_atoul( argv[i] ); |
| 525 | // set type |
| 526 | msg.type = CK_MSG_REMOVE; |
| 527 | // host to network byte ordering |
| 528 | otf_hton( &msg ); |
| 529 | ck_send( dest, (char *)&msg, sizeof(msg) ); |
| 530 | } while( ++i < argc ); |
| 531 | // log |
| 532 | EM_poplog(); |
| 533 | } |
| 534 | else if( !strcmp( argv[i], "--" ) ) |
| 535 | { |
| 536 | if( !(dest = otf_send_connect( host, port )) ) return 0; |
| 537 | EM_pushlog(); |
| 538 | // log |
| 539 | EM_log( CK_LOG_INFO, "requesting REMOVE LAST shred..." ); |
| 540 | msg.param = CK_NO_VALUE; |
| 541 | msg.type = CK_MSG_REMOVE; |
| 542 | otf_hton( &msg ); |
| 543 | ck_send( dest, (char *)&msg, sizeof(msg) ); |
| 544 | // log |
| 545 | EM_poplog(); |
| 546 | } |
| 547 | else if( !strcmp( argv[i], "--replace" ) || !strcmp( argv[i], "=" ) ) |
| 548 | { |
| 549 | if( ++i >= argc ) |
| 550 | { |
| 551 | EM_error2( 0, "not enough arguments following [replace]..." ); |
| 552 | goto error; |
| 553 | } |
| 554 | |
| 555 | if( i <= 0 ) |
| 556 | msg.param = CK_NO_VALUE; |
| 557 | else |
| 558 | { |
| 559 | // arg as c++ string |
| 560 | string arg = trim(argv[i]); |
| 561 | // check for '%' | 1.5.1.5 (nshaheed & ge) |
| 562 | if( arg.length() && arg[0] == '%' ) |
| 563 | { |
| 564 | // convert into number |
| 565 | msg.param = ck_atoul( argv[i]+1 ); |
| 566 | // flag this for always add (whether the remove ID is present or not) |
| 567 | msg.param3 = TRUE; |
| 568 | } |
| 569 | else // no '%' |
| 570 | { |
| 571 | // convert |
| 572 | msg.param = ck_atoul( argv[i] ); |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | if( ++i >= argc ) |
| 577 | { |
| 578 | EM_error2( 0, "not enough arguments following [replace]..." ); |
| 579 | goto error; |
| 580 | } |
| 581 | |
| 582 | if( !(dest = otf_send_connect( host, port )) ) return 0; |
| 583 | EM_pushlog(); |
| 584 | EM_log( CK_LOG_INFO, "requesting REPLACE shred '%i' with '%s'...", msg.param, mini(argv[i]) ); |
| 585 | msg.type = CK_MSG_REPLACE; |
| 586 | if( !otf_send_file( argv[i], msg, "replace", dest ) ) |
| 587 | goto error; |
| 588 | EM_poplog(); |
| 589 | } |
| 590 | else if( !strcmp( argv[i], "--removeall" ) || !strcmp( argv[i], "--remall" ) || !strcmp( argv[i], "--remove.all" ) ) |
| 591 | { |
| 592 | if( !(dest = otf_send_connect( host, port )) ) return 0; |
| 593 | EM_pushlog(); |
| 594 | EM_log( CK_LOG_INFO, "requesting REMOVE ALL..." ); |
| 595 | msg.type = CK_MSG_REMOVEALL; |
| 596 | msg.param = 0; |
| 597 | otf_hton( &msg ); |
| 598 | ck_send( dest, (char *)&msg, sizeof(msg) ); |
| 599 | EM_poplog(); |
| 600 | } |
| 601 | else if( !strcmp( argv[i], "--clear.vm" ) || !strcmp( argv[i], "--clear-vm" ) ) |
| 602 | { |
| 603 | if( !(dest = otf_send_connect( host, port )) ) return 0; |
| 604 | EM_pushlog(); |
| 605 | EM_log( CK_LOG_INFO, "requesting CLEAR VM..." ); |
| 606 | msg.type = CK_MSG_CLEARVM; |
| 607 | msg.param = 0; |
| 608 | otf_hton( &msg ); |
| 609 | ck_send( dest, (char *)&msg, sizeof(msg) ); |
| 610 | EM_poplog(); |
| 611 | } |
| 612 | else if( !strcmp( argv[i], "--kill" ) || !strcmp( argv[i], "--exit" ) ) |
| 613 | { |
| 614 | if( !(dest = otf_send_connect( host, port )) ) return 0; |
| 615 | EM_pushlog(); |
| 616 | EM_log( CK_LOG_INFO, "requesting EXIT..." ); |
| 617 | msg.type = CK_MSG_REMOVEALL; |
| 618 | msg.param = 0; |
| 619 | otf_hton( &msg ); |
| 620 | ck_send( dest, (char *)&msg, sizeof(msg) ); |
| 621 | msg.type = CK_MSG_EXIT; |
| 622 | msg.param = (i+1)<argc ? ck_atoul(argv[++i]) : 0; |
| 623 | otf_hton( &msg ); |
| 624 | ck_send( dest, (char *)&msg, sizeof(msg) ); |
| 625 | EM_poplog(); |
| 626 | } |
| 627 | else if( !strcmp( argv[i], "--time" ) ) |
| 628 | { |
| 629 | if( !(dest = otf_send_connect( host, port )) ) return 0; |
| 630 | EM_pushlog(); |
| 631 | EM_log( CK_LOG_INFO, "requesting TIME..." ); |
| 632 | msg.type = CK_MSG_TIME; |
| 633 | msg.param = 0; |
| 634 | otf_hton( &msg ); |
| 635 | ck_send( dest, (char *)&msg, sizeof(msg) ); |
| 636 | EM_poplog(); |
| 637 | } |
| 638 | else if( !strcmp( argv[i], "--rid" ) || !strcmp( argv[i], "--reset.id" ) || !strcmp( argv[i], "--reset-id" ) ) |
| 639 | { |
| 640 | if( !(dest = otf_send_connect( host, port )) ) return 0; |
| 641 | EM_pushlog(); |
| 642 | EM_log( CK_LOG_INFO, "requesting RESET ID..." ); |
| 643 | msg.type = CK_MSG_RESET_ID; |
| 644 | msg.param = 0; |
| 645 | otf_hton( &msg ); |
| 646 | ck_send( dest, (char *)&msg, sizeof(msg) ); |
| 647 | EM_poplog(); |
| 648 | } |
| 649 | else if( !strcmp( argv[i], "--status" ) || !strcmp( argv[i], "^" ) ) |
| 650 | { |
| 651 | if( !(dest = otf_send_connect( host, port )) ) return 0; |
| 652 | EM_pushlog(); |
| 653 | EM_log( CK_LOG_INFO, "requesting STATUS..." ); |
| 654 | msg.type = CK_MSG_STATUS; |
| 655 | msg.param = 0; |
| 656 | otf_hton( &msg ); |
| 657 | ck_send( dest, (char *)&msg, sizeof(msg) ); |
| 658 | EM_poplog(); |
| 659 | } |
| 660 | else if( !strcmp( argv[i], "--abort.shred" ) || !strcmp( argv[i], "--abort-shred" ) ) |
| 661 | { |
| 662 | if( !(dest = otf_send_connect( host, port )) ) return 0; |
| 663 | EM_pushlog(); |
| 664 | EM_log( CK_LOG_INFO, "requesting ABORT SHRED..." ); |
| 665 | msg.type = CK_MSG_ABORT; |
| 666 | msg.param = 0; |
| 667 | otf_hton( &msg ); |
| 668 | ck_send( dest, (char *)&msg, sizeof(msg) ); |
| 669 | EM_poplog(); |
| 670 | } |
| 671 | else |
| 672 | { |
| 673 | if( is_otf ) *is_otf = FALSE; |
| 674 | return 0; |
| 675 | } |
| 676 | |
| 677 | // send |
| 678 | msg.type = CK_MSG_DONE; |
| 679 | otf_hton( &msg ); |
| 680 | // log |
| 681 | EM_log( CK_LOG_INFO, "otf sending request..." ); |
| 682 | // send done message |
| 683 | ck_send( dest, (char *)&msg, sizeof(msg) ); |
| 684 | |
| 685 | // set timeout |
| 686 | ck_recv_timeout( dest, 0, 2000000 ); |
| 687 | // log |
| 688 | EM_log( CK_LOG_INFO, "otf awaiting reply..." ); |
| 689 | // reply |
| 690 | if( ck_recv( dest, (char *)&msg, sizeof(msg) ) ) |
| 691 | { |
| 692 | // network to host byte ordering |
| 693 | otf_ntoh( &msg ); |
| 694 | if( !msg.param ) |
| 695 | { |
| 696 | EM_error2( 0, "REMOTE operation failed..." ); |
| 697 | EM_error2( 0, "reason: %s", |
| 698 | ( strstr( (char *)msg.buffer, ":" ) ? strstr( (char *)msg.buffer, ":" ) + 1 : (char *)msg.buffer ) ); |
| 699 | } |
| 700 | else |
| 701 | { |
| 702 | EM_log( CK_LOG_INFO, "reply received..." ); |
| 703 | } |
| 704 | } |
| 705 | else |
| 706 | { |
| 707 | EM_error2( 0, "remote operation timed out..." ); |
| 708 | } |
| 709 | // close the sock |
| 710 | ck_close( dest ); |
| 711 | |
| 712 | // exit |
| 713 | // exit( msg.param ); |
| 714 | |
| 715 | return 1; |
| 716 | |
| 717 | error: |
| 718 | |
| 719 | // if sock was opened |
| 720 | if( dest ) |
| 721 | { |
| 722 | msg.type = CK_MSG_DONE; |
| 723 | otf_hton( &msg ); |
| 724 | ck_send( dest, (char *)&msg, sizeof(msg) ); |
| 725 | ck_close( dest ); |
| 726 | } |
| 727 | |
| 728 | // exit( 1 ); |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | //----------------------------------------------------------------------------- |
| 737 | // name: otf_recv_cb() |
| 738 | // desc: OTF server thread for receiving OTF message over network |
| 739 | //----------------------------------------------------------------------------- |
| 740 | void * otf_recv_cb( void * p ) |
| 741 | { |
| 742 | OTF_Net_Msg msg; |
| 743 | OTF_Net_Msg ret; |
| 744 | ck_socket client; |
| 745 | int n; |
| 746 | |
| 747 | // REFACTOR-2017: get per-VM carrier |
| 748 | Chuck_Carrier * carrier = (Chuck_Carrier *)p; |
| 749 | |
| 750 | // catch SIGINT |
| 751 | // signal( SIGINT, signal_int ); |
| 752 | #ifndef __PLATFORM_WINDOWS__ |
| 753 | // catch SIGPIPE |
| 754 | // REFACTOR 2017: TODO register global signal_pipe function |
| 755 | // signal( SIGPIPE, signal_pipe ); |
| 756 | #endif |
| 757 | |
| 758 | while( true ) |
| 759 | { |
| 760 | // REFACTOR-2017: change g_sock to per-VM socket |
| 761 | client = ck_accept( carrier->otf_socket ); |
| 762 | |
| 763 | // check for thread shutdown, potentially occurred during ck_accept |
| 764 | if( !carrier->otf_thread ) |
| 765 | break; |
| 766 | |
| 767 | if( !client ) |
| 768 | { |
| 769 | if( carrier->vm ) |
| 770 | EM_log( CK_LOG_INFO, "[chuck]: socket error during accept()..." ); |
| 771 | ck_usleep( 40000 ); |
| 772 | // ck_close( client ); don't close NULL client |
| 773 | continue; |
| 774 | } |
| 775 | msg.clear(); |
| 776 | // set time out |
| 777 | ck_recv_timeout( client, 0, 5000000 ); |
| 778 | // receive packet |
| 779 | n = ck_recv( client, (char *)&msg, sizeof(msg) ); |
| 780 | // network to host byte ordering |
| 781 | otf_ntoh( &msg ); |
| 782 | // check |
| 783 | if( n != sizeof(msg) ) |
| 784 | { |
| 785 | EM_error2( 0, "0-length packet..." ); |
| 786 | ck_usleep( 40000 ); |
| 787 | ck_close( client ); |
| 788 | continue; |
| 789 | } |
| 790 | |
| 791 | // check header |
| 792 | if( msg.header != CK_NET_HEADER ) |
| 793 | { |
| 794 | EM_error2( 0, "header discrepancy: possible client/server version mismatch..." ); |
| 795 | ck_close( client ); |
| 796 | continue; |
| 797 | } |
| 798 | |
| 799 | // while not done |
| 800 | while( msg.type != CK_MSG_DONE ) |
| 801 | { |
| 802 | if( carrier->vm ) |
| 803 | { |
| 804 | // process incoming OTF message |
| 805 | if( !otf_process_msg( carrier->vm, carrier->compiler, &msg, FALSE, client ) ) |
| 806 | { |
| 807 | // problem |
| 808 | ret.param = FALSE; |
| 809 | // error message |
| 810 | strcpy( (char *)ret.buffer, EM_lasterror() ); |
| 811 | // drain network data |
| 812 | while( msg.type != CK_MSG_DONE && n ) |
| 813 | { |
| 814 | // get next packet |
| 815 | n = ck_recv( client, (char *)&msg, sizeof(msg) ); |
| 816 | // network to host byte ordering (not that it matters here; just draining) |
| 817 | otf_ntoh( &msg ); |
| 818 | } |
| 819 | break; |
| 820 | } |
| 821 | else |
| 822 | { |
| 823 | // made it |
| 824 | ret.param = TRUE; |
| 825 | // success code |
| 826 | strcpy( (char *)ret.buffer, "success" ); |
| 827 | // receive next packet (done msg) |
| 828 | n = ck_recv( client, (char *)&msg, sizeof(msg) ); |
| 829 | // network to host byte ordering |
| 830 | otf_ntoh( &msg ); |
| 831 | } |
| 832 | } |
| 833 | else |
| 834 | { |
| 835 | // wait if no VM on the carrier |
| 836 | ck_usleep( 10000 ); |
| 837 | } |
| 838 | } |
| 839 | |
| 840 | // host to network byte ordering |
| 841 | otf_hton( &ret ); |
| 842 | // send return acknoledgement |
| 843 | ck_send( client, (char *)&ret, sizeof(ret) ); |
| 844 | // close socket to client |
| 845 | ck_close( client ); |
| 846 | } |
| 847 | |
| 848 | // reach here only if thread shutdown |
| 849 | return NULL; |
| 850 | } |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | // we got yer errors, yer problems, yer worst nightmares right here folks |
| 856 | const char * poop[] = { |
| 857 | "[chuck]: bus error...", |
| 858 | "[chuck]: segmentation fault, core dumped...", |
| 859 | "[chuck]: access violation, NULL pointer...", |
| 860 | "[chuck]: unhandled exception in chuck: 0xC0000005: access violation", |
| 861 | "[chuck]: malloc: damage after normal block...", |
| 862 | "[chuck]: virtual machine panic!!!", |
| 863 | "[chuck]: confused by earlier errors, bailing out...", |
| 864 | "[chuck]: lack of destructors have led to the unrecoverable mass build-up of trash\n" |
| 865 | " the chuck garbage collector will now run, deleting all files (bye.)", |
| 866 | "[chuck]: calling Machine.crash()...", |
| 867 | "[chuck]: an unknown fatal error has occurred. please restart your computer...", |
| 868 | "[chuck]: an unknown fatal error has occurred. please reinstall something...", |
| 869 | "[chuck]: an unknown fatal error has occurred. please update to chuck-3.0", |
| 870 | "[chuck]: (internal error) unknown error", |
| 871 | "[chuck]: (internal error) too many errors!!!", |
| 872 | "[chuck]: error printing error message. cannot continue 2#%%HGAFf9a0x" |
| 873 | }; |
| 874 | // poop size |
| 875 | long poop_size = sizeof( poop ) / sizeof( char * ); |
| 876 | |
| 877 | |
| 878 | |
| 879 | |
| 880 | //----------------------------------------------------------------------------- |
| 881 | // name: uh() |
| 882 | // desc: ... |
| 883 | //----------------------------------------------------------------------------- |
| 884 | void uh( ) |
| 885 | { |
| 886 | ck_srandom( (unsigned)time( NULL ) ); |
| 887 | while( true ) |
| 888 | { |
| 889 | int n = (int)(ck_random() / (float)CK_RANDOM_MAX * poop_size); |
| 890 | printf( "%s\n", poop[n] ); |
| 891 | ck_usleep( (unsigned int)(ck_random() / (float)CK_RANDOM_MAX * 2000000) / 10 ); |
| 892 | } |
| 893 | } |