@@ -333,7 +333,7 @@ int main(int argc, char* argv[])
333
333
close (clients[i]->socket );
334
334
delete clients[i];
335
335
clients.erase (clients.begin ()+i);
336
- fprintf (stderr, MSG_START " done closing client from main process." );
336
+ fprintf (stderr, MSG_START " done closing client from main process.\n " );
337
337
}
338
338
}
339
339
else { if (clients[i]->error ) print_client (clients[i], " pipe okay again." ); clients[i]->error =0 ; }
@@ -347,16 +347,24 @@ int main(int argc, char* argv[])
347
347
348
348
pid_t run_subprocess (char * cmd, int * pipe_in, int * pipe_out, pid_t * pgrp)
349
349
{
350
+ /* char sem_name[101];
351
+ snprintf(sem_name,100,"ddcd_sem_%d",getpid());
352
+ sem_t mysem;
353
+ if(sem_init(&mysem, 1, 1)==-1) error_exit("failed to sem_init() in run_subprocess()");
354
+ fprintf(stderr, "sem_waiting\n");
355
+ if(sem_wait(&mysem)==-1) error_exit("the first sem_wait() failed in run_subprocess()");
356
+ fprintf(stderr, "sem_waited\n");
357
+ */
358
+ int syncpipe[2 ];
359
+ if (pipe (syncpipe)==-1 ) error_exit (" failed to create pipe()" );
350
360
pid_t pid = fork ();
351
- if (*pgrp>=0 )
352
- {
353
- setpgrp ();
354
- *pgrp = getpgrp ();
355
- }
356
- // fprintf(stderr, "run_subprocess :: fork-ed %d\n", pid);
361
+
357
362
if (pid < 0 ) return 0 ; // fork failed
358
363
if (pid == 0 )
359
364
{
365
+ setpgrp ();
366
+ write (syncpipe[1 ], " " , 1 );
367
+ // if(sem_post(&mysem)==-1) error_exit("failed to sem_post() in run_subprocess()");
360
368
// We're the subprocess
361
369
// fprintf(stderr, "run_subprocess :: execl\n");
362
370
// if(fcntl(pipe_in[1], F_SETPIPE_SZ, pipe_max_size) == -1) perror("Failed to F_SETPIPE_SZ in run_subprocess()");
@@ -373,19 +381,22 @@ pid_t run_subprocess(char* cmd, int* pipe_in, int* pipe_out, pid_t* pgrp)
373
381
execl (" /bin/bash" ," bash" ," -c" ,cmd, (char *)0 );
374
382
error_exit (MSG_START " run_subprocess failed to execute command" );
375
383
}
376
- else return pid;
384
+ else
385
+ {
386
+ // if(sem_wait(&mysem)==-1) error_exit("the second sem_wait() failed in run_subprocess()");
387
+ int synctemp;
388
+ read (syncpipe[0 ], &synctemp, 1 );
389
+ *pgrp = getpgid (pid);
390
+ fprintf (stderr, MSG_START " run_subprocess pgid returned = %d\n " , *pgrp);
391
+ return pid;
392
+ }
377
393
}
378
394
379
395
void print_client (client_t * client, const char * what)
380
396
{
381
397
fprintf (stderr,MSG_START " (client %s:%d) %s\n " , inet_ntoa (client->addr .sin_addr ), client->addr .sin_port , what);
382
398
}
383
399
384
- void client_cleanup ()
385
- {
386
- close (this_client->pipefd [0 ]);
387
- }
388
-
389
400
#define CTL_BUFSIZE 1024
390
401
391
402
int read_socket_ctl (int fd, char * output, int max_size)
@@ -397,7 +408,7 @@ int read_socket_ctl(int fd, char* output, int max_size)
397
408
if (buffer_index==CTL_BUFSIZE) buffer_index=0 ;
398
409
int bytes_read=recv (fd,buffer+buffer_index,(CTL_BUFSIZE-buffer_index)*sizeof (char ), MSG_DONTWAIT);
399
410
if (bytes_read<=0 ) return 0 ;
400
- fprintf (stderr, " recv %d\n " , bytes_read);
411
+ // fprintf(stderr, "recv %d\n", bytes_read);
401
412
402
413
int prev_newline_at=0 ;
403
414
int last_newline_at=0 ;
@@ -425,6 +436,26 @@ int read_socket_ctl(int fd, char* output, int max_size)
425
436
}
426
437
}
427
438
439
+ int ctl_get_arg (char * input, const char * cmd, const char * format, ...)
440
+ {
441
+ int retval=0 ;
442
+ int cmdlen=strlen (cmd);
443
+ if (input[cmdlen]==' =' )
444
+ {
445
+ // fprintf(stderr, "cga found=\n");
446
+ if (input[cmdlen]=0 , !strcmp (input,cmd))
447
+ {
448
+ // fprintf(stderr, "cga foundokay\n");
449
+ va_list vl;
450
+ va_start (vl,format);
451
+ retval=vsscanf (input+cmdlen+1 ,format,vl);
452
+ va_end (vl);
453
+ }
454
+ input[cmdlen]=' =' ;
455
+ }
456
+ // fprintf(stderr, "cga retval %d\n", retval);
457
+ return retval;
458
+ }
428
459
429
460
void client ()
430
461
{
@@ -433,12 +464,13 @@ void client()
433
464
434
465
char client_subprocess_cmd_buf[500 ];
435
466
int input_fd = this_client->pipefd [0 ];
467
+ int pipe_ctl[2 ], pipe_stdout[2 ];
436
468
437
469
prctl (PR_SET_PDEATHSIG, SIGHUP); // get a signal when parent exits
438
470
439
471
if (decimation!=1 )
440
472
{
441
- int pipe_ctl[ 2 ], pipe_stdout[ 2 ];
473
+
442
474
if (pipe (pipe_ctl)==-1 ) error_exit (MSG_START " cannot open new pipe() for the client subprocess" );
443
475
if (pipe (pipe_stdout)==-1 ) error_exit (MSG_START " cannot open new pipe() for the client subprocess" );
444
476
switch (ddc_method)
@@ -459,14 +491,45 @@ void client()
459
491
write (pipe_ctl[1 ], " 0.0\n " , 4 );
460
492
}
461
493
char recv_cmd[CTL_BUFSIZE];
494
+ char temps[CTL_BUFSIZE*2 ];
495
+ int tempi;
496
+ float tempf;
497
+
462
498
for (;;)
463
499
{
464
500
while (read_socket_ctl (this_client->socket , recv_cmd, CTL_BUFSIZE))
465
- fprintf (stderr, " read_socket_ctl: %s\n " , recv_cmd);
466
- read (input_fd,buf,bufsizeall);
467
- if (send (this_client->socket ,buf,bufsizeall,0 )==-1 )
468
501
{
469
- print_client (this_client, " client process is exiting." );
502
+ sprintf (temps, " read_socket_ctl: %s" , recv_cmd);
503
+ print_client (this_client, temps);
504
+ if (ctl_get_arg (recv_cmd, " bypass" , " %d" , &tempi))
505
+ {
506
+ if (tempi==1 && client_subprocess_pid)
507
+ {
508
+ // print_client(this_client, "suspending client_subprocess_pgrp...\n");
509
+ // fprintf(stderr, "client_subprocess_pgrp = %d\n", client_subprocess_pgrp);
510
+ // killpg(client_subprocess_pgrp, SIGTSTP);
511
+ // while(proc_exists(client_subprocess_pid)) usleep(10000);
512
+ // print_client(this_client, "done killing client_subprocess_pid.\n");
513
+ input_fd=this_client->pipefd [0 ]; // by doing this, we don't read from pipe_stdout[0] anymore, so that csdr stops doing anything, and also doesn't read anymore from the input: we get the whole I/Q stream!
514
+ }
515
+ if (tempi==0 && client_subprocess_pid)
516
+ {
517
+ input_fd=pipe_stdout[0 ];
518
+ }
519
+
520
+ }
521
+ if (ctl_get_arg (recv_cmd, " shift" , " %g" , &tempf))
522
+ {
523
+ tempi=sprintf (temps, " %g\n " , tempf);
524
+ write (pipe_ctl[1 ], temps, tempi);
525
+ fsync (pipe_ctl[1 ]);
526
+ }
527
+ }
528
+ int nread = read (input_fd,buf,bufsizeall);
529
+ if (nread<=0 ) continue ;
530
+ if (send (this_client->socket ,buf,nread,0 )==-1 )
531
+ {
532
+ print_client (this_client, " client process is exiting.\n " );
470
533
if (client_subprocess_pid) killpg2 (client_subprocess_pgrp);
471
534
exit (0 );
472
535
}
@@ -475,6 +538,7 @@ void client()
475
538
476
539
void killpg2 (pid_t pgrp)
477
540
{
541
+ // fprintf(stderr, MSG_START "killpg2: %d\n", pgrp);
478
542
if (pgrp!=1 && pgrp!=0 ) killpg (pgrp, SIGTERM);
479
543
}
480
544
0 commit comments