Example #1
0
void Xmpp_client::file_received (QXmppTransferJob *job)
{
    qDebug() << "Xmpp_client::file_received";
    qDebug() << "Got transfer request from:" << job->jid();


    bool check = connect(job, SIGNAL(error(QXmppTransferJob::Error)), this, SLOT(job_error(QXmppTransferJob::Error)));
    Q_ASSERT(check);

    check = connect(job, SIGNAL(finished()), this, SLOT(job_finished()));
    Q_ASSERT(check);

    check = connect(job, SIGNAL(progress(qint64,qint64)), this, SLOT(job_progress(qint64,qint64)));
    Q_ASSERT(check);



    // allocate a buffer to receive the file
    m_buffer = new QBuffer(this);
    m_buffer->open(QIODevice::WriteOnly);
    job->accept(m_buffer);
}
// handler for all of the signals (SIGCHLD, SIGINT, SIGTSTP)
void child_signal_handler(int signum, siginfo_t* siginfo, void* extra){

  if(signum == SIGCHLD){
    int status, ch_pid;
    ch_pid = waitpid(-1, &status, WNOHANG | WUNTRACED | WCONTINUED);

    if(ch_pid > 0){

      if(ch_pid == pipe_pid){
        pipe_pid = 0;
      }
      //find which subjob(process) and which job this child process belongs to.
      job* this_job = find_job_pid(ch_pid, jlist);
      subjob* this_subjob = find_subjob(this_job, ch_pid);

      //handle each case of status change
      if(WIFEXITED(status)){
        this_subjob->finished = 1; // if subjob exited normally

        if(job_finished(this_job)){

          if(ch_pid == fg_pid){
            fg_pid = 0; // if child process exited is a foreground job, reset

            if(tcsetpgrp(STDIN_FILENO, getpgid(0)) == -1){
              perror("tcsetpgrp error 1");
            }
          }
          else{ //if ch_pid was a background job
            message_q = add(message_q, "\n");
            message_q = add(message_q, this_job->command);
            message_q = add(message_q, "Finished: ");
          }
          delete_job(this_job, jlist);
        }
      }
      else if(WIFSIGNALED(status)){

        if(ch_pid == fg_pid){
          fg_pid = 0;

          if(tcsetpgrp(STDIN_FILENO, getpgid(0)) == -1){
            perror("tcsetpgrp error");
          }
        }
        delete_job(this_job, jlist); //delete corresponding job
      }
      else if(WIFSTOPPED(status)){

        if(ch_pid == fg_pid){
          fg_pid = 0;

          if(tcsetpgrp(STDIN_FILENO, getpgid(ch_pid)) == -1){
            perror("tcsetpgrp error 3");
          }
        }
        this_subjob->stopped = 1; // set the stopped boolean to TRUE

        if(job_stopped(this_job)){
          message_q = add(message_q, "\n"); // add stopped prompt to message q
          message_q = add(message_q, this_job->command);
          message_q = add(message_q, "Stopped: ");
          update_orders(jlist, this_job); // update ranking of most recent jobs 
        }
      }

      else if(WIFCONTINUED(status)){//restart the stopped process
        this_subjob->stopped = 0;

        if(!job_stopped(this_job)){
         update_orders(jlist, this_job); // update order of jobs in job_list
        }

        if(fg_pid){
          if(tcsetpgrp(STDIN_FILENO, fg_pid) == -1){
            perror("tcsetpgrp error 5");
          }
        }
        else{

        }

      }
      ch_pid = waitpid(-1, &status, WNOHANG | WUNTRACED | WCONTINUED); // wait
    }
  }
  if(signum == SIGINT){
    //ctrl-C
    //if the signal is coming from the main shell, exit the shell.
    if(sh_pgid == getpgid(siginfo->si_pid)){

      if(fg_pid){
        killpg(getpgid(fg_pid), SIGINT); // kill fg process with SIGINT
      }
    }
    else{
      killpg(0, SIGTERM);
    }
  }

  if(signum == SIGTSTP){

    if(sh_pgid == getpgid(siginfo->si_pid)){

      if(fg_pid){
        killpg(getpgid(fg_pid), SIGTSTP); // kill fg process with SIGTSTP
      }
    }
    else{
      killpg(0, SIGSTOP);
    } 
  }
}