<commit_before>/*
 * Copyright (c) 2018 <NAME>. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * lgetty - A minimal getty that respawns itself
 */

#include "inc.h"

static int usage(void)
{
	printf("Usage: %s tty ...\n", __progname);
	return 1;
}

int main(int argc, char *argv[])
{
	// An argument is required
	if(argc < 2)
		return usage();

	// Find login(1)
	char login_path[19];
	if(access("/bin/login", X_OK) == 0)
		memcpy(login_path, "/bin/login ", 12);
	else if(access("/usr/bin/login", X_OK) == 0)
		memcpy(login_path, "/usr/bin/login ", 16);
	else {
		printf(RED "* Could not find login(1) (please symlink it to either /bin/login or /usr/bin/login and give it executable permissions)" RESET "\n");
		return 127;
	}

	// Infinite loop
	int status;
	for(;;) {

		// Child process for login(1)
		pid_t login = fork();
		if(login == 0) {

			// This is required for the subshell to have job control on FreeBSD
#			ifdef FreeBSD
			revoke(argv[1]);
#			endif

			// Verify the tty exists
			int tty = open(argv[1], O_RDWR | O_NOCTTY);
			if(tty == -1 || isatty(tty) == 0)
				return usage();

			// Set the tty as the controlling terminal
			login_tty(tty);
			dup2(tty, STDIN_FILENO);
			dup2(tty, STDOUT_FILENO);
			dup2(tty, STDERR_FILENO);
			ioctl(tty, TIOCSCTTY, 1);

			// Get user input (for logging in)
			char user[100];
			printf(CYAN "\n* " WHITE "%s login:" RESET " ", argv[1]);
			scanf("%s", user);

			// Execute login(1)
			char *sargv[] = { "login", user, NULL };
			return execve(login_path, sargv, environ);
		}

		// Prevent spamming
		waitpid(login, &status, 0);
		if(WEXITSTATUS(status) != 0) {
			printf(RED "* login(1) exited with a return status of %d" RESET "\n", WEXITSTATUS(status));
			return status;
		}
	}
}<commit_msg>Properly return when the child process fails<commit_after>/*
 * Copyright (c) 2018 <NAME>. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * lgetty - A minimal getty that respawns itself
 */

#include "inc.h"

static int usage(int ret)
{
	printf("Usage: %s tty ...\n", __progname);
	return ret;
}

int main(int argc, char *argv[])
{
	// An argument is required
	if(argc < 2)
		return usage(1);

	// Find login(1)
	char login_path[16];
	if(access("/bin/login", X_OK) == 0)
		memcpy(login_path, "/bin/login ", 12);
	else if(access("/usr/bin/login", X_OK) == 0)
		memcpy(login_path, "/usr/bin/login ", 16);
	else {
		printf(RED "* Could not find login(1) (please symlink it to either /bin/login or /usr/bin/login and give it executable permissions)" RESET "\n");
		return 127;
	}

	// Infinite loop
	int status;
	for(;;) {

		// Child process for login(1)
		pid_t login = fork();
		if(login == 0) {

			// This is required for the subshell to have job control on FreeBSD
#			ifdef FreeBSD
			revoke(argv[1]);
#			endif

			// Verify the tty exists
			int tty = open(argv[1], O_RDWR | O_NOCTTY);
			if(isatty(tty) == 0)
				return usage(127);

			// Set the tty as the controlling terminal
			login_tty(tty);
			dup2(tty, STDIN_FILENO);
			dup2(tty, STDOUT_FILENO);
			dup2(tty, STDERR_FILENO);
			ioctl(tty, TIOCSCTTY, 1);

			// Get user input (for logging in)
			char user[100];
			printf(CYAN "\n* " WHITE "%s login:" RESET " ", argv[1]);
			scanf("%s", user);

			// Execute login(1)
			char *sargv[] = { "login", user, NULL };
			return execve(login_path, sargv, environ);
		}

		// Prevent spamming
		waitpid(login, &status, 0);
		if(WEXITSTATUS(status) != 0) {
			printf(RED "* The LGetty child process exited with a return status of %d" RESET "\n", WEXITSTATUS(status));
			return WEXITSTATUS(status);
		}
	}
}
{'fraction_non_alphanumeric': 0.11796765119549929, 'fraction_numerical': 0.013185654008438819, 'mean_word_length': 3.5731511254019295, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1141022', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1791, 'n_words': 714}