示例#1
0
        ~Timer()
        {
            print();
#ifndef BH_TIMING_SUM
            write2file();
#endif
        }
示例#2
0
文件: daemonrun.c 项目: donhall/util
/* daemonize and run 'cmd', returning pid of the daemonized process */
int daemonrun(char *incmd, char *pidfile) {
  int pid, sid, i, status, rc;
  char **argv=NULL, *cmd;
  
  if (!incmd) {
    return(1);
  }
  
  pid = fork();
  if (pid < 0) {
    return(1);
  }
  if (!pid) {
    char *tok=NULL, *ptr=NULL;
    int idx, rc;
    struct sigaction newsigact = { 0 };

    newsigact.sa_handler = SIG_DFL;
    newsigact.sa_flags = 0;
    sigemptyset(&newsigact.sa_mask);
    sigprocmask(SIG_SETMASK, &newsigact.sa_mask, NULL);
    sigaction(SIGTERM, &newsigact, NULL);
    
    rc = daemon(0,0);
    // become parent of session
    sid = setsid();
    
    // construct argv
    cmd = strdup(incmd);
    idx=0;
    argv = realloc(NULL, sizeof(char *));
    tok = strtok_r(cmd, " ", &ptr);
    while(tok) {
      fflush(stdout);
      argv[idx] = strdup(tok);
      idx++;
      tok = strtok_r(NULL, " ", &ptr);
      argv = realloc(argv, sizeof(char *) * (idx+1));
    }
    argv[idx] = NULL;
    free(cmd);
    
    // close all fds
    for (i=0; i<sysconf(_SC_OPEN_MAX); i++) {
      close(i);
    }

    if (pidfile) {
      char pidstr[32];
      snprintf(pidstr, 32, "%d", getpid());
      rc = write2file(pidfile, pidstr);
    }
    // run
    exit(execvp(*argv, argv));
  } else {
    rc = waitpid(pid, &status, 0);
  }
  
  return(0);
}
void main(int argc, char *argv[]){
  int t;
  char fname1[100], fname2[100];
  int m = sprintf(fname1,"phi");
  int n = sprintf(fname2,"velocities");

  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD,&numtasks);
  MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
  numworkers = numtasks - 1;
  allocate_memory_phasefields(MESHX);
#ifdef FLUID
  allocate_memory_FluidVariables(MESHX,pmesh);
