Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用execvp()执行流程时加载用户环境;_C++_Linux_Fork_Su_Execvp - Fatal编程技术网

C++ 使用execvp()执行流程时加载用户环境;

C++ 使用execvp()执行流程时加载用户环境;,c++,linux,fork,su,execvp,C++,Linux,Fork,Su,Execvp,我有一个守护进程正在运行,它可能会在输入连接时产生一个进程。这是使用execvp()和fork()完成的 问题是该进程不应以root用户身份运行,并且该进程依赖于正确的用户环境。因此,我正在寻找一种加载用户环境的方法 当前,通过设置gid和uid,该进程作为另一个用户执行。我已经尝试过的是使用as commandsu-l user-c command,但由于某种原因,它不起作用 有人知道如何加载用户环境(尤其是$HOME变量)吗 提前感谢。具有附加选项-H,用于设置$HOME变量。所以你可以试试

我有一个守护进程正在运行,它可能会在输入连接时产生一个进程。这是使用
execvp()
fork()
完成的

问题是该进程不应以root用户身份运行,并且该进程依赖于正确的用户环境。因此,我正在寻找一种加载用户环境的方法

当前,通过设置gid和uid,该进程作为另一个用户执行。我已经尝试过的是使用as command
su-l user-c command
,但由于某种原因,它不起作用

有人知道如何加载用户环境(尤其是
$HOME
变量)吗

提前感谢。

具有附加选项
-H
,用于设置
$HOME
变量。所以你可以试试

sudo -H -u user command
代替您的
su
命令行。

具有附加选项
-H
,该选项设置
$HOME
变量。所以你可以试试

sudo -H -u user command

代替您的
su
命令行。

您可以这样做:

#include <cstring>
#include <iostream>
#include <vector>

#include <stdio.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

// Version 0 or 1
#ifndef VERSION
#define VERSION 1
#endif

// Please compile twise TEST_PROCESS = 0 and TEST_PROCESS = 1
#ifndef TEST_PROCESS
#define TEST_PROCESS 0
#endif

// User for the process started.
#ifndef USER
#error Please define a user (just a name, no string).
#endif

#define MAKE_STR(x) DO_MAKE_STR(x)
#define DO_MAKE_STR(x) #x
#define USER_NAME MAKE_STR(USER)

class UserEnvironment
{
    public:
    typedef std::vector<char*> Values;

    UserEnvironment(const char* user) {
        auto cmd = std::string("su - ") + user + " -c '. ~/.profile; env'";
        FILE* fp = popen(cmd.c_str(), "r");
        if(fp == 0)  perror(0);
        else {
            char* line = 0;
            size_t len = 0;
            ssize_t n = 0;
            while(1 < (n = getline(&line, &len, fp))) {
                line[n - 1] = 0;
                m_values.push_back(line);
                line = 0;
            }
            pclose(fp);
        }
        m_values.push_back(0);
    };

    ~UserEnvironment() {
        m_values.pop_back();
        for(char* p: m_values) free(p);
    }

    const Values& values() const { return m_values; }
    Values& values() { return m_values; }
    operator const Values& () const { return m_values; }
    operator Values& () { return m_values; }
    char* const * data() const { return m_values.data(); }
    char** data() { return m_values.data(); }

    private:
    Values m_values;
};

inline const char* get_home() {
    const char* home = getenv("HOME");
    if(home == 0) home = "[Missing]";
    return home;
}

#if ! TEST_PROCESS
// Debug/Test
int main() {

    const char* user = USER_NAME;

    int pid = fork();
    if(pid < 0) return 1;
    else if(pid) {
        const char* home = get_home();
        std::cout << "Running Demon: Home = " << home
            << ", User ID = " << getuid() << std::endl;
        wait(NULL);
    }
    else {
        struct passwd* pw = getpwnam(user);
        if( ! pw) {
            std::cout << "Invalid User [" << user << ']'<< std::endl;
        }
        else {
            char* process = strdup("Debug/TestProcess");
            char *const argv[] = {
                process,
                NULL
            };
        #if VERSION == 0
            // Oberwrite environment variables.
            // You might call clearenv();
            if(setgid(pw->pw_gid) == 0 && setuid(pw->pw_uid) == 0) {
                setenv("HOME", pw->pw_dir, 1);
                execvp(process, argv);
            }
        #else
            // Use the user environt.
            UserEnvironment environment(user);
            if(setgid(pw->pw_gid) == 0 && setuid(pw->pw_uid) == 0) {
                execvpe(process, argv, environment.data());
            }
        #endif
            free(process);
        }
    }

    const char* home = get_home();
    std::cout << "Exit: Home = " << home << ", User ID = " << getuid() << std::endl;
    return 0;
}

