exec.cc

back


/*
 BSD 3-Clause License
 
 Copyright (c) 2024, k4m1 <me@k4m1.net>
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
 
 1. Redistributions of source code must retain the above copyright notice, this
    list of conditions and the following disclaimer.
 
 2. Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
 
 3. Neither the name of the copyright holder nor the names of its
    contributors may be used to endorse or promote products derived from
    this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/wait.h>

#include <iostream>
#include <string.h>
#include <stdexcept>
#include <unistd.h>

#include <built_in.h>
#include <exec.h>

/* Run a command given to us by user. We'll handle
 * resolving command type, and then call appropriate
 * exec_... function.
 *
 * @param command_handler::command cmd -- Command to run
 * @return int exit status of command
 */
int command_handler::run(command_handler::command& cmd) {
    int ret = 127;
    std::string path = "";
    command_type type = get_command_type(cmd.argument_vector[0]);
    switch (type) {
    case (BUILT_IN):
        ret = built_in::run(cmd.argument_vector, this->variable_handler);
        break;
    case (PATH_TO_BE_RESOLVED):
        path = get_path_to_program(cmd.argument_vector[0]);
        if (path != "") {
            ret = command_handler::exec(path, cmd);
        }
        break;
    case (PATH_GIVEN):
        ret = command_handler::exec(cmd.argument_vector[0], cmd);
        break;
    }
    return ret;
}

int command_handler::exec(std::string& prog, command& cmd) {
    int ret;
    int argc = cmd.argument_vector.size();
    char **argv = (char **)calloc(argc + 1, sizeof(char **));
    if (!argv) {
        throw std::runtime_error("Out of memory");
    }
    for (int i = 0; i < argc; i++) {
        argv[i] = (char *)calloc(cmd.argument_vector[i].length() + 1, sizeof(char *));
        if (!argv[i]) {
            throw std::runtime_error("Out of memory");
        }
        strcpy(argv[i], cmd.argument_vector[i].c_str());
    }
    pid_t pid = fork();

    switch (pid) {
    case (-1):
        std::cerr << "Failed to fork!" << std::endl;
        break;
    case (0):
        ret = execv(prog.c_str(), argv);
        exit(ret);
    default:
        if (waitpid(pid, &ret, 0) == -1) {
            std::cerr << "Unable to wait for child to exit" << std::endl;
        }
    }
    for (int i = 0; i < argc; i++) {
        free(argv[i]);
    }
    free(argv);
    return ret;
}