Beispiel #1
0
int fuser_main(int argc UNUSED_PARAM, char **argv)
{
	pid_list *plist;
	inode_list *ilist;
	char **pp;
	dev_t dev;
	ino_t inode;
	unsigned port;
	int opt;
	int success;
	int killsig;
/*
fuser [options] FILEs or PORT/PROTOs
Find processes which use FILEs or PORTs
        -m      Find processes which use same fs as FILEs
        -4      Search only IPv4 space
        -6      Search only IPv6 space
        -s      Silent: just exit with 0 if any processes are found
        -k      Kill found processes (otherwise display PIDs)
        -SIGNAL Signal to send (default: TERM)
*/
	/* Handle -SIGNAL. Oh my... */
	killsig = SIGTERM;
	pp = argv;
	while (*++pp) {
		char *arg = *pp;
		if (arg[0] != '-')
			continue;
		if (arg[1] == '-' && arg[2] == '\0') /* "--" */
			break;
		if ((arg[1] == '4' || arg[1] == '6') && arg[2] == '\0')
			continue; /* it's "-4" or "-6" */
		opt = get_signum(&arg[1]);
		if (opt < 0)
			continue;
		/* "-SIGNAL" option found. Remove it and bail out */
		killsig = opt;
		do {
			pp[0] = arg = pp[1];
			pp++;
		} while (arg);
		break;
	}

	opt = getopt32(argv, OPTION_STRING);
	argv += optind;

	ilist = NULL;
	pp = argv;
	while (*pp) {
		char *proto = parse_net_arg(*pp, &port);
		if (proto) { /* PORT/PROTO */
			ilist = scan_proc_net(proto, port, ilist);
			free(proto);
		} else { /* FILE */
			if (!file_to_dev_inode(*pp, &dev, &inode))
				bb_perror_msg_and_die("can't open %s", *pp);
			ilist = add_inode(ilist, dev, inode);
		}
		pp++;
	}

	plist = scan_proc_pids(ilist); /* changes dir to "/proc" */

	if (!plist)
		return EXIT_FAILURE;
	success = 1;
	if (opt & OPT_KILL) {
		success = kill_pid_list(plist, killsig);
	} else if (!(opt & OPT_SILENT)) {
		success = print_pid_list(plist);
	}
	return (success != 1); /* 0 == success */
}
Beispiel #2
0
int main() {
        printf("Variante %d: %s\n", VARIANTE, VARIANTE_STRING);

#ifdef USE_GUILE
        scm_init_guile();
        /* register "executer" function in scheme */
        scm_c_define_gsubr("executer", 1, 0, 0, executer_wrapper);
#endif

		pidTable = create_pid_list();

		struct sigaction act;
		memset(&act, '\0', sizeof(act));
		act.sa_sigaction = &terminationHandler;
		act.sa_flags = SA_SIGINFO | SA_NOCLDSTOP;
		if(sigaction(SIGCHLD, &act, NULL) == -1){
			perror("sigaction");
		}

	while (1) {
		char *line=0;
		char *prompt = "ensishell>";

		/* Readline use some internal memory structure that
		   can not be cleaned at the end of the program. Thus
		   one memory leak per command seems unavoidable yet */
		line = readline(prompt);
		if (line == 0 || ! strncmp(line,"exit", 4)) {
			terminate(line);
		}
		else if(!strncmp(line, "jobs", 4)){
			clean_pid_list(&pidTable);
			print_pid_list(pidTable);
#ifdef USE_GNU_READLINE
		add_history(line);
#endif
			continue;
		}

#ifdef USE_GNU_READLINE
		add_history(line);
#endif


#ifdef USE_GUILE
		/* The line is a scheme command */
		if (line[0] == '(') {
			char catchligne[strlen(line) + 256];
			sprintf(catchligne, "(catch #t (lambda () %s) (lambda (key . parameters) (display \"mauvaise expression/bug en scheme\n\")))", line);
			scm_eval_string(scm_from_locale_string(catchligne));
			free(line);
                        continue;
                }
#endif

		executer(line);
//		struct cmdline *l;
//		int i, j;
//		/* parsecmd free line and set it up to 0 */
//		l = parsecmd( & line);
//
//		/* If input stream closed, normal termination */
//		if (!l) {
//		  
//			terminate(0);
//		}
//		
//
//		
//		if (l->err) {
//			/* Syntax error, read another command */
//			printf("error: %s\n", l->err);
//			continue;
//		}
//
//		if (l->in) printf("in: %s\n", l->in);
//		if (l->out) printf("out: %s\n", l->out);
//		if (l->bg) printf("background (&)\n");
//
//		/* Display each command of the pipe */
//		for (i=0; l->seq[i]!=0; i++) {
//			char **cmd = l->seq[i];
//			printf("seq[%d]: ", i);
//                        for (j=0; cmd[j]!=0; j++) {
//                                printf("'%s' ", cmd[j]);
//                        }
//			printf("\n");
//		}
	}

}