#else

// Debug/TestProcess
int main() {
    const char* home = get_home();
    std::cout
         << "Running Process: Home = " << home
         << ", User ID = " << getuid()
         << std::endl;
    system("env");
    exit(0);
}

#endif

您可以这样做:

#include <cstring>
#include <iostream>
#include <vector>

#include <stdio.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

// Version 0 or 1
#ifndef VERSION
#define VERSION 1
#endif

// Please compile twise TEST_PROCESS = 0 and TEST_PROCESS = 1
#ifndef TEST_PROCESS
#define TEST_PROCESS 0
#endif

// User for the process started.
#ifndef USER
#error Please define a user (just a name, no string).
#endif

#define MAKE_STR(x) DO_MAKE_STR(x)
#define DO_MAKE_STR(x) #x
#define USER_NAME MAKE_STR(USER)

class UserEnvironment
{
    public:
    typedef std::vector<char*> Values;

    UserEnvironment(const char* user) {
        auto cmd = std::string("su - ") + user + " -c '. ~/.profile; env'";
        FILE* fp = popen(cmd.c_str(), "r");
        if(fp == 0)  perror(0);
        else {
            char* line = 0;
            size_t len = 0;
            ssize_t n = 0;
            while(1 < (n = getline(&line, &len, fp))) {
                line[n - 1] = 0;
                m_values.push_back(line);
                line = 0;
            }
            pclose(fp);
        }
        m_values.push_back(0);
    };

    ~UserEnvironment() {
        m_values.pop_back();
        for(char* p: m_values) free(p);
    }

    const Values& values() const { return m_values; }
    Values& values() { return m_values; }
    operator const Values& () const { return m_values; }
    operator Values& () { return m_values; }
    char* const * data() const { return m_values.data(); }
    char** data() { return m_values.data(); }

    private:
    Values m_values;
};

inline const char* get_home() {
    const char* home = getenv("HOME");
    if(home == 0) home = "[Missing]";
    return home;
}

#if ! TEST_PROCESS
// Debug/Test
int main() {

    const char* user = USER_NAME;

    int pid = fork();
    if(pid < 0) return 1;
    else if(pid) {
        const char* home = get_home();
        std::cout << "Running Demon: Home = " << home
            << ", User ID = " << getuid() << std::endl;
        wait(NULL);
    }
    else {
        struct passwd* pw = getpwnam(user);
        if( ! pw) {
            std::cout << "Invalid User [" << user << ']'<< std::endl;
        }
        else {
            char* process = strdup("Debug/TestProcess");
            char *const argv[] = {
                process,
                NULL
            };
        #if VERSION == 0
            // Oberwrite environment variables.
            // You might call clearenv();
            if(setgid(pw->pw_gid) == 0 && setuid(pw->pw_uid) == 0) {
                setenv("HOME", pw->pw_dir, 1);
                execvp(process, argv);
            }
        #else
            // Use the user environt.
            UserEnvironment environment(user);
            if(setgid(pw->pw_gid) == 0 && setuid(pw->pw_uid) == 0) {
                execvpe(process, argv, environment.data());
            }
        #endif
            free(process);
        }
    }

    const char* home = get_home();
    std::cout << "Exit: Home = " << home << ", User ID = " << getuid() << std::endl;
    return 0;
}

#else

// Debug/TestProcess
int main() {
    const char* home = get_home();
    std::cout
         << "Running Process: Home = " << home
         << ", User ID = " << getuid()
         << std::endl;
    system("env");
    exit(0);
}

#endif

我没有在目标系统上安装sudo(也不应该安装)。还有别的路吗?通常情况下,su-l user-c命令方法应该可以工作,或者我在这里遗漏了什么吗?我没有在目标系统上安装sudo(并且它不应该被安装)。还有别的路吗?通常情况下,su-l user-c命令方法应该可以工作,还是我在这里遗漏了什么?