#endif
  if(taskid == MASTER) {
    initialize_phasefields();
#ifdef FLUID
    initialize_velocities(MESHX);
#endif
    mpi_distribute(MESHX);
    for (t = 0; t <= phi_timesteps; t++){
#ifdef FLUID
      gauss_siedel();
#endif
      if (t%save_phi == 0) {
        receivefrmworker(phi_old);
        receivefrmworker(u_old);
        receivefrmworker(v_old);
        write2file( t, MESHX, phi_old, fname1);
        write2file1( t, MESHX, u_old, v_old, fname2);
      }
    }
  }else {
    mpi_distribute(MESHX);

    for (t = 0; t <= phi_timesteps; t++){
      mpiexchange(taskid, phi_old, MESHX);
      mpiexchange(taskid, mu_old, MESHX);
      boundary_mpi(taskid, phi_old, MESHX);
      boundary_mpi(taskid, mu_old, MESHX);
      laplacian(phi_old, lap_phi, MESHX);
      laplacian(mu_old, lap_mu, MESHX);
#ifdef FLUID
      computeH(u_old,v_old,Hx,Hy);
      RHS_fn(Hx,Hy,rhs_fn,MESHX, pmesh);
      LHS_fn(MESHX, pmesh);
      boundary_pressure(taskid);
      gauss_siedel();
      ns_solver(start, end, phi_old);
      update_velocities(MESHX);
#endif
      solverloop();
      phi_update();
      if (t%save_phi == 0) {
        sendtomaster(taskid, phi_old);
      }
    }
  }
}
示例#4
0
//!
//! Main entry point of the application
//!
//! @param[in] argc the number of parameter passed on the command line
//! @param[in] argv the list of arguments
//!
//! @return Always return 0
//!
int main(int argc, char **argv)
{
    int status = 0;
    char d[MAX_PATH] = "/tmp/euca-XXXXXX";
    char h0[MAX_PATH] = "";
    char h1[MAX_PATH] = "";
    char h3[MAX_PATH] = "";
    char h4[MAX_PATH] = "";

    assert(call_hooks("e1", NULL) != 0);
    assert(call_hooks("e1", "p1") != 0);
    assert(init_hooks("/tmp", "/foobar") != 0);
    assert(init_hooks("/foobar", "/tmp") != 0);

    assert(mkdtemp(d) != NULL);
    assert(init_hooks("/tmp", d) == 0);

    snprintf(h1, sizeof(h1), "%s/h1", d);
    write2file(h1, "#!/bin/bash\necho h1 -$1- -$2- -$3-\n");
    chmod(h1, S_IXUSR | S_IRUSR);
    snprintf(h3, sizeof(h3), "%s/h3", d);
    write2file(h3, "#!/bin/bash\necho h3 -$1- -$2- -$3-\n");
    chmod(h3, 0);                      // unreadable hook
    snprintf(h4, sizeof(h4), "%s/h4", d);
    mkdir(h4, 0700);                   // not a file

    assert(call_hooks("e1", NULL) == 0);
    assert(call_hooks("e1", "p1") == 0);
    snprintf(h0, sizeof(h0), "%s/h0", d);
    write2file(h0, "#!/bin/bash\nexit 99;\n");
    chmod(h0, S_IXUSR | S_IRUSR);
    assert(call_hooks("e1", "p1") == 99);

    assert(rmdir(h4) == 0);
    assert(unlink(h3) == 0);
    assert(unlink(h0) == 0);
    assert(unlink(h1) == 0);
    assert(rmdir(d) == 0);
    printf("removed directory %s\n", d);

    return (status);
}
示例#5
0
文件: cache.c 项目: sosilent/euca
// saves the summary of disk item to disk
int add_summ_disk_item (disk_item * di, artifacts_spec * spec)
{
    int ret = ERROR;

    char * summary = gen_summary (spec);
    if (summary != NULL) {
        ret = write2file (di->summ, summary);
        free (summary);
    }

    return ret;
}
void file_output(){
  char fname1[100], fname2[100], fname3[100];
  int  m = sprintf(fname1, "velocities");
  int  n = sprintf(fname2, "rho");
  int  o = sprintf(fname3, "phi");
#ifdef LBM
  write2file1(t,Mx,u,v,fname1);
#endif
#ifdef PHI
  write2file(t,Mx,phi_old,fname3);
#endif
}
示例#7
0
//!
//! Run a daemonized program and maintain its state. If a PID file is given, it will check if the
//! process is currently running and if the running process matches the given program. If not, the
//! current process will be terminated and restarted with the new program. If the process is running
//! and matche our program name, it will be left alone. If the process is not currently running,
//! it will be started.
//!
//! @param[in] psPidFilePath a constant string pointer to the PID file path
//! @param[in] psRootWrap a constant string pointer to the rootwrap program location
//! @param[in] force set to TRUE if we want to kill the process regardless of its state and restart it. Otherwise set to FALSE.
//! @param[in] psProgram a constant string pointer to the pathname of a program which is to be executed
//! @param[in] ... the list of string arguments to pass to the program
//!
//! @return 0 on success or 1 on failure
//!
//! @pre
//!     - psProgram should not be NULL
//!     - There more be more than 1 variable argument provided
//!
//! @post
//!     On success, the program is executed and its PID is recorded in the psPidFilePath location if provided. If
//!     the process is already running, nothing will change. On failure, depending of where it occured, the system
//!     is left into a non-deterministic state from the caller's perspective.
//!
//! @note
//!
//! @todo
//!     We should move this to something more global under util/euca_system.[ch]
//!
int eucanetd_run_program(const char *psPidFilePath, const char *psRootWrap, boolean force, const char *psProgram, ...)
{
#define PID_STRING_LEN       32

    int i = 0;
    int rc = 0;
    char *psPidId = NULL;
    char *pString = NULL;
    char **argv = NULL;
    char sPid[PID_STRING_LEN] = "";
    char sFilePath[EUCA_MAX_PATH] = "";
    char sCommand[EUCA_MAX_PATH] = "";
    const char *psProgramName = psProgram;
    FILE *pFh = NULL;
    pid_t pid = 0;
    boolean found = FALSE;
    va_list va = { {0} };

    // Make sure we know what app we are running
    if (!psProgram) {
        return (1);
    }
    // turn variable arguments into a array of strings for the euca_execvp_fd()
    va_start(va, psProgram);
    {
        argv = build_argv(psProgram, va);
    }
    va_end(va);

    // Make sure we have a valid arg list
    if (argv == NULL)
        return (1);

    //
    // Set the psProgramName properly. If its currently the rootwrap program, then move to the
    // next argument in the list
    //
    if (!strcmp(psProgram, psRootWrap)) {
        // We should have another argument or I don't see how we can run rootwrap without something else?!?!?
        if (argv[1] == NULL) {
            free_char_list(argv);
            return (1);
        }
        // We're good, use the next argument
        psProgramName = argv[1];
    }
    // Do we need to check if we have the exact same program running?
    if (psPidFilePath) {
        found = FALSE;

        // Does the PID file exists?
        if ((rc = check_file(psPidFilePath)) == 0) {
            //
            // read and make sure the command matches. If it does not match, we will need to restart.
            //
            if ((psPidId = file2str(psPidFilePath)) != NULL) {
                snprintf(sFilePath, EUCA_MAX_PATH, "/proc/%s/cmdline", psPidId);
                // Check if the process is running
                if (check_file(sFilePath) == 0) {
                    // read the old command and make sure we have the same command running
                    if ((pFh = fopen(sFilePath, "r")) != NULL) {
                        if (fgets(sCommand, EUCA_MAX_PATH, pFh)) {
                            if (strstr(sCommand, psProgramName)) {
                                // process is running, and is indeed psProgram
                                found = TRUE;
                            }
                        }
                        fclose(pFh);
                    }
                }

                EUCA_FREE(psPidId);
            }

            if (found) {
                // pidfile passed in and process is already running
                if (force) {
                    // kill process and remove pidfile
                    LOGTRACE("Stopping '%s'\n", psProgramName);
                    rc = safekillfile(psPidFilePath, psProgramName, 9, psRootWrap);
                } else {
                    // nothing to do
                    LOGTRACE("Program '%s' running properly. Nothing to do.\n", psProgramName);
                    free_char_list(argv);
                    return (0);
                }
            } else {
                // pidfile passed in but process is not running
                unlink(psPidFilePath);
            }
        }

    }
    // Build the command string for debugging purpose
    for (i = 0, pString = sCommand; argv[i] != NULL; i++) {
        pString += snprintf(pString, (EUCA_MAX_PATH - (pString - sCommand)), "%s ", argv[i]);
    }

    rc = euca_execvp_fd(&pid, NULL, NULL, NULL, argv);
    LOGTRACE("Executed '%s'. PID=%d, RC=%d\n", sCommand, pid, rc);
    free_char_list(argv);

    if (psPidFilePath) {
        snprintf(sPid, PID_STRING_LEN, "%d", pid);
        rc = write2file(psPidFilePath, sPid);
    }
    return (rc);

#undef PID_STRING_LEN
}