예제 #1
0
파일: users.c 프로젝트: realhalo/sample
/* takes a comma-separated list of users and makes the list to compare to. */
void sampled_users_makelist(char *users, unsigned char mode) {
	unsigned int i;
	char *ptr, *lptr;

	/* the list must have already been made, no dupes. */
	if(sampled_usermode != SAM_USERMODE_NONE) return;

	i = 1;

	ptr = users;

	/* how many users are there? */
	while(*ptr) {
		if(*ptr == ':' && *(ptr + 1) && *(ptr + 1) != ':') i++;
		ptr++;
	}

	/* allocate it. */
	if(!(sampled_users = (char **)malloc(sizeof(char *) * i + 2)))
		sample_error(SAM_ERR, "Sample failed to allocate memory for creating user list.");

	lptr = ptr = users;
	i = 0;

	/* breaks itself on a null-byte, puts the users in the array. */
	while(SAM_TRUE) {
		if(!*ptr || *ptr == ':') {
			if(lptr == ptr) lptr = ptr + 1;
			else {
				if(!(sampled_users[i] = (char *)malloc(ptr - lptr + 1)))
					sample_error(SAM_ERR, "Sample failed to allocate memory for creating a user list entry.");
				memset(sampled_users[i], 0 , ptr - lptr + 1);
				strncpy(sampled_users[i++], lptr, ptr - lptr);
				lptr = ptr + 1;
			}
			if(!*ptr) break;
		}
		ptr++;
	}

	/* total number of users. */
	sampled_users_size = i;

	/* set the mode the userlist is. */
	sampled_usermode = mode;

	return;
}
예제 #2
0
파일: sample.c 프로젝트: realhalo/sample
/* tests the path for the number of files it matches. */
size_t sample_get_file_count(char *path, char *home) {
	signed int i, flags;
	size_t tot;
	char *homepath, *globpath, *pptr, *lptr, *ptr;
	glob_t g;

	tot = g.gl_pathc = 0;
	flags = 0;
	pptr = lptr = path;

	/* will break when it hits a null-byte. */
	while(SAM_TRUE) {

		/* "path:path:..." support. */
		if(!*pptr || *pptr == ':') {
			i = pptr - lptr;

			/* no path? */
			if(i < 1) {
				if(!*pptr) break;
				pptr++;
				lptr = pptr;
				continue;
			}

			/* need a possible extra 2 bytes on the end of the buffer, so copy it to modify it. */
			if(!(globpath = (char *)malloc(i + 3)))
				sample_error(SAM_ERR, "Failed to allocate memory for creating glob() path.");
			memset(globpath, 0, i + 3);
			strncpy(globpath, lptr, i);

			/* no directories. */
			if((sample_path_mode(globpath) & S_IFDIR)) {
				ptr = globpath + strlen(globpath) - 1;
				if(*ptr++ != '/') *ptr++ = '/';
				*ptr++ = '*';
				*ptr = 0;
			}

			/* parse ~ to the home dir of the user. */
			homepath = sample_parse_home_string(globpath, home, strlen(home));

			glob(homepath, flags, NULL, &g);

			free(homepath);
			free(globpath);

			/* if another path, append next time. */
			if(!flags) flags = GLOB_APPEND;

			if(!*pptr) break;
			lptr = pptr + 1;
		}
		pptr++;
	}
	tot = g.gl_pathc;
	globfree(&g);
	return(tot);
}
예제 #3
0
파일: sample.c 프로젝트: realhalo/sample
/* checks if ~/.sample exists for a given user, and makes the example ~/.sample if needed. */
void sample_check_exist(char *sample_file, struct passwd *pwd, unsigned char example, unsigned char tab) {
	signed int fd;

	/* already exists, abort. */
	if(!access(sample_file, F_OK)) return;

	if ((fd = open(sample_file, O_WRONLY | O_CREAT, (tab ? 0640 : 0600))) < 0)
		sample_error(SAM_ERR, "Sample failed to open: %s", sample_file);

#ifdef HAVE_FCHOWN
	fchown(fd, (tab ? 0 : pwd->pw_uid), pwd->pw_gid);
#endif
	/* write example if specified. */
	if(example) write(fd, sample_example, strlen(sample_example));

	close(fd);
#ifndef HAVE_FCHOWN
	chown(sample_file, (tab ? 0 : pwd->pw_uid), pwd->pw_gid);
#endif
	return;
}
예제 #4
0
파일: samples.c 프로젝트: realhalo/sample
/* execution start. */
int main(signed int argc, char **argv) {
    unsigned char op;
    unsigned int argc_offset, ttl;
    char *sample_spooldir;
    uid_t uid;
    struct passwd *pwd;

    /* see if we're set*id, which shouldn't be. */
    if(sample_is_privileged())
        sample_error(SAM_ERR, "samples appears to be set*id, which should never be.");

    ttl = 0;
    uid = getuid();

    /* need at least 1 arguments for anything beyond here. */
    if(argc < 2) samples_usage(argv[0]);

    /* set our argument offset, and check for a specified user to run as. */
    argc_offset = 1;
    if(argc > 2 && strlen(argv[2]) > 1 && argv[2][0] == '-') {
        if(uid) sample_error(SAM_ERR, "'%s' user specification option can only be used by root.", argv[2]);
        if(!(pwd = getpwnam(argv[2] + 1)))
            sample_error(SAM_ERR, "'%s' user specification option is not a valid username.", argv[2]);
        else uid = pwd->pw_uid;
        argc_offset++;
    }

    /* user sanity check. */
    if(!(pwd = getpwuid(uid)))
        sample_error(SAM_ERR, "No passwd information for this UID? this shouldn't happen.");
    if(!pwd->pw_dir || !strlen(pwd->pw_dir))
        sample_error(SAM_ERR, "No home directory for this UID? this shouldn't happen.");

    /* make the ~/.sample_spool/ path. */
    if(!(sample_spooldir = (char *)malloc(strlen(pwd->pw_dir) + strlen(SAM_SPOOLDIR) + 3)))
        sample_error(SAM_ERR, "Samples failed to allocate memory for creating sample spool dir.");
    memset(sample_spooldir, 0, strlen(pwd->pw_dir) + strlen(SAM_SPOOLDIR) + 3);
    sprintf(sample_spooldir, "%s/" SAM_SPOOLDIR "/", pwd->pw_dir);

    /* what is our operation? */
    /* see if '-v' is there, need a way to version samples. */
    if(!strcmp(argv[1], "-v")) {
        free(sample_spooldir);
        printf("%s: version " SAM_VERSION "\n", argv[0]);
        exit(SAM_EXIT_SUCCESS);
    }
    else if(!strncmp(argv[1], "add", 3) && argc - argc_offset > 4) op = SAM_SPOOL_OP_ADD;
    else if(!strncmp(argv[1], "del", 3) && argc - argc_offset > 1) op = SAM_SPOOL_OP_DEL;
    else if(!strncmp(argv[1], "wipe", 4)) op = SAM_SPOOL_OP_WIPE;
    else {
        free(sample_spooldir);
        samples_usage(argv[0]);
    }

    /* add monitoring to file. */
    if(op == SAM_SPOOL_OP_ADD) samples_spool_handle_add(argc, argv, argc_offset, sample_spooldir, uid, pwd);

    /* delete monitoring to file. */
    else if(op == SAM_SPOOL_OP_DEL) {
        for(argc_offset++; argc_offset < argc; argc_offset++) {
            if(access(argv[argc_offset], F_OK))
                sample_error(SAM_WRN, "Non-existent file to remove sample spool(s) on: %s", argv[argc_offset]);
            else
                samples_spool_handle_del(sample_spooldir, argv[argc_offset], SAM_FALSE);
        }

    }

    /* wipe all monitoring of files. */
    else if(op == SAM_SPOOL_OP_WIPE) {
        if(access(sample_spooldir, F_OK))
            sample_error(SAM_ERR, "Non-existent sample spool directory: %s", sample_spooldir);
        samples_spool_handle_del(sample_spooldir, NULL, SAM_TRUE);
    }

    exit(SAM_EXIT_SUCCESS);
}
예제 #5
0
파일: simple.c 프로젝트: kandycs/PVFS-AS
int main( int argc, char *argv[] )
{
  int opt;
  extern char   *optarg;
  extern int     optind;
  int is_output_timing=0, is_print_usage = 0;
  int _debug=0, use_gen_file = 0, use_actsto = 0, use_normalsto=0;
  char *token;

  MPI_Offset disp, offset, file_size;
  MPI_Datatype etype, ftype, buftype;

  int errs = 0;
  int size, rank, i, count;
  char *fname = NULL;
  double *buf;
  MPI_File fh;
  MPI_Comm comm;
  MPI_Status status;
  int64_t nitem = 0;
  int fsize = 0, type_size;
  double stime, etime, iotime, comptime, elapsed_time;
  double max_iotime, max_comptime;

  double max, min, sum=0.0, global_sum;

  MPI_Init( &argc, &argv );
 
  comm = MPI_COMM_WORLD;

  MPI_Comm_size( comm, &size );
  MPI_Comm_rank( comm, &rank );
 
  while ( (opt=getopt(argc,argv,"i:s:godhxt"))!= EOF) {
    switch (opt) {
    case 'i': fname = optarg;
      break;
    case 'o': is_output_timing = 1;
      break;
    case 'g': use_gen_file = 1;
      break;
    case 'd': _debug = 1;
      break;
    case 'h': is_print_usage = 1;
      break;
    case 's': 
        token = strtok(optarg, ":");
        //if (rank == 0) printf("token=%s\n", token);
        if(token == NULL) {
            if (rank == 0) printf("1: Wrong file size format!\n");
            MPI_Finalize();
            exit(1);
        }

        fsize = atoi(token);
        token = strtok(NULL, ":");
        //if (rank == 0) printf("token=%s\n", token);
        if(token == NULL) {
            if (rank == 0) printf("2: Wrong file size format!\n");
            MPI_Finalize();
            exit(1);
        }
        if(*token != 'm' && *token != 'g') {
            if (rank == 0) printf("3: Wrong file size format!\n");
            MPI_Finalize();
            exit(1);
        }
        if (rank ==0) printf("fsize = %d (%s)\n", fsize, (*token=='m'?"MB":"GB"));
      if (fsize == 0)
	nitem = 0;
      else {
	MPI_Type_size(MPI_DOUBLE, &type_size);
	nitem = fsize*1024; /* KB */
	nitem = nitem*1024; /* MB */
        if(*token == 'g') {
            //if(rank == 0) printf("data in GB\n");
            nitem = nitem*1024; /* GB */
        }
	nitem = nitem/type_size;
	//printf("nitem=%lld\n", nitem);
	nitem = nitem/size; /* size means comm size */
      }
      if (rank == 0) printf("nitem = %d\n", nitem);
      break;
    case 'x': use_actsto = 1;
      break;
    case 't': use_normalsto = 1;
      break;
    default: is_print_usage = 1;
      break;
    }
  }

  if (fname == NULL || is_print_usage == 1 || nitem == 0) {
    if (rank == 0) usage(argv[0]);
    MPI_Finalize();
    exit(1);
  }

  int sizeof_mpi_offset;
  sizeof_mpi_offset = (int)(sizeof(MPI_Offset)); // 8 
  //if (rank == 0) printf ("size_of_mpi_offset=%d\n", sizeof_mpi_offset);

  if(use_normalsto == 1 && use_actsto == 1) {
      if(rank == 0)
          printf("Can't test both: either normalsto or actsto\n");
      MPI_Finalize();
      exit(1);
  }
#if 0
  if(use_actsto == 1) {
      if (size != 1) {
          if(rank == 0)
              printf("active storage should be run with only 1 process!!!\n");
          MPI_Finalize();
          exit(1);
      }
  }
#endif
  /* initialize random seed: */
  srand(time(NULL));

  if(use_gen_file == 1) {
    int t, result;

    MPI_File_open( comm, fname, MPI_MODE_RDWR | MPI_MODE_CREATE, MPI_INFO_NULL, &fh );

    /* Set the file view */
    disp = rank * nitem * type_size;
    printf("%d: disp = %lld\n", rank, disp);
    etype = MPI_DOUBLE;
    ftype = MPI_DOUBLE;

    result = MPI_File_set_view(fh, disp, etype, ftype, "native", MPI_INFO_NULL);

    if(result != MPI_SUCCESS) 
      sample_error(result, "MPI_File_set_view");

    buf = (double *)malloc( nitem * sizeof(double) );

    if (buf == NULL) {
        if(rank == 0) printf("malloc() failed\n");
        MPI_Finalize();
        exit(1);
    }

    buf[0] = rand()%4096;
    if(rank==0) printf("%lf\n", buf[0]);
    max = min = sum = buf[0];

    for(i=1; i<nitem; i++) {
      t = rand()%4096;
      if (t>max) max = t;
      if (t<min) min = t;
      sum += t;
      buf[i] = t;
      if (i<10 && rank == 0) printf("%lf\n", buf[i]);
    }
    
    if(rank == 0) {
      printf("MPI_Type_size(MPI_DOUBLE)=%d\n", type_size);
      printf ("max=%lf, min=%lf, sum=%lf\n", max, min, sum);
    }

    stime = MPI_Wtime();
    /* Write to file */
    MPI_File_write_all( fh, buf, nitem, MPI_DOUBLE, &status );
    etime = MPI_Wtime();
    iotime = etime - stime;
      
    printf("%d: iotime (write) = %10.4f\n", rank, iotime);

    MPI_Get_count( &status, MPI_DOUBLE, &count );
    //printf("count = %lld\n", count);

    if (count != nitem) {
      fprintf( stderr, "%d: Wrong count (%lld) on write\n", rank, count );
      fflush(stderr);
      /* exit */
      MPI_Finalize();
      exit(1);
    }

    MPI_File_close(&fh);
    MPI_Barrier(MPI_COMM_WORLD);
    if(rank == 0) printf("File is written\n\n");
  }

  double *tmp = (double *)malloc( nitem * sizeof(double) );
  memset (tmp, 0, nitem*sizeof(double));

  if(use_normalsto == 1) {
      MPI_File_open( comm, fname, MPI_MODE_RDWR, MPI_INFO_NULL, &fh );
      /* Read nothing (check status) */
      memset( &status, 0xff, sizeof(MPI_Status) );
      
      offset = rank * nitem * type_size;

      /* start I/O */
      stime = MPI_Wtime();
      MPI_File_read_at(fh, offset, tmp, nitem, MPI_DOUBLE, &status);
      etime = MPI_Wtime();
      /* end I/O */
      iotime = etime - stime;
      
      if(_debug==1) printf("%d: iotime = %10.4f\n", rank, iotime);
      MPI_Reduce(&iotime, &max_iotime, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
      
      sum = 0.0; /* reset sum */
      
      /* start computation */
      stime = MPI_Wtime();
      
      for(i=0; i<nitem; i++) {
          sum += tmp[i];
      }
      
      MPI_Reduce(&sum, &global_sum, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);
      etime = MPI_Wtime();
      /* end computation */

      comptime = etime - stime;

      if(_debug==1) printf("%d: comptime = %10.4f\n", rank, comptime);
      
      MPI_Reduce(&comptime, &max_comptime, 1, MPI_DOUBLE, MPI_MAX, 0, MPI_COMM_WORLD);

      if(rank == 0) {
          elapsed_time = max_comptime + max_iotime;
          printf("<<Result (SUM) with normal read>>\n"
                 "SUM              = %10.4f \n"
                 "Computation time = %10.4f sec\n"
                 "I/O time         = %10.4f sec\n"
                 "total time       = %10.4f sec\n\n", 
                 global_sum, max_comptime, max_iotime, elapsed_time);
      }
      
      MPI_File_close(&fh);
  }
#if 0
  if(use_actsto == 1) {
#if 0
    /* MPI_MAX */
    MPI_File_open( comm, fname, MPI_MODE_RDWR, MPI_INFO_NULL, &fh );

    stime = MPI_Wtime();
    MPI_File_read_at_ex( fh, offset, tmp, nitem, MPI_DOUBLE, MPI_MAX, &status );
    etime = MPI_Wtime();
    elapsed_time = etime-stime;
    printf ("<<Result with active storage>>\n"
	    "max=%lf (in %10.4f sec)\n", tmp[0], elapsed_time);
    
    MPI_File_close(&fh);
    
    /* MPI_MIN */
    MPI_File_open( comm, fname, MPI_MODE_RDWR, MPI_INFO_NULL, &fh );
    
    stime = MPI_Wtime();
    MPI_File_read_at_ex( fh, offset, tmp, nitem, MPI_DOUBLE, MPI_MIN, &status );
    etime = MPI_Wtime();
    elapsed_time = etime - stime;
    printf ("min=%lf (in %10.4f sec)\n", tmp[0], elapsed_time); 
    
    MPI_File_close(&fh);
#endif

    /* MPI_SUM */
    MPI_File_open( comm, fname, MPI_MODE_RDWR, MPI_INFO_NULL, &fh );
    memset(&status, 0xff, sizeof(MPI_Status));
    offset = rank * nitem * type_size;
    
    stime = MPI_Wtime();
    MPI_File_read_at_ex( fh, offset, tmp, nitem, MPI_DOUBLE, MPI_SUM, &status );
    etime = MPI_Wtime();
    elapsed_time = etime - stime;
    printf ("<<Result with active storage>>\n"
            "sum=%lf (in %10.4f sec)\n", tmp[0], elapsed_time); 
    
    MPI_File_close( &fh );
  }
#endif
  MPI_Barrier(MPI_COMM_WORLD);
  if (use_gen_file == 1) free( buf );
  free( tmp );
 
  MPI_Finalize();
  return errs;
}
예제 #6
0
파일: sample.c 프로젝트: realhalo/sample
/* execution start. */
int main(signed int argc, char **argv) {
	unsigned char example, remove, tab;
	signed int chr;
	char *editor, *sample_file, *ptr;
	uid_t uid;
	struct passwd *pwd;

	/* see if we're set*id, which shouldn't be. */
	if(sample_is_privileged())
		sample_error(SAM_ERR, "sample appears to be set*id, which should never be.");

	/* defaults. */
	example = SAM_TRUE;
	remove = SAM_FALSE;
	tab = SAM_FALSE;

	editor = 0;
	uid = getuid();

	while((chr = getopt(argc, argv, "vrltu:e:nh")) != EOF) {
		switch(chr) {
			case 'v': /* version. */
				printf("%s: version " SAM_VERSION "\n", argv[0]);
				exit(SAM_EXIT_SUCCESS);
				break;
			case 'n': /* don't create example comments in ~/.sample for new files. */
				example = SAM_FALSE;
				break;
			case 'r': /* remove your (or -u specified) ~/.sample file. */
				remove = SAM_TRUE;
				break;				
			case 'l': /* (root) list users on the system with ~/.sample files. */
				if(getuid()) sample_error(SAM_ERR, "'-l' option can only be used by root.");
				sample_list_samples();
				exit(SAM_EXIT_SUCCESS);
				break;				
			case 't': /* (root) switch to /etc/sampletab/<username>, instead of ~/.sample */
				if(getuid()) sample_error(SAM_ERR, "'-t' option can only be used by root.");
				tab = SAM_TRUE;
				break;				
			case 'u': /* (root) specify other user to edit. */
				if(getuid()) sample_error(SAM_ERR, "'-u' option can only be used by root.");
				if(!(pwd = getpwnam(optarg)))
					sample_error(SAM_ERR, "'-u' option argument is not a valid username.");
				else uid = pwd->pw_uid;
				break;
			case 'e': /* specify alternate editor. */
				if(!access(optarg, X_OK) && (sample_path_mode(optarg) & S_IFREG)) {
					if(editor) free(editor);
					if(!(editor = (char *)malloc(strlen(optarg) + 1)))
						sample_error(SAM_ERR, "Sample failed to allocate memory for creating editor path.");
					memset(editor, 0, strlen(optarg) + 1);
					strcpy(editor, optarg);
				}

				/* no '/'? maybe it wants us to find the path. */
				else if(!strchr(optarg, '/')) {

					/* try $PATH first. */
					if((ptr = getenv("PATH"))) editor = sample_get_editor(ptr, optarg);

					/* nothing? try built-in paths. */
					if(!editor) editor = sample_get_editor(sample_paths, ptr);

				}

				if (!editor) sample_error(SAM_ERR, "'-e' option argument does not exist or isn't executable.");
				break;
			case 'h': /* brief help. */
			default:
				printf("usage: %s [-vnrlt] [-u user] [-e editor]\n", argv[0]);
				exit(SAM_EXIT_ERROR);
				break;
		}
	}


	/* sanity check. */
	if(!(pwd = getpwuid(uid)))
		sample_error(SAM_ERR, "No passwd information for this UID? this shouldn't happen.");
	if(!pwd->pw_dir || !strlen(pwd->pw_dir))
		sample_error(SAM_ERR, "No home directory for this UID? this shouldn't happen.");

	/* make /etc/sampletab/<username> (-t option as root) or ~/.sample string. */
	if(tab) {

		/* make the tab directory if it doesn't exist. (we'll be root if we make it here) */
		if(access(SAM_TABDIR, F_OK)) mkdir(SAM_TABDIR, 0755);

		if(!(sample_file = (char *)malloc(strlen(SAM_TABDIR) + strlen(pwd->pw_name) + 2)))
			sample_error(SAM_ERR, "Sample failed to allocate memory for creating sample filename.");
		memset(sample_file, 0, strlen(SAM_TABDIR) + strlen(pwd->pw_name) + 2);
		sprintf(sample_file, SAM_TABDIR "/%s", pwd->pw_name);
	}
	else {
		if(!(sample_file = (char *)malloc(strlen(pwd->pw_dir) + strlen(SAM_FILENAME) + 2)))
			sample_error(SAM_ERR, "Sample failed to allocate memory for creating sample filename.");
		memset(sample_file, 0, strlen(pwd->pw_dir) + strlen(SAM_FILENAME) + 2);
		sprintf(sample_file, "%s/%s", pwd->pw_dir, SAM_FILENAME);
	}

	/* if we're removing, do it first and bail. */
	if(remove) {
		if(access(sample_file, F_OK)) sample_error(SAM_ERR, "%s's \"%s\" doesn't appear to exist.", pwd->pw_name, sample_file);
		else if(unlink(sample_file)) sample_error(SAM_ERR, "%s's \"%s\" failed to be removed.", pwd->pw_name, sample_file);
		else sample_error(SAM_WRN, "%s's \"%s\" has been removed successfully.", pwd->pw_name, sample_file);
		exit(SAM_EXIT_SUCCESS);
	}

	/* possibly set by the -e command-line argument. */
	if(!editor) {

		/* try user's choice first. ('-e' is still priority) */
		if((ptr = getenv("EDITOR"))) {

			/* direct path. */
			if(!access(ptr, X_OK)) {

				/* free()ing a getenv() pointer is no good, follow the mold. */
				if(!(editor = (char *)malloc(strlen(ptr) + 1)))
					sample_error(SAM_ERR, "Sample failed to allocate memory for creating direct editor path.");
				memset(editor, 0, strlen(ptr) + 1);
				strcpy(editor, ptr);
			}

			/* no '/' in $EDITOR? maybe it wants us to find the path. */
			else if(!strchr(ptr, '/')) {

				/* try $PATH first. */
				if(getenv("PATH")) editor = sample_get_editor(getenv("PATH"), ptr);

				/* nothing? try built-in paths. */
				if(!editor) editor = sample_get_editor(sample_paths, ptr);
			}

			/* oh well. */
			if (!editor) sample_error(SAM_ERR, "$EDITOR is set, but the file does not exist or isn't executable.");
		}

		/* generic editor. */
		else{

			/* try $PATH first. */
			if((ptr = getenv("PATH"))) editor = sample_get_editor(ptr, NULL);

			/* nothing? try built-in paths. */
			if(!editor) editor = sample_get_editor(sample_paths, NULL);
		}
	}

	/* nothing? too bad. */
	if(!editor) 
		sample_error(SAM_ERR, "No viable text editor found to edit ~/" SAM_FILENAME ".");

	/* see if we have a ~/.sample already, create with/without example if not. */
	sample_check_exist(sample_file, pwd, example, tab);

	/* start up the editor to edit the ~/.sample and wait. */	
	sample_run_editor(sample_file, editor);

	/* another back-up, different for a sampletab. */
	if(tab) {
		chmod(sample_file, 0640);
		chown(sample_file, 0, pwd->pw_gid);
	}
	else {
		chmod(sample_file, 0600);
		chown(sample_file, pwd->pw_uid, pwd->pw_gid);
	}

	/* validate the updated/new ~/.sample file. */
	sample_validate(sample_file, pwd->pw_dir);

	free(editor);
	free(sample_file);

	exit(SAM_EXIT_SUCCESS);
}