小编Dom*_*ski的帖子

Linux C编程以用户身份执行

我有一个以root身份运行的程序.我希望该程序作为普通用户执行另一个应用程序.我尝试过setgid()它可以工作,但我不能再回到root用户或其他用户.目前的计划非常简单;

 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>

 int main(int argc, char *argv[] )
 {
     if ( argc != 2) {
         printf("usage: %s command\n",argv[0]);
         exit(1);
     }
     setgid(100);
     setuid(1000);
     putenv("HOME=/home/caroline");
     putenv("DISPLAY=:0");
     system(argv[1]);
     seteuid(1001);
     putenv("HOME=/home/john");
     putenv("DISPLAY=:1");
     system(argv[1]);
     return 0;
 }
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?这就像命令的行动su $user-c $command

c linux setuid su

14
推荐指数
1
解决办法
5599
查看次数

Linux登录和strcmp

我正在为Linux编写一个简单的程序.我有一个功能"addonlyonce"的问题.它比较字符串并添加到没有重复的数组(如果字符串不存在于数组中).该函数运行良好,但不适用于utmp结构.当比较utmp结构的每个字符串总是返回1(这意味着字符串已经存在于数组中 - 并且它是错误的:().您可以编译此代码gcc thiscode.c -o test(它仅适用于Linux).

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <utmp.h>

int addonlyonce(char **array, char *str) {
    int i=0;
    printf("I got %s\n",str);
    while(*array != '\0') {
        if(strcmp(*array,str)==0) {
            printf("Already exists!\n");
            return 1;
        }
        i++;
        array++;
    }
    *array=str;
    printf("Sit down on the number: %d\n",i);
    return 0;
}

void printarray(char **array) {
    printf("Array looks like: ");
    while(*array != '\0') {
        printf("%s ",*array);
        array++;
    }
    printf("\n");
}

int main(void) {
    char *users[20]={0};
        char exuser2[]="exuser2";
    struct utmp current_record;
    FILE …
Run Code Online (Sandbox Code Playgroud)

c linux compare strcmp

1
推荐指数
1
解决办法
127
查看次数

标签 统计

c ×2

linux ×2

compare ×1

setuid ×1

strcmp ×1

su ×1