Path: blob/master/tools/testing/selftests/exec/recursion-depth.c
26285 views
/*1* Copyright (c) 2019 Alexey Dobriyan <[email protected]>2*3* Permission to use, copy, modify, and distribute this software for any4* purpose with or without fee is hereby granted, provided that the above5* copyright notice and this permission notice appear in all copies.6*7* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES8* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF9* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR10* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES11* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN12* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF13* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.14*/15/* Test that pointing #! script interpreter to self doesn't recurse. */16#include <errno.h>17#include <sched.h>18#include <stdio.h>19#include <string.h>20#include <sys/types.h>21#include <sys/stat.h>22#include <fcntl.h>23#include <sys/mount.h>24#include <unistd.h>25#include "../kselftest.h"2627int main(void)28{29int fd, rv;3031ksft_print_header();32ksft_set_plan(1);3334if (unshare(CLONE_NEWNS) == -1) {35if (errno == ENOSYS || errno == EPERM) {36ksft_test_result_skip("error: unshare, errno %d\n", errno);37ksft_finished();38}39ksft_exit_fail_perror("error: unshare");40}4142if (mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) == -1)43ksft_exit_fail_perror("error: mount '/'");4445/* Require "exec" filesystem. */46if (mount(NULL, "/tmp", "ramfs", 0, NULL) == -1)47ksft_exit_fail_perror("error: mount ramfs");4849#define FILENAME "/tmp/1"5051fd = creat(FILENAME, 0700);52if (fd == -1)53ksft_exit_fail_perror("error: creat");5455#define S "#!" FILENAME "\n"56if (write(fd, S, strlen(S)) != strlen(S))57ksft_exit_fail_perror("error: write");5859close(fd);6061rv = execve(FILENAME, NULL, NULL);62ksft_test_result(rv == -1 && errno == ELOOP,63"execve failed as expected (ret %d, errno %d)\n", rv, errno);64ksft_finished();65}666768