예제 #1
0
파일: procs.c 프로젝트: changhui1989/procs
struct proc_process *
receive_proc_from_local_server(char* id) {
	struct sockaddr_un them;
	int sender = socket(AF_UNIX, SOCK_STREAM, 0);
 	if (sender < 0) {
		printf("Error creating connection to server\n");
	 	return NULL;
 	}
	them.sun_family = AF_UNIX; 
	strcpy(them.sun_path,LOCAL_SERVER_SOCK);
 	if (connect(sender,(struct sockaddr *)&them,sizeof(them)) < 0) {
		printf("Error connecting to server\n");
	 	return NULL;
 	}
 	if (id!=NULL) {
	 	unsigned char type = 'r';
	 	write_to_socket(&sender,&type,sizeof(char));
	 	write_to_socket(&sender,id,SIZE_OF_ID*sizeof(char));
		return read_process(&sender,read_from_socket);
	} else {
	 	unsigned char type = 'w';
	 	write_to_socket(&sender,&type,sizeof(char));
		return read_process(&sender,read_from_socket);
	}
}
예제 #2
0
파일: process.c 프로젝트: GovReady/openscap
int probe_main(probe_ctx *ctx, void *arg)
{
	SEXP_t *ent;

	ent = probe_obj_getent(probe_ctx_getobject(ctx), "command", 1);
	if (ent == NULL) {
		return PROBE_ENOVAL;
	}

	if (read_process(ent, ctx)) {
		SEXP_free(ent);
		return PROBE_EACCESS;
	}

	SEXP_free(ent);

	return 0;
}
예제 #3
0
파일: process.c 프로젝트: GovReady/openscap
int probe_main(probe_ctx *ctx, void *arg)
{
	SEXP_t *obj, *ent;

	obj = probe_ctx_getobject(ctx);
	over = probe_obj_get_schema_version(obj);
	ent = probe_obj_getent(obj, "command", 1);
	if (ent == NULL) {
		return PROBE_ENOVAL;
	}

	if (read_process(ent, ctx)) {
		SEXP_free(ent);
		return PROBE_EACCESS;
	}

	SEXP_free(ent);

	return 0;
}
예제 #4
0
int probe_main(probe_ctx *ctx, void *arg)
{
	SEXP_t *command_line_ent, *pid_ent;

	command_line_ent = probe_obj_getent(probe_ctx_getobject(ctx), "command_line", 1);
	pid_ent = probe_obj_getent(probe_ctx_getobject(ctx), "pid", 1);
	if (command_line_ent == NULL && pid_ent == NULL) {
		return PROBE_ENOVAL;
	}

	if (read_process(command_line_ent, pid_ent, ctx)) {
		SEXP_free(command_line_ent);
		SEXP_free(pid_ent);
		return PROBE_EACCESS;
	}

	SEXP_free(command_line_ent);
	SEXP_free(pid_ent);

	return 0;
}
예제 #5
0
void real_main(int argc, char** argv)
{
    image_fd = 42;

    /* See if we're being executed for the second time. If so, read arguments
     * from the file.
     */
    if (lseek(image_fd, 0, SEEK_SET) != -1) {
	safe_read(image_fd, &argc, sizeof(argc), "argc from cryopid.state");
	argv = (char**)xmalloc(sizeof(char*)*argc+1);
	argv[argc] = NULL;
	int i, len;
	for (i=0; i < argc; i++) {
	    safe_read(image_fd, &len, sizeof(len), "argv len from cryopid.state");
	    argv[i] = (char*)xmalloc(len);
	    safe_read(image_fd, argv[i], len, "new argv from cryopid.state");
	}
	close(image_fd);
	reforked = 1;
    } else {
	if (errno && errno != EBADF) {
	    /* EBADF is the only error we should be expecting! */
	    fprintf(stderr, "Unexpected error on lseek. Aborting (%s).\n",
		    strerror(errno));
	    exit(1);
	}
    }

    /* Parse options */
    while (1) {
	int option_index = 0;
	int c;
	static struct option long_options[] = {
	    {0, 0, 0, 0},
	};
	
	c = getopt_long(argc, argv, "dvpPg",
		long_options, &option_index);
	if (c == -1)
	    break;
	switch(c) {
	    case 'd':
		action &= ~ACTION_LOAD;
		action |= ACTION_PRINT;
		break;
	    case 'v':
		verbosity++;
		action |= ACTION_PRINT;
		break;
	    case 'p':
		do_pause = 1;
		break;
	    case 'P':
		want_pid = 1;
		break;
#ifdef USE_GTK
	    case 'g':
		gtk_can_close_displays = 1;
		break;
#endif
	    case '?':
		/* invalid option */
		usage(argv[0]);
		break;
	}
    }

    if (argc - optind) {
	fprintf(stderr, "Extra arguments not expected (%s ...)!\n", argv[optind]);
	usage(argv[0]);
    }

    image_fd = real_fd;
    seek_to_image(image_fd);

    read_process();

    fprintf(stderr, "Something went wrong :(\n");
    exit(1);
}
예제 #6
0
파일: procs.c 프로젝트: changhui1989/procs
int 
main(int argc, char** argv)
{
	program_name = argv[0];
	static char *commands[] = {
		"save",
		"restore",
		"send",
		"receive",
		"list",
		"delete"
	};
	int commands_len = sizeof(commands)/sizeof(char*);
	static struct option long_options[] = {
		{"server", 1, 0, 's'},
		{"name", 1, 0, 'n'},
		{"output", 1, 0, 'o'},
		{"nokill", 0, 0, 'K'},
		{"verbose", 0, 0, 'v'},
		{"help", 0, 0, 'h'},
		{"file-save", 0, 0, 'f'},
		{NULL, 0, NULL, 0}
	};
	int c,i,option_index = 0;
	if (argc<2) {
		usage(-1);
		exit(1);
	}
	for (i=0;i<commands_len;i++) {
		if (!strcmp(commands[i],argv[1]))
			break;
	}
	if (!strcmp(argv[1],"--help") || !strcmp(argv[1],"-h")) {
		usage(-1);
		exit(0);
	}
	if (i==commands_len) {
		usage(-1);
		exit(1);
	}
	command_index = i;
	char** real_argv = malloc(sizeof(char*)*argc-1);
	real_argv[0] = argv[0];
	for (i=1;i<argc-1;i++) {
		real_argv[i] = argv[i+1];
	}
	char *output=NULL,*host=NULL,*name=NULL;
	int nokill = FALSE;
	verbose = 0;
	flags = 0;
	while ((c = getopt_long(argc-1, real_argv, "s:vn:hKo:f", long_options, &option_index)) != -1) {
		switch (c) {
			case 's':
				host = malloc(strlen(optarg));
				strcpy(host,optarg);
				break;
			case 'v':
				verbose=TRUE;
				break;
			case 'n':
				name = malloc(strlen(optarg));
				strcpy(name,optarg);
				break;
			case 'h':
				usage(command_index);
				exit(0);
				break;
			case 'o':
				output = malloc(strlen(optarg));
				strcpy(output,optarg);
				break;
			case 'K':
				nokill = TRUE;
				break;
			case 'f':
				flags |= PROC_FLAG_SAVE_FILE;
				break;
			default:
				fprintf(stderr,"?? getopt returned character code 0%o ??\n", c);
		}
	}
	if (command_index==0) {
		//save
		if (optind >= argc-1 && name==NULL) {
			usage(command_index);
			exit(1);
		}
		pid_t pid = 0;
		if (name!=NULL) {
			char tmpproc[100];
			sprintf(tmpproc,"ps ux | awk '/%s/ && !/awk/ {print $2}'",name);
			FILE* proc = popen(tmpproc,"r");
			fscanf(proc,"%d",&pid);
			pclose(proc);
		} else {
			pid = atoi(real_argv[optind]);
		}
		struct proc_process p = save_process(pid,"test",nokill);
		if (output!=NULL) {
			FILE* proc = fopen(output,"w+");
			write_process(p,proc,write_to_file_pointer);
			fclose(proc);
		} else {
			char* id;
			if ((id = send_proc_to_local_server(p))==NULL)
				write_process(p,stdout,write_to_file_pointer);
			printf("%s\n",id);
		}
	} else if (command_index==1) {
		//restore
		if (optind >= argc-1) {
			usage(command_index);
			exit(1);
		}
		char* f_id = real_argv[optind];
		FILE* proc = fopen(f_id,"r");	
		struct proc_process *p=NULL;
		if (!proc) {
			p = receive_proc_from_local_server(f_id);
		} else {
			p = read_process(proc,read_from_file_pointer);
		}
		if (p!=NULL)
			restore_process(*p);
	} else if (command_index==2) {
		//send
		if (optind >= argc-1 && name==NULL) {
			usage(command_index);
			exit(1);
		}
		pid_t pid = 0;
		struct proc_process *p=NULL;
		if (name!=NULL) {
			char tmpproc[100];
			sprintf(tmpproc,"ps ux | awk '/%s/ && !/awk/ {print $2}'",name);
			FILE* proc = popen(tmpproc,"r");
			fscanf(proc,"%d",&pid);
			pclose(proc);
			struct proc_process tmpp = save_process(pid,"test",nokill);
			p = &tmpp;
		} else {
			char* f_id = real_argv[optind];
			FILE* proc = fopen(f_id,"r");	
			if (!proc) {
				p = receive_proc_from_local_server(f_id);
				if (!p) {
					pid = atoi(real_argv[optind]);
					struct proc_process tmpp = save_process(pid,"test",nokill);
					p = &tmpp;
				}
			} else {
				p = read_process(proc,read_from_file_pointer);
			}
		}

		char* ip=DEFAULT_HOST;
		int port=DEFAULT_PORT;
		if (host!=NULL) {
			char* pos = strchr(host,':');
			if (pos!=NULL) {
				*pos='\0';
				ip=host;
				port=*(++pos);
			} else {
				ip=host;
				port = DEFAULT_PORT;
			}
		}
		send_process(*p,ip,port);
	} else if (command_index==3) {
		//receive
		char* id = NULL;
		if (optind >= argc-1) {
			id = real_argv[optind];
		}
		struct proc_process *p= receive_proc_from_local_server(id);
		if (p!=NULL)
			restore_process(*p);
	} else if (command_index==4) {
		//list
		list_from_local_server();
	} else if (command_index==5) {
		//delete
		if (optind >= argc-1) {
			usage(command_index);
			exit(1);
		}
		char id[SIZE_OF_ID];
		strcpy(id,real_argv[optind]);
		delete_from_local_server(id);
	} 
	return 0;
}
예제 #7
0
static int write_message(Server *s, const char *buf, struct ucred *ucred) {
        ssize_t k;
        char priority[6], pid[16];
        struct iovec iovec[5];
        unsigned i = 0;
        char *process = NULL;
        int r = 0;
        int prio = LOG_USER | LOG_INFO;

        assert(s);
        assert(buf);

        parse_syslog_priority((char**) &buf, &prio);

        if (*buf == 0)
                return 0;

        if ((prio & LOG_FACMASK) == 0)
                prio = LOG_USER | LOG_PRI(prio);

        /* First, set priority field */
        snprintf(priority, sizeof(priority), "<%i>", prio);
        char_array_0(priority);
        IOVEC_SET_STRING(iovec[i++], priority);

        /* Second, skip date */
        skip_date(&buf);

        /* Then, add process if set */
        if (read_process(&buf, &iovec[i]) > 0)
                i++;
        else if (ucred &&
                 ucred->pid > 0 &&
                 get_process_name(ucred->pid, &process) >= 0)
                IOVEC_SET_STRING(iovec[i++], process);

        /* Skip the stored PID if we have a better one */
        if (ucred) {
                snprintf(pid, sizeof(pid), "[%lu]: ", (unsigned long) ucred->pid);
                char_array_0(pid);
                IOVEC_SET_STRING(iovec[i++], pid);

                skip_pid(&buf);

                if (*buf == ':')
                        buf++;

                buf += strspn(buf, WHITESPACE);
        }

        /* Is the remaining message empty? */
        if (*buf) {

                /* And the rest is the message */
                IOVEC_SET_STRING(iovec[i++], buf);
                IOVEC_SET_STRING(iovec[i++], "\n");

                if ((k = writev(s->kmsg_fd, iovec, i)) <= 0) {
                        log_error("Failed to write log message to kmsg: %s", k < 0 ? strerror(errno) : "short write");
                        r = k < 0 ? -errno : -EIO;
                }
        }

        free(process);

        return r;
}
예제 #8
0
파일: query.c 프로젝트: SciLifeLab/facs
char *query (char *query, char *bloom_filter, double tole_rate, double sampling_rate, char *list, char *target_path, char *report_fmt, char mode)
{
  gzFile zip = NULL;
  char type = '@';
  int normal = 0;
  int threads = 0;
  BIGCAST offset = 0;
  char *position = NULL;
  static char timestamp[40] = {0};

  // Get current timestamp, for benchmarking purposes (begin of run timestamp)
  isodate(timestamp);
  bloom *bl_2 = NEW (bloom);
  F_set *File_head = make_list (bloom_filter, list);
  /*initialization for python interface*/
  File_head->hits = 0;
  File_head->all_k = 0;
  File_head->reads_num = 0;
  File_head->reads_contam = 0;
  File_head->filename = bloom_filter;           //extra initialization for python interface
  if (load_bloom (File_head->filename, bl_2)<=0)	//load a bloom filter
	exit(-1);
  
  if (tole_rate == 0)
  {
  	tole_rate = mco_suggestion (bl_2->k_mer); // suggest an optimal match cut-off
  }
  if (mode == 'r')
  {
 	init_string(ONEG); // initialize strings for containing reads
  }
/*
  if ((get_size (query) < 2 * ONEG) && !strstr (query, ".gz") && !strstr (query, ".tar"))
        normal = 0;
  else
  {
      if ((zip = gzopen (query, "rb")) < 0)
	{
          perror ("query open error...\n");
          exit (-1);
	}
      normal = 0;
  }
*/
  if ((zip = gzopen (query, "rb")) <= 0)
  {
  	fprintf(stderr, "%s\n", strerror(errno));
  	exit(EXIT_FAILURE);
  }
  
  if (strstr (query, ".fastq") != NULL || strstr (query, ".fq") != NULL)
  	type = '@';
  else
  	type = '>';
  if (normal == 0)
  	position = (char *) calloc (1,(ONEG+1)*sizeof (char));
  while (offset != -1)
  {
      if (normal == 1)
      {
	  position = mmaping (query);
	  offset = -1;
      }
      else
      {
	  offset = CHUNKer (zip, offset, ONEG, position, type);
      }
      Queue *head = NEW (Queue);
      head->location = NULL;
      Queue *tail = NEW (Queue);
      head->next = tail;
      Queue *head2 = head;
      get_parainfo (position, head, type);
#pragma omp parallel
      {
// XXX: Awesome will be the day when OpenMP is in OSX
#ifndef __APPLE__ 
          threads = omp_get_num_threads();
#endif
#pragma omp single nowait
	{
	  while (head != tail)
	    {
#pragma omp task firstprivate(head)
	      {
		if (head->location != NULL)
		{
			read_process (bl_2, head, tail, File_head, sampling_rate, tole_rate, mode, type);
		}
	      }
	      head = head->next;
	    }			// End of firstprivate
	}			// End of single - no implied barrier (nowait)
      }				// End of parallel region - implied barrier
  if (position != NULL && normal == 0)
  {
  	memset (position, 0, strlen (position));
  } 
  else if (normal == 1)
  {
	munmap (position, strlen (position));
  } 
  else
  {
 	perror ("Cannot memset, wrong position on fastq file\n");
	exit (-1);
  }
  clean_list (head2, tail);
  if (mode == 'r')
  {
	if (target_path!=NULL)
	{
      		save_result (query, File_head->filename, type, target_path, re_clean(), re_contam()); //save results into file if facs remove is called
  	}
	else
	{
		write_default(re_clean(), re_contam(), offset);
	}
	reset_string();
  }
  }				//end while
  if (normal == 0)
  {
 	bloom_destroy(bl_2);
  	gzclose(zip);
  	free (position);        //dont like file mapping, strings need to be freed in a normal way
  }

  /*
  mode c and r refer to contamination checking and removal function respectively. 
  The following 9 lines make sure that json/tsv output is printed after the checking 
  process, but willnot be written in stdout when running removal process.
  */
  if (target_path!=NULL || mode == 'c')
  {
  	return report(File_head, query, report_fmt, target_path, timestamp, prob_suggestion(bl_2->k_mer), threads);
  }
  else
  {
	char *s = "";
	return s;
  }
}