Exemplo n.º 1
0
void UpdateJobs(pid_t pid, int state)
{
  jobNode* job;
  job = FindJob(pid, TRUE);
  
 // printf("%d\n", job->state);
  //printf("%s\n", job->cmdline);
  
  if (job != NULL)
  {
    if (state == DONE && job->state != BACKGROUND)
    {
  job->printedJob = TRUE;
    }
    job->state = state;
    if (state == STOPPED)
    {
      if (job->jid == 0)
      {
        job->jid = nextJobId;
        nextJobId += 1;
      }
      printf("[%d]   Stopped                 %s\n", job->jid, job->cmdline);
      //NEED TO PRINT OUT THAT THE JOB WAS STOPPED
      fflush(stdout);
    }
   // printf("%d\n", state);
  }
}
Exemplo n.º 2
0
void
JobListView::UpdateJob(Job* job)
{
	JobItem* item = FindJob(job);
	if (item) {
		item->Update();
		InvalidateItem(IndexOf(item));
	}
}
Exemplo n.º 3
0
void
JobListView::RemoveJob(Job* job)
{
	JobItem* item = FindJob(job);
	if (item) {
		RemoveItem(item);
		delete item;
		Invalidate();
	}
}
Exemplo n.º 4
0
void WaitFg(pid_t pid)
{
    jobNode* job = FindJob(pid,TRUE);
    //while job still in the foreground
    while (job->state==FOREGROUND && job!=NULL)
    {
        //not killed at some point 
            //wait 1 second for job to finish
            sleep(1);
    }
}
Exemplo n.º 5
0
static void RunBuiltInCmd(commandT* cmd)
{
        //running built-in command
        //int i=0;
        //check if it's exit
        if (strcmp(cmd->argv[0],"exit")==0){
                return;
        }


        //check for echo
  /*
        if (strcmp(cmd->argv[0],"echo")==0){
                for (i=1;i<cmd->argc;i++){
                        //check for environment var
                        if (cmd->argv[i][0]=="$"){
                                //get the 1st through n elements of this argument
                                char* envVar = malloc(strlen((cmd->argv[i]))*sizeof(char));
                                memcpy(envVar,cmd->argv[i]+sizeof(char),sizeof(char)*strlen(cmd->argv[i]));
                                printf("%s ",getenv(envVar));
                        }
                        else{
                                printf("%s ",cmd->argv[i]);
                        }
                }
                printf("\n");
        }
  */
  if (strcmp(cmd->argv[0], "jobs")==0){
    PrintJobs();
  }
  
  if (strcmp(cmd->argv[0], "fg")==0){
      //bring process to foreground
      //the jid should have been given in the command so in argv[1]
    
    if (cmd->argv[1]==NULL){
      //no job id given
      printf("No job id \n");
    }
    else {
      //get the job id as an integer
      pid_t jid = atoi(cmd->argv[1]);
      //find job
      jobNode* jobTofg = FindJob(jid,FALSE);
      //now change state to foreground
      if (jobTofg==NULL){
        printf("Job does not exist \n");
        //job does not exist
        return;
      }
      else {
        if (jobTofg->state==STOPPED || jobTofg->state==BACKGROUND){
          //continue if it wasn't already there
          kill(-(jobTofg->pid),SIGCONT);
        }

        //and bring state to FOREGROUND
        jobTofg->state = FOREGROUND;
//        printf("Job %d brought to foreground: %s \n",jobTofg->jid,jobTofg->cmdline);
        //now wait for it to finish
        fgpid = jobTofg->pid;
        WaitFg(jobTofg->pid);
        
      }
      
    }
  }

  if (strcmp(cmd->argv[0], "bg") == 0)
  {
    if (cmd->argv[1] == NULL)
    {
      //no job id given
      printf("No job id \n");
    }
    else
    {
      //get the job id as an integer
      pid_t jid = atoi(cmd->argv[1]);
      //find the job in the list
      jobNode* jobToBg = FindJob(jid, FALSE);
      if (jobToBg == NULL)
      {
        printf("Job does not exist \n");
        return;
      }
      else
      {
  if (jobToBg->state == STOPPED || jobToBg->state == BACKGROUND)
  {
    kill(-(jobToBg->pid), SIGCONT);
  }
        jobToBg->state = BACKGROUND; 
      }
    }
  }
  
  if (strcmp(cmd->argv[0], "cd") == 0)
  {
    if (cmd->argv[1] == NULL)
    {
      if (chdir(getenv("HOME")) != 0)
      {
        printf("error in cd\n");
      }
    }
    else
    {
      if (chdir(cmd->argv[1]) != 0)
      {
        printf("error in cd\n");
      }
    }
  }
  
  if (strcmp(cmd->argv[0], "alias") == 0)
  {
    
    if (cmd->argv[1]== NULL)
    {
      aliasNode* current = aliasHead;
      
      while (current != NULL)
      {
        printf("alias %s=\'%s\'\n", current->name, current->cmdLine);
        current = current->next;
      }
    }
    
    else
    {
      
      //add the command specified to the alias list
      aliasNode* current = aliasHead;
    
      //PARSING FOR QUOTATIONS
      int i=0;
      int firstQuoteIndex = 0;
      //int secondQuoteIndex = 0;
      char quotes = 39;
      // int sizeArgv1 = sizeof(cmd->argv[1])*sizeof(char*);
    
      //char* firstIndx;
      char* secondIndx;
      
      secondIndx = strtok(cmd->cmdline,"'");
      secondIndx = strtok(NULL,"'");
      
      //printf("Command is %s",secondIndx);

      while (cmd->argv[1][i]!=quotes){
        //printf("%c %c \n", cmd->argv[1][i], quotes);
        if (cmd->argv[1][i]==0){
          printf("Invalid command\n");
          return;
        }
        i++;
      }
      firstQuoteIndex = i;
/*
      i++;
      
      while (cmd->argv[1][i]!=quotes){
        //printf("%c %c \n", cmd->argv[1][i], quotes);
        if (cmd->argv[1][i]==0){
          printf("Invalid command \n");
          return;
        }
        i++;
      }
      secondQuoteIndex = i;
*/

      //char* commandLine = malloc(sizeof(char) * (secondQuoteIndex - firstQuoteIndex));
      char* commandName = malloc(sizeof(char) * (firstQuoteIndex-1));
    
    	strncpy(commandName, cmd->argv[1], firstQuoteIndex-1);
      //strncpy(commandLine, cmd->argv[1]+(sizeof(char)*firstQuoteIndex), secondQuoteIndex-firstQuoteIndex+1);
    
      //come up with command as variable with the command name
      //come up with commandLine as variable with the command line entry
    
      aliasHead = malloc(sizeof(aliasNode));
      aliasHead->next = current;
      aliasHead->name = commandName;
      aliasHead->cmdLine = secondIndx;
    }
    
  }



  if (strcmp(cmd->argv[0],"unalias") == 0)
  {
    //remove alias from list of aliases
    char* aliasName = cmd->argv[1];
    if (aliasName==NULL){
	printf("Specify command\n");
	return;
    }
    aliasNode* current = aliasHead;
    aliasNode* previous = NULL;
    while (current != NULL)
    {
      if (strcmp(aliasName,current->name)==0){
      
          //if it's at the head of the list
          if (previous==NULL){
              //then just set the head to the next one
              aliasHead = current->next;
              free(current);
          }
          
          //otherwise
          else {
              //point the previous to the next one
              previous->next = current->next;
              free(current);
          }
      }
      previous = current;
      current = current->next;
    }
  }
  
}
Exemplo n.º 6
0
DWORD
CheckPrinterJobToken(
    LPCWSTR string,
    LPCWSTR pSecondPart,
    PDWORD pTypeofHandle,
    PINIPRINTER* ppIniPrinter,
    PINIJOB* ppIniJob,
    PHANDLE phReadFile,
    const PINISPOOLER pIniSpooler
    )
{
    HANDLE  hImpersonationToken;
    DWORD Position;
    DWORD JobId;
    PINIPRINTER pIniPrinter;
    PINIJOB pIniJob, pCurrentIniJob;

    if( wcsncmp( pSecondPart, L"Job ", 4 ) != STRINGS_ARE_EQUAL ||
        !( pIniPrinter = FindPrinter( string ))){

        return ROUTER_UNKNOWN;
    }

    //
    //  Get the Job ID ",Job xxxx"
    //
    pSecondPart += 4;

    JobId = Myatol( (LPWSTR)pSecondPart );

    pIniJob = FindJob( pIniPrinter, JobId, &Position );

    if( pIniJob == NULL ) {

        DBGMSG( DBG_WARN, ("OpenPrinter failed to find Job %d\n", JobId ));
        return ROUTER_UNKNOWN;
    }

    DBGMSG( DBG_TRACE, ("OpenPrinter: pIniJob->cRef = %d\n", pIniJob->cRef));

    if( pIniJob->Status & JOB_DIRECT ) {

        SplInSem();
        INCJOBREF( pIniJob );

        *pTypeofHandle |= PRINTER_HANDLE_JOB | PRINTER_HANDLE_DIRECT;
        goto Success;
    }

    //
    //  If this job is assigned to a port
    //  Then pick up the correct chained jobid file instead of the master
    //  JobId.
    //


    if ( pIniJob->pCurrentIniJob != NULL ) {

        SPLASSERT( pIniJob->pCurrentIniJob->signature == IJ_SIGNATURE );

        DBGMSG( DBG_TRACE,("CheckPrinterJobToken pIniJob %x JobId %d using chain JobId %d\n",
                pIniJob, pIniJob->JobId, pIniJob->pCurrentIniJob->JobId ));


        pCurrentIniJob = pIniJob->pCurrentIniJob;


        SPLASSERT( pCurrentIniJob->signature == IJ_SIGNATURE );

    } else {

        pCurrentIniJob = pIniJob;

    }


    GetFullNameFromId( pCurrentIniJob->pIniPrinter,
                       pCurrentIniJob->JobId,
                       TRUE,
                       (LPWSTR)string,
                       FALSE );


    //  !! BUGBUG !!
    //  Even a user without previledge can open a ", JOB #"
    //  if he is physically running on the machine.


    hImpersonationToken = RevertToPrinterSelf();

    *phReadFile = CreateFile( string,
                  GENERIC_READ,
                  FILE_SHARE_READ |
                  FILE_SHARE_WRITE,
                  NULL,
                  OPEN_EXISTING,
                  FILE_ATTRIBUTE_NORMAL |
                  FILE_FLAG_SEQUENTIAL_SCAN,
                  NULL );

    ImpersonatePrinterClient( hImpersonationToken );

    if( *phReadFile != INVALID_HANDLE_VALUE ) {

    DBGMSG( DBG_TRACE, ("OpenPrinter JobID %d pIniJob %x CreateFile( %ws ), hReadFile %x success", JobId, pIniJob, string, *phReadFile ));

        SplInSem();
    INCJOBREF( pIniJob );

        *pTypeofHandle |= PRINTER_HANDLE_JOB;
        goto Success;
    }

    DBGMSG( DBG_WARNING, ("LocalOpenPrinter CreateFile(%ws) GENERIC_READ failed : %d\n", string, GetLastError()));
    SPLASSERT( GetLastError( ));

    return ROUTER_STOP_ROUTING;

Success:

    *ppIniJob = pIniJob;
    *ppIniPrinter = pIniPrinter;
    return ROUTER_SUCCESS;
}