示例#1
0
void makeCdec()
{
  double  Cppm = getval("dfrq"),                 /*  pre-set C decoupling parameters */        
          cbw = 250.0*Cppm,                       /* C-decoupling bandwidth, 250 ppm */
          cpw = 0.006,                              /* C-decoupling pulsewidth, 6 ms */
	  pwC = getval("pwC"),
	  pwClvl = getval("pwClvl"),
          compC = getval("compC"),
          Cdecpwr;
  char    cmd[MAXSTR];

            /* create low power decoupling for long range J(CH) and do it only once */
            
    if((getval("arraydim") < 1.5) || (ix==1))  
    {
      sprintf(cmd, "Pbox Cdec_lp -w \"WURST-40 %.2f/%.6f\" -s 4.0 -p %.0f -l %.2f -0",
                    cbw, cpw, pwClvl, 1.0e6*pwC*compC);
      system(cmd);
      Cdseq = getDsh("Cdec_lp");
      Cdseq.pwr = Cdseq.pwr + 1.0;                   /* optional 1 dB power increase */
    }   
    if ((find("Cdecpwr") == 4) && (Cdecpwr = getval("Cdecpwr")))
      Cdseq.pwr = Cdecpwr;              /* if requested, set to user defined power */
    setlimit("Cdseq.pwr", Cdseq.pwr, 45.0);
}
示例#2
0
void makeHHdec()
{
  double  ppm = getval("sfrq"),                  /* pre-set HH decoupling parameters */        
          hhbw = 3.0*ppm,                               /* h**o-decoupling bandwidth */
          hhpw = 0.020,                                /* h**o-decoupling pulsewidth */
          hhofs = -0.25*ppm,                      /* h**o-decoupling offset from H2O */
          hhstp = 1e6/getval("sw"),                /* h**o-decoupling shape stepsize */
          compH = getval("compH"),
          Hdecpwr;
  char    cmd[MAXSTR];
      
            /* create shape for HH h**o-decoupling and do it only once */
                        
    if((getval("arraydim") < 1.5) || (ix==1))  
    {
      sprintf(cmd, "Pbox hhdec -w \"CAWURST-8 %.2f/%.6f %.2f\" -s %.2f ",
                    hhbw, hhpw, hhofs, hhstp);
      sprintf(cmd, "%s -dcyc 0.05 -p %.0f -l %.2f -0\n", 
                    cmd, tpwr, 1.0e6*pw*compH);
      system(cmd);
      HHdseq = getDsh("hhdec"); 
    }   
    if ((find("Hdecpwr") == 4) && (Hdecpwr = getval("Hdecpwr")))
      HHdseq.pwr = Hdecpwr;              /* if requested, set to user defined power */
    setlimit("HHdseq.pwr", HHdseq.pwr, 51.0);
}
示例#3
0
int main (int argc, char **argv, char **envp)
{
  struct rusage usage;
  char **p;
  int status, mem;
  int tsource, ttarget;
  int v;

  redirect = stderr;
  safe_signal (SIGPIPE, SIG_DFL);

  tsource = time (NULL);
  p = parse (argv);
  if (p == NULL) {
    printusage (argv);
    return (EXIT_FAILURE);
  }

  if (profile.env_vars != NULL) {
    if (strcmp(profile.env_vars, "PY") == 0) { 
      /* shorthand for needed Python environment variables, assuming that
	 the necessary executables, libraries, etc. are in the root of jail */
      char* xnewenvp[] = { "PYTHONIOENCODING=utf_8", "PYTHONHOME=/", "PYTHONPATH=/static", NULL };
      envp = xnewenvp;
    }
    else {
      char * newenvp[100];
      char ** iter = newenvp;
      *iter = strtok (profile.env_vars,"\n");
      while (*iter != NULL) { 
	iter++;
	*iter = strtok (NULL, "\n");
      } 
      envp = newenvp;
    }
  }

  if (profile.report_file != NULL) {
    redirect = fopen (profile.report_file, "w");
    if (redirect == NULL)
      error ("Couldn't open redirection file\n");
  }

  if (profile.customuid) {
    profile.theuid = profile.customuid;
  }
  else {
    profile.theuid = profile.uidplus + getpid();
  }

  if (profile.exec_dir == NULL && profile.chroot_dir == NULL) {
    system("chmod -R 777 ./");
  }
  else if (profile.exec_dir != NULL) {
    chdir(profile.exec_dir);
    system("chmod -R 777 ./");
  }
  else {
    chdir(profile.chroot_dir);
    system("chmod -R 777 ./");
  }

  pid = fork ();
  if (pid < 0)
    error (NULL);

  if (pid == 0) { /* FORK: slave process that will run the submitted code */

    setlimit (RLIMIT_AS, profile.memory * 1024);   /* ENOMEM error, SIGSEGV */
    setlimit (RLIMIT_DATA, profile.memory * 1024); /* ENOMEM error */
    setlimit (RLIMIT_CORE, profile.core * 1024);   /*  */
    setlimit (RLIMIT_STACK, profile.stack * 1024); /* SIGSEGV */
    setlimit (RLIMIT_FSIZE, profile.fsize * 1024); /* SIGXFSZ */
    setlimit (RLIMIT_CPU, profile.cpu);            /* SIGKILL */
    setlimit (RLIMIT_NOFILE, profile.nfile);       /* EMFILE error */
    setlimit (RLIMIT_NPROC, profile.nproc + 1);    /* 1 required by setuid */
    
    if (profile.chroot_dir != NULL && 0 != chdir(profile.chroot_dir)) {
      error ("Could not chdir to chroot dir [%s] while in [%s]\n", profile.chroot_dir, getcwd(NULL, 0));
      kill (getpid (), SIGPIPE);
    }
    
    if (profile.chroot_dir != NULL && chroot(".") != 0) {
      error ("Cannot chroot while in [%s]\n", getcwd(NULL, 0));
      kill (getpid (), SIGPIPE);
    }
    
    if (profile.exec_dir != NULL && 0 != chdir(profile.exec_dir)) {
      error ("Cannot change to rundir");
      kill (getpid (), SIGPIPE);
    }
    
    if (0 != setpriority(PRIO_PROCESS, getpid(), profile.niceness)) {
      error ("Could not set priority");
      kill (getpid (), SIGPIPE);
    }
    
    if (setgid (profile.gid) < 0 || getgid () != profile.gid || profile.gid == 0)
      error ("setgid failed\n");
    
    if (setgroups (0, NULL) < 0 || getgroups (0, NULL) != 0)
      error ("setgroups failed\n");
    
    if (setuid (profile.theuid) < 0 || getuid() != profile.theuid || profile.theuid == 0)
      error ("setuid failed\n");

    if (execve (*p, p, envp) < 0) {
      error ("execve error\n");
      kill (getpid (), SIGPIPE);
    }
    /* goodbye process! execve never returns. */
  }
  
  /* FORK: supervisor process */
    
  if (signal (SIGALRM, wallclock) == SIG_ERR)
    printstats ("Couldn't install signal handler\n");
  
  else if (alarm (profile.clock) != 0)
    printstats ("Couldn't set alarm\n");
  
  else {
    mark = OK;
    
    /* Poll at INTERVAL ms and determine the maximum *
     * memory usage,  exit when the child terminates */
    
    mem = -1;
    do {
      msleep (INTERVAL);
      mem = max (mem, memusage (pid));
      if (mem > profile.memory) {
        terminate (pid);
        mark = MLE;
        exit_code = mle_code;
      }
      do
        v = wait4 (pid, &status, WNOHANG | WUNTRACED, &usage);
      while ((v < 0) && (errno != EINTR));
      if (v < 0)
        error (NULL);
    } while (v == 0);
    
    ttarget = time (NULL);
    
    if (mark == MLE)
      printstats ("Memory Limit Exceeded\n");
    else if (mark == RTLE)
      printstats ("Time Limit Exceeded\n");
    else {
      if (WIFEXITED (status) != 0) {
        if (WEXITSTATUS (status) != 0) {
          if (mark == OK)
            mark = RETNZ;
          printstats ("Command exited with non-zero status (%d)\n",
                      WEXITSTATUS (status));
        }
        /*else
          printstats ("OK\n"); */
      }
      else {
        if (WIFSIGNALED (status) != 0) {
          /* Was killed for a TLE (or was it an OLE) */
          if (WTERMSIG (status) == SIGKILL) {
            mark = TLE;
            exit_code = tle_code;
          }
          else if (WTERMSIG (status) == SIGXFSZ) {
            mark = OLE;
            exit_code = ole_code;
          }
          else if (WTERMSIG (status) == SIGHUP) {
            mark = RF;
            exit_code = rf_code;
          }
          else if (WTERMSIG (status) == SIGPIPE) {
            mark = IE;
            exit_code = ie_code;
          }
          else {
            if (mark == OK) {
              mark = TERM;
              /*exit_code = term_code; */
              exit_code = WTERMSIG (status);
            }
            printstats ("Command terminated by signal (%d: %s)\n",
                        WTERMSIG (status),
                        name (WTERMSIG (status)));
          }
        }
        else if (WIFSTOPPED (status) != 0) {
          if (mark == OK) {
            mark = TERM;
            /* exit_code = term_code; */
            exit_code = WTERMSIG (status);
          }
          printstats ("Command terminated by signal (%d: %s)\n",
                      WSTOPSIG (status), name (WSTOPSIG (status)));
        }
        else
          printstats ("OK\n");
        
        if (mark == TLE) {
          /* Adjust the timings... although we know the child   *
           * was been killed just in the right time seeing 1.99 *
           * as TLE when the limit is 2 seconds is anoying      */
          usage.ru_utime.tv_sec = profile.cpu;
          usage.ru_utime.tv_usec = 0;
          printstats ("Time Limit Exceeded\n");
        }
        else if (mark == OLE)
          printstats ("Output Limit Exceeded\n");
        else if (mark == RTLE)
          printstats ("Time Limit Exceeded\n");
        else if (mark == RF)
          printstats ("Invalid Function\n");
        else if (mark == IE)
          printstats ("Internal Error\n");
      }
    }
    /* printstats ("elapsed time: %d seconds\n", ttarget - tsource);
    if (mem != -1) // -1: died too fast to measure
      printstats ("memory usage: %d kbytes\n", mem);
    printstats ("cpu usage: %0.3f seconds\n",
                (float) milliseconds (&usage.ru_utime) / 1000.0); */
  } 

  setuid(0);
  if (profile.exec_dir == NULL && profile.chroot_dir == NULL) {
    system("chown -R grader:grader ./");
  }
  else if (profile.exec_dir != NULL) {
    chdir(profile.exec_dir);
    system("chown -R grader:grader ./");
  }
  else {
    chdir(profile.chroot_dir);
    system("chown -R grader:grader ./");
  }
  /* Clean up any orphaned descendant processes. kill everything with the target user id.
     The only reliable way to do this seems to be kill(-1) from that user.
     We also have to use setgid to change the euid away from 0. */
  if (setgid (profile.gid) < 0 || getgid () != profile.gid || profile.gid == 0)
    printstats ("cleanup setgid failed\n");
  else if (setgroups (0, NULL) < 0 || getgroups (0, NULL) != 0)
    printstats ("cleanup setgroups failed\n");
  else if (setuid (profile.theuid) < 0 || getuid() != profile.theuid || profile.theuid == 0)
    printstats ("cleanup setuid failed\n");

  if (getuid() != 0 && getuid() == profile.theuid 
      && geteuid() != 0 && geteuid() == profile.theuid
      && getgid() != 0 && getgid() == profile.gid) {
    kill(-1, SIGKILL);
  }
  else {
    printstats ("not safe to kill: uid %d, euid %d, gid %d, target uid %d, target gid %d\n", 
                getuid(), geteuid(), getgid(), profile.theuid, profile.gid);
  }

  fclose (redirect);

  if (mark == OK)
    return (EXIT_SUCCESS);
  else
    return (exit_code);
}
示例#4
0
pulsesequence()
{
  char    mixpat[MAXSTR], pshape[MAXSTR], httype[MAXSTR], sspul[MAXSTR];
  double  rg1	= 2.0e-6,
          mix	= getval("mix"),	/* mixing time */
	  mixpwr = getval("mixpwr"),	/* mixing pwr */
	  compH  = getval("compH"),
	  gt0    = getval("gt0"),	/* gradient pulse in sspul */
	  gt2    = getval("gt2"),	/* gradient pulse preceding mixing */
	  gzlvl0 = getval("gzlvl0"),	/* gradient level for gt0 */
	  gzlvl2 = getval("gzlvl2"),	/* gradient level for gt2 */
	  gstab  = getval("gstab");	/* delay for gradient recovery */
  shape   hdx;
  void    setphases();

  getstr("sspul", sspul);
  getstr("pshape", pshape);
  getstr("httype", httype);
  getstr("mixpat", mixpat);
  setlimit("mixpwr", mixpwr, 48.0);

  (void) setphases();
  if (httype[0] == 'i')
    assign(zero,v2);

  /* MAKE PBOX SHAPES */

  if (FIRST_FID)
    hhmix = pbox_mix("HHmix", mixpat, mixpwr, pw*compH, tpwr);

  /* HADAMARD stuff */
  if(httype[0] == 'e')                           /* excitation */
    hdx = pboxHT_F1e(pshape, pw*compH, tpwr);
  else if(httype[0] == 'r')                      /* refocusing */
    hdx = pboxHT_F1r(pshape, pw*compH, tpwr);
  else if(httype[0] == 'd')                      /* DPFGSE */
  {
    hdx = pboxHT_F1r(pshape, pw*compH, tpwr);
    if (FIRST_FID)
      ref180 = hdx;
  }
  else /* httype[0] == 'i' */                    /* inversion */
    hdx = pboxHT_F1i(pshape, pw*compH, tpwr);

  if (getval("htcal1") > 0.5)          /* Optional fine power calibration */
    hdx.pwr += getval("htpwr1");

 /* THE PULSE PROGRAMM STARTS HERE */

  status(A);


    delay(5.0e-5);
    zgradpulse(gzlvl0,gt0);
    if (sspul[A] == 'y')
    {
      rgpulse(pw,zero,rof1,rof1);
      zgradpulse(gzlvl0,gt0);
    }

    pre_sat();
      
    if (getflag("wet"))
      wet4(zero,one);
      
    delay(1.0e-5);

    obspower(tpwr);
    delay(1.0e-5);

  status(B);

    if (httype[0] == 'i')           /* longitudinal encoding */
    {
      ifzero(v1);
        delay(2.0*(pw+rg1));
        zgradpulse(gzlvl2,gt2);
        delay(gstab);
      elsenz(v1);
        rgpulse(2.0*pw,v3,rg1,rg1);
        zgradpulse(gzlvl2,gt2);
        delay(gstab);
      endif(v1);

      pbox_pulse(&hdx, zero, rg1, rg1);
      zgradpulse(gzlvl2,gt2);
      delay(gstab);
    }
    else                            /* transverse encoding */
    {
      if (httype[0] == 'e')
        pbox_pulse(&hdx, oph, rg1, rg1);
      else
      {
        rgpulse(pw,oph,rg1,rg1);
        if (httype[0] == 'd')       /* DPFGSE */
        {
          zgradpulse(2.0*gzlvl2,gt2);
          delay(gstab);
          pbox_pulse(&ref180, oph, rg1, rg1);
          zgradpulse(2.0*gzlvl2,gt2);
          delay(gstab);
        }
        zgradpulse(gzlvl2,gt2);
        delay(gstab);
        pbox_pulse(&hdx, v2, rg1, rof2);
        zgradpulse(gzlvl2,gt2);
        delay(gstab - POWER_DELAY);
      }
    }

    if (mix)
      pbox_spinlock(&hhmix, mix, v2);

    if (httype[0] == 'i')
    {
      zgradpulse(0.87*gzlvl2,gt2);
      delay(gstab);
      obspower(tpwr);
      txphase(v3);
      rgpulse(pw,v3,rg1,rof2);
    }

  status(C);
}
示例#5
0
int main (int argc, char **argv, char **envp)
{
  struct rusage usage;
  char **p;
  int status, mem;
  int tsource, ttarget;
  int v;

  redirect = stderr;
  safe_signal (SIGPIPE, SIG_DFL);

  tsource = time (NULL);
  p = parse (argv);
  if (p == NULL)
    {
      printusage (argv);
      return (EXIT_FAILURE);
    }
  else
    {
      /*
         fprintf (stderr, "profile: \"%s\"\n", limit->name);
         fprintf (stderr, "  cpu=%u\n  mem=%u\n", (unsigned int) limit->cpu,
         (unsigned int) limit->memory);
         fprintf (stderr, "  core=%u\n  stack=%u\n", (unsigned int) limit->core,
         (unsigned int) limit->stack);
         fprintf (stderr, "  fsize=%u\n  nproc=%u\n", (unsigned int) limit->fsize,
         (unsigned int) limit->nproc);
         fprintf (stderr, "  minuid=%u\n  maxuid=%u\n", (unsigned int) limit->minuid,
         (unsigned int) limit->maxuid);
         fprintf (stderr, "  clock=%u\n",
         (unsigned int) limit->clock);
       */

      /* Still missing: get an unused uid from interval */
      if (profile.minuid != profile.maxuid)
        {
          srand (time (NULL) ^ getpid ());
          profile.minuid += rand () % (profile.maxuid - profile.minuid);
        }

      if (setuid (profile.minuid) < 0)
        error (NULL);

      if (strcmp (usage_file, "/dev/null") != 0)
        {
          redirect = fopen (usage_file, "w");
          chmod (usage_file, 0644);
          if (redirect == NULL)
            error ("Couldn't open redirection file\n");
        }

      if (getuid () == 0)
        error ("Not changing the uid to an unpriviledged one is a BAD ideia");

      if (signal (SIGALRM, wallclock) == SIG_ERR)
        error ("Couldn't install signal handler");

      if (alarm (profile.clock) != 0)
        error ("Couldn't set alarm");

      pid = fork ();
      if (pid < 0)
        error (NULL);
      if (pid == 0)
        {
          /* change to chroot dir */
          if (0 != chdir(chroot_dir))
            {
              kill (getpid (), SIGPIPE);
              error ("Cannot change to chroot dir");
            }

          /* chroot to judge dir  */
          if (0 != chroot(chroot_dir))
            {
              kill (getpid (), SIGPIPE);
              error ("Cannot chroot");
            }
          /* change to run dir */
          if (0 != chdir(run_dir))
            {
              kill (getpid (), SIGPIPE);
              error ("Cannot change to rundir");
            }

          if (setuid (profile.minuid) < 0)
            error (NULL);

          if (getuid () == 0)
            error ("Not changing the uid to an unpriviledged one is a BAD ideia");

          /* set priority */
          if (0 != setpriority(PRIO_USER,profile.minuid,NICE_LEVEL))
            {
              kill (getpid (), SIGPIPE);
              error (NULL);
            }

          /* Set Address space limit, 1 mbyte tolerancy (librarys also count!) */
          /*setlimit (RLIMIT_AS, (1024 + limit->memory) * 1024); */
          setlimit (RLIMIT_CORE, profile.core * 1024);
          setlimit (RLIMIT_STACK, profile.stack * 1024);
          setlimit (RLIMIT_FSIZE, profile.fsize * 1024);
          setlimit (RLIMIT_NPROC, profile.nproc);
          setlimit (RLIMIT_CPU, profile.cpu);

          /* Execute the program */
          if (execve (*p, p, envp) < 0)
            {
              kill (getpid (), SIGPIPE);
              error (NULL);
            }
        }
      else
        {
          if (setuid (profile.minuid) < 0)
            error (NULL);

          if (getuid () == 0)
            error ("Not changing the uid to an unpriviledged one is a BAD ideia");

          mark = OK;

          /* Poll at INTERVAL ms and determine the maximum *
           * memory usage,  exit when the child terminates */

          mem = 64;
          do
            {
              msleep (INTERVAL);
              mem = max (mem, memusage (pid));
              if (mem > profile.memory)
                {
                  terminate (pid);
                  mark = MLE;
                }
              do
                v = wait4 (pid, &status, WNOHANG | WUNTRACED, &usage);
              while ((v < 0) && (errno != EINTR));
              if (v < 0)
                error (NULL);
            }
          while (v == 0);

          ttarget = time (NULL);

          if (mark == MLE)
            printstats ("Memory Limit Exceeded\n");
          else if (mark == RTLE)
            printstats ("Time Limit Exceeded\n");
          else
            {
              if (WIFEXITED (status) != 0)
                {
                  if (WEXITSTATUS (status) != 0)
                    printstats ("Command exited with non-zero status (%d)\n",
                                WEXITSTATUS (status));
                  else
                    printstats ("OK\n");
                }
              else
                {
                  if (WIFSIGNALED (status) != 0)
                    {
                      /* Was killed for a TLE (or was it an OLE) */
                      if (WTERMSIG (status) == SIGKILL)
                        mark = TLE;
                      else if (WTERMSIG (status) == SIGXFSZ)
                        mark = OLE;
                      else if (WTERMSIG (status) == SIGHUP)
                        mark = RF;
                      else if (WTERMSIG (status) == SIGPIPE)
                        mark = IE;
                      else
                        printstats ("Command terminated by signal (%d: %s)\n",
                                    WTERMSIG (status),
                                    name (WTERMSIG (status)));
                    }
                  else if (WIFSTOPPED (status) != 0)
                    printstats ("Command terminated by signal (%d: %s)\n",
                                WSTOPSIG (status), name (WSTOPSIG (status)));
                  else
                    printstats ("OK\n");

                  if (mark == TLE)
                    {
                      /* Adjust the timings... although we know the child   *
                       * was been killed just in the right time seing 1.990 *
                       * as TLE when the limit is 2 seconds is anoying      */
                      usage.ru_utime.tv_sec = profile.cpu;
                      usage.ru_utime.tv_usec = 0;
                      printstats ("Time Limit Exceeded\n");
                    }
                  else if (mark == OLE)
                    printstats ("Output Limit Exceeded\n");
                  else if (mark == RTLE)
                    printstats ("Time Limit Exceeded\n");
                  else if (mark == RF)
                    printstats ("Invalid Function\n");
                  else if (mark == IE)
                    printstats ("Internal Error\n");
                }
            }
          printstats ("elapsed time: %d seconds\n", ttarget - tsource);
          printstats ("memory usage: %d kbytes\n", mem);
          printstats ("cpu usage: %0.3f seconds\n",
                      (float) miliseconds (&usage.ru_utime) / 1000.0);
        }
    }
  fclose (redirect);

  return (EXIT_SUCCESS);
}