Beispiel #1
0
int Java_org_smalltalk_stack_StackVM_setVMPath(JNIEnv *env, jobject self,jstring executablePath_, jstring imagePath_, jstring command_) {
  const char *exePath = (*env)->GetStringUTFChars(env, executablePath_, 0);
  const char *imgpath = (*env)->GetStringUTFChars(env, imagePath_, 0);
  const char *cmd = (*env)->GetStringUTFChars(env, command_, 0);
  int i,j,z,rc;

  int maximgarg = 128;
  char *imgargv[maximgarg];
  int imgargc = splitcmd(cmd, maximgarg, imgargv);
  
  int argl = 2 + imgargc + 1;
  char **argc = alloca(sizeof(char *) * argl);

  argc[0] = exePath;
  argc[1] = imgpath;
  i = 2;
  for(j = 0; j < imgargc; j++, i++) argc[i] = imgargv[j];
  argc[i] = NULL;
  char *envp[] = {NULL};
  
  rc = main(argl - 1, argc, envp);
  
  (*env)->ReleaseStringUTFChars(env, executablePath_, exePath);
  (*env)->ReleaseStringUTFChars(env, imagePath_, imgpath);
  (*env)->ReleaseStringUTFChars(env, command_, cmd);
  return rc;
}
Beispiel #2
0
void parse_cmd(char *cmdpart)
{
  int setoutpipe = 0;        /* TRUE if need to set up output pipe
                   after forking */
  int pid;            /* Set to pid of child process */
  int fd;            /* fd to use for input redirection */

  char *args[MAXARGS + 5];
  char *filename;            /* Filename to use for I/O redirection */

  splitcmd(cmdpart, args);

  if (pipefd[0] == PARSE_USEPIPE) {
    pipe(pipefd);
    setoutpipe = 1;
  }

  pid = fork();
  if (!pid) {            /* child */
    if (setoutpipe) {
      dup2(pipefd[1], 1);    /* connect stdout to pipe if necessary */
    }
    if (!setoutpipe && (pipefd[0] > -1)) {
      /* Need to set up an input pipe. */
      dup2(pipefd[0], 0);
    }

    filename = parseredir('<', args);

    if (filename) {    /* Input redirection */
      fd = open(filename, O_RDONLY);
      if (!fd) {
       fprintf(stderr, "Couldn't redirect from %s", filename);
       exit(255);
      }
      dup2(fd, 0);
    }

    if ((filename = parseredir('>', args))) { /* Output redirection */
      fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
      if (!fd) {
       fprintf(stderr, "Couldn't redirect to %s\n", filename);
       exit(255);
      }
      dup2(fd, 1);
    }

    if (!args[0]) {
      fprintf(stderr, "No program name specified.\n");      
      exit(255);
    }
    
    execvp(args[0], args);
    /* If failed, die. */
    exit(255);
  } else {            /* parent */
    if ((!background) &&
       (!setoutpipe))
      waitpid(pid, (int *)NULL, 0);
    else
      if (background)
       fprintf(stderr, "BG process started: %d\n", (int) pid);
    if (pipefd[0] > -1) {    /* Close the pipe if necessary. */
      if (setoutpipe)
        close(pipefd[1]);
      else                     
        close(pipefd[0]);
    }                           
  } /* if (!pid) */
  freeargs(args);
} /* parse_cmd()  */
Beispiel #3
0
int main(){
	FILE *fs= NULL,*fp=NULL; //fs->cmdlist.log fp->command pipe
	pid_t process_id = 0;
	pid_t sid = 0; //Session id
	char cmdline[MAX_CMD_LENGTH];
	const char cmdlist_folder[]="/var/log/dcmd";
	const char cmdlist_path[]="/var/log/dcmd/cmdlist.log";
	char text_old_daemon_pid[MAX_PID_LENGTH+1];
	char text_new_daemon_pid[MAX_PID_LENGTH+1];
	char cmd_tmp[MAX_CMD_LENGTH];
	int time_tmp;
	struct cmdtime cmdlist[MAX_NUMBER_OF_CMDS];
	int cmd_count=0;
	
	process_id = fork(); //Create child process

	if (process_id < 0){ //Indication of fork() failure
		fprintf(stderr, "aparigraha-daemon: ","Fork failed");
		exit(1);
	}
	
	
	//LAUNCHER
	if (process_id > 0){
		printf("aparigraha-daemon: Process_id of child process %d \n", process_id);
		exit(0);//Parent process must end.
	}
	
	//DAEMONIZATION
	
	umask(0); //Unmask the file mode
	sid = setsid(); //Set new session
	if(sid < 0)
		exit(1);

	chdir("/"); // Change the current working directory to root.
	
	// Close stdin, stdout and stderr
	close(STDIN_FILENO);
	close(STDOUT_FILENO);
	close(STDERR_FILENO);
	
	if((fs = fopen (cmdlist_path, "r+"))==NULL)// Open a log file in read/write mode.
		exit(1);
	
	text_old_daemon_pid[0]=fgetc(fs);
	
	/*READ PID*/
	if(text_old_daemon_pid[0]!='0'){
		//The PID stored in cmdlist.log is not 0, so there is another daemon running. End it.
		int count=1;
		while(text_old_daemon_pid[count]!='#' && text_old_daemon_pid[count]!= EOF){
			text_old_daemon_pid[count] = fgetc (fs);
			count++;
    		}
    		text_old_daemon_pid[count]='\0';
    		pid_t old_daemon=atoi(text_old_daemon_pid);
    		kill(old_daemon,SIGTERM); //Terminate old daemon
	}
	
	/*WRITE PID*/
	fseek ( fs, 0 , SEEK_SET ); //Go back to the begin of file
	int i;
	for(i=0;i<MAX_PID_LENGTH+1;i++)
		fprintf(fs,"#");	//####################
	fseek ( fs, 0 , SEEK_SET ); //Go back to the begin of file
	fprintf(fs,"%d",getpid()); //Write pid: <PID>###...
	fflush(fs);
	//fclose(fs);
	
	//Start sleeps/commands
	fseek ( fs, 0 , SEEK_SET ); //Go back to the begin of file
	
	fgets(cmdline, sizeof(cmdline)-1, fs); //First row has the PID, don't care
	while (fgets(cmdline, sizeof(cmdline)-1, fs) != NULL) {
		splitcmd(cmdline,cmd_tmp,&time_tmp);
		cmdlist[cmd_count].cmd=cmd_tmp;
		cmdlist[cmd_count].date=time_tmp;		
    		cmd_count++;
  	}
  	qsort (cmdlist,cmd_count, sizeof(struct cmdtime), compare); //Order by ascending date
  	//FILE *fx= fopen ("/home/ezaupa/Documents/uni/2^anno/SO1/SOProject/dcmd/6_aparigraha/dallas", "r+");
  	for(i=0;i<cmd_count;i++){
  		sleep(cmdlist[i]-(int)time(NULL)); //Future-now=sleep time
  		//COMMAND
  		//fprintf(fx,"cmds: %s",cmdlist[i].cmd);
    		//fprintf(fx," date: %d\n",(int)cmdlist[i].date); 
  		//fflush(fx);
  	}
  	//fclose(fx);
	//End sleeps/commands
	
	/*RESET PID*/
	fs = fopen (cmdlist_path, "r+");
	fseek ( fs, 0 , SEEK_SET ); //Go back to the begin of file
	for(i=0;i<MAX_PID_LENGTH+1;i++)
		fprintf(fs,"#");	//####################
	fseek ( fs, 0 , SEEK_SET ); //Go back to the begin of file
	fprintf(fs,"0"); //Reset PID before close pid
	fflush(fs);
	fclose(fs);
	
	exit(0);
}