Esempio n. 1
0
void	check_first(char **argv, char **envp, pid_t pid, t_shell *shell)
{
  int	i;

  i = 0;
    if (shell->buffer[0] == 'l' && shell->buffer[1] == 's'
	&& shell->buffer[3] == ' ' && shell->buffer[4] == '\0')
    {
      execve(path_dir("/bin/", "ls"), argv, NULL);
      exit(1);
    }
  if (my_strncmp(shell->buffer, "cd", 2) == 0)
    {
      i = chdir(shell->reader[1]);
      exit(1);
    }
  else if (my_strcmp(shell->buffer, "exit") == 0)
    {
      kill(pid, 1);
      exit(1);
    }
  else if (my_strcmp(shell->buffer, "env") == 0)
    show_env(envp);
  else if (my_strcmp(shell->buffer, "clear") == 0)
    clear_window();
}
Esempio n. 2
0
int	check_builtins(t_main *list, t_main **env)
{
  if (strcmp(list->str, "cd") == 0)
    my_cd(list, *env);
  else if (strcmp(list->str, "env") == 0)
    show_env(*env);
  else if (strcmp(list->str, "setenv") == 0)
    my_setenv(list, env);
  else if (strcmp(list->str, "unsetenv") == 0)
    my_unsetenv(list, env);
  else if (strcmp(list->str, "exit") == 0)
    my_exit(list);
  else if (strcmp(list->str, "echo") == 0)
    my_echo(list);
  else if (strcmp(list->str, "..") == 0)
    my_cd(manage_double_dots(&list), *env);
  else
    return (0);
  return (1);
}
Esempio n. 3
0
void	execute(t_env *env)
{
	if (ft_strcmp(env->com, "exit") == 0 || ft_strcmp(env->com, "quit") == 0)
		env->done = 1;
	else if (ft_strcmp(env->com, "clear") == 0)
		ft_printf("\033c");
	else if (ft_strcmp(env->com, "echo") == 0)
		echo(env);
	else if (ft_strcmp(env->com, "env") == 0)
		show_env(env->env_var);
	else if (ft_strcmp(env->com, "setenv") == 0)
		ft_setenv(env);
	else if (ft_strcmp(env->com, "unsetenv") == 0)
		ft_unsetenv(env);
	else if (ft_strcmp(env->com, "cd") == 0)
		ft_cd(env);
	else if (ft_strcmp(env->com, "pwd") == 0)
		ft_pwd(env);
	else
		ft_run(env);
}
Esempio n. 4
0
File: main.cpp Progetto: DPMI/marcd
int main(int argc, char *argv[]){
	/* extract program name from path. e.g. /path/to/MArCd -> MArCd */
	const char* separator = strrchr(argv[0], '/');
	if ( separator ){
		program_name = separator + 1;
	} else {
		program_name = argv[0];
	}

	default_env();

	int ret;
	int option_index = 0;
	int op;

#ifdef HAVE_INIPARSER_H
	if ( config::load(argc, argv) != 0 ){ /* passing argv so it can read -f/--config */
		return 1; /* error already shown */
	}
#endif

	while ( (op = getopt_long(argc, argv, shortopts, longopts, &option_index)) != -1 ){
		switch (op){
		case '?': /* error */
			exit(1);

		case 0: /* long opt */
			break;

		case 'b': /* --daemon */
			daemon_mode = 1;
			break;

		case 's': /* --syslog */
			syslog_flag = true;
			break;

		case FLAG_USER: /* --user */
			config::set_drop_username(optarg);
			break;

		case FLAG_GROUP: /* --group */
			config::set_drop_group(optarg);
			break;

		case FLAG_DROP_PRIV: /** --drop */
			drop_priv_flag = true;
			break;

		case FLAG_NODROP_PRIV: /** --no-drop */
			drop_priv_flag = false;
			break;

		case 'f':
#ifndef HAVE_INIPARSER_H
			fprintf(stderr, "%s: configuration files not supported (build with --with-iniparser)\n", program_name);
#endif
			break;

		case 'H':
			strncpy(db_hostname, optarg, sizeof(db_hostname));
			db_hostname[sizeof(db_hostname)-1] = '\0';
			break;

		case 'N':
			strncpy(db_name, optarg, sizeof(db_name));
			db_name[sizeof(db_name)-1] = '\0';
			break;

		case 'u':
			strncpy(db_username, optarg, sizeof(db_username));
			db_username[sizeof(db_username)-1] = '\0';
			break;

		case 'p':
			if ( strcmp(optarg, "-") == 0 ){ /* read password from stdin */
				strncpy(db_password, getpass("mysql password: "******"Invalid port given to --relay: %s. Ignored\n", optarg);
				}
			}

			break;

		case FLAG_DATADIR:
			free(rrdpath);
			rrdpath = strdup(optarg);
			break;

		case FLAG_PIDFILE:
			pidfile = optarg;
			break;

		case 'v': /* --verbose */
			verbose_flag = 1;
			break;

		case 'q': /* --quiet */
			verbose_flag = 0;
			break;

		case 'd': /* --debug */
			debug_flag = 1;
			break;

		case 'h': /* --help */
			show_usage();
			exit(0);

		default:
			if ( option_index >= 0 ){
				fprintf(stderr, "flag --%s declared but not handled\n", longopts[option_index].name);
			} else {
				fprintf(stderr, "flag -%c declared but not handled\n", op);
			}
			abort();
		}
	}

	/* database */
	if ( argc > optind ){
		strncpy(db_name, argv[optind], sizeof(db_name));
		db_name[sizeof(db_name)-1] = '\0';
	}

	setup_output();

	/* test if possible to drop privileges */
	if ( drop_priv_flag && getuid() != 0 ){
		Log::message(MAIN, "Not executing as uid=0, cannot drop privileges.\n");
		drop_priv_flag = 0;
	}

	/* Drop privileges.
	 * Done before forking since unlinking requires write permission to folder so
	 * if it fails to write it will fail unlinking. It is also done before
	 * check_env so it actually check environment for the dropped user instead of
	 * root.
	 */
	if ( drop_priv_flag ){
		privilege_drop();
	}

	/* sanity checks */
	show_env();
	if ( !check_env() ){
		return 1;
	}

	if ( daemon_mode ){
		/* opening file before fork since it will be a fatal error if it fails to write the pid */
		FILE* fp = fopen(pidfile, "w");
		if ( !fp ){
			Log::fatal(MAIN, "failed to open '%s` for writing: %s\n", pidfile, strerror(errno));
			return 1;
		}

		Log::message(MAIN, "Forking to background\n");
		pid_t pid = fork();

		if ( pid ){ /* parent */
			fprintf(fp, "%d\n", pid);
			fclose(fp);

			/* change owner of pidfile */
			if ( drop_priv_flag ){
				if ( chown(pidfile, drop_uid, drop_gid) != 0 ){
					Log::error("failed to change owner of '%s`, will probably fail to unlink it later: %s\n", pidfile, strerror(errno));
				}
			}

			return 0;
		}

		fclose(fp);
	}

	/* initialize daemons */
	pthread_barrier_t barrier;
	int threads = 1;
	threads += (int)have_control_daemon;
	threads += (int)have_relay_daemon;
	pthread_barrier_init(&barrier, NULL, threads);
	control.addr = relay.addr;

	if ( have_control_daemon && (ret=Daemon::instantiate<Control>(2000, &barrier)) != 0 ){
		Log::fatal(MAIN, "Failed to initialize control daemon, terminating.\n");
		if ( daemon_mode && unlink(pidfile) == -1 ){
			Log::fatal(MAIN, "Failed to remove pidfile: %s\n", strerror(errno));
		}
		return ret;
	}

	if ( have_relay_daemon && (ret=Daemon::instantiate<Relay>(2000, &barrier)) != 0 ){
		Log::fatal(MAIN, "Failed to initialize relay daemon, terminating.\n");
		if ( daemon_mode && unlink(pidfile) == -1 ){
			Log::fatal(MAIN, "Failed to remove pidfile: %s\n", strerror(errno));
		}
		return ret;
	}

	/* install signal handler */
	signal(SIGINT, handle_signal);
	signal(SIGTERM, handle_signal);

	/* release all threads and wait for them to finish*/
	pthread_barrier_wait(&barrier);
	if ( daemon_mode ){
		Log::message(MAIN, "Threads started. Going to sleep.\n");
	} else {
		Log::message(MAIN, "Threads started. Going to sleep. Abort with SIGINT\n");
	}
	Daemon::join_all();

	/* cleanup */
	free(rrdpath);
	if ( daemon_mode && unlink(pidfile) == -1 ){
		Log::fatal(MAIN, "Failed to remove pidfile: %s\n", strerror(errno));
	}

	Log::message(MAIN, "%s terminated.\n", program_name);
	return 0;
}