getgroups

#include < sys/types.h >
#include < unistd.h >
#include < grp.h >

#include 

int main(){
    gid_t list[3];
    
    list[0] = 3000;
    list[1] = 3001;
    list[2] = 3002;
    if (setgroups(3, list) < 0){
        perror("setgroups");
        return 1;
    }
    return 0;
}

getgroups

#include < sys/types.h >
#include < unistd.h >

#include < limits.h >
#include < stdio.h >

int main()
{
    int i;
    int groups;
    gid_t list[NGROUPS_MAX];
    
    if((groups = getgroups(NGROUPS_MAX, list))< 0){
        perror("getgroups");
        return 1;
    }
    
    for (i = 0; i < groups; i++){
        printf("%d", (int)list[i]);
    }
    putchar('\n');
    return 0;
}

setid

forkした後に親プロセス側は終了し、子プロセスがsetidを実行して、新しいセッションを作成。

#include 

#include 
#include 

int main()
{
    pid_t pid;
    
    if ((pid = fork()) < 0){
        perror("fork");
        return 1;
    } else if(pid > 0){
        _exit(0);
    }
    
    if (setsid() < 0){
        perror("setsid");
        return 1;
    }
    
    return 0;
}

getpgid

自分自身のプロセスループIDを取得し、それを表示

#define _XOPEN_SOURCE 500
#include < unistd.h >

#include < stdio.h >

int main()
{
    pid_t pgid;
    
    if ((pgid = getpgid(0)) < 0){
        perror("getpgid");
        return 1;
    }
    printf("pgid = %d\n", (int)pgid);
    return 0;
}

seteuid

#include < sys/types.h >
#include < unistd.h >

#include < stdio.h >

static void
printuid()
{
    printf("uid = %d euid = %d\n",
           (int)getuid(), (int)geteuid());
}

int main() {
    uid_t uid, euid;
    
    uid = getuid();
    euid = geteuid();
    
    printuid();
    
    if (seteuid(uid) < 0){
        perror("seteuid");
        return 1;
    }
    printuid();
    
    if (seteuid(euid)<0){
        perror("seteuid");
        return 1;
    }
    printuid();
    
    return 0;
}

プロセスの実ユーザID

getuid/geteuid/getgid/getegidは、それぞれ自分のプロセスの実ユーザID、実行ユーザID、実グループID、実行グループIDを取得します。

#include < unistd.h >
#include < sys/types.h >

#include < stdio.h >

int main()
{
    uid_t uid, euid;
    gid_t gid, egid;
    
    uid = getuid();
    euid = geteuid();
    gid = getgid();
    egid = getegid();
    
    printf("uid = %d, euid = %d, gid = %d, egid = %d\n",
           (int)uid, (int)euid, (int)gid, (int)egid);
    
    return 0;
}

wait4プログラム

wait3に引数pidが増えただけです。

#include < sys/types.h >
#include < sys/time.h >
#include < sys/resource.h >
#include < sys/wait.h >

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

int
main()
{
    int status;
    pid_t pid;
    struct rusage usage;
    
    if ((pid = fork()) < 0){
        perror("fork");
        return 1;
    } else if (pid == 0){
        write(1, "chlid process\n", 14);
        _exit(12);
    }
    write(1, "parent process\n", 15);
    
    if ((pid = wait4(pid, status, 0, &usage)) < 0){
        perror("wait4");
        return 1;
    }
    if(WIFEXITED(status)){
        printf(
        "pid = %d exited with status = %d\n",
               (int)pid, WEXITSTATUS(status));
    }
    
    printf(
    "user time = %ld.%06ld\n"
    "system time = %ld.%06ld\n",
    usage.ru_utime.tv_sec, usage.ru_utime.tv_usec,
           usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
    return 0;
}

wait3プログラム

#include < sys/types.h >
#include < sys/time.h >
#include < sys/resource.h >
#include < sys/wait.h >

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

int main()
{
    int status;
    pid_t pid;
    struct rusage usage;
    
    if ((pid = fork())< 0){
        perror("fork");
        return 1;
    } else if (pid == 0){
        write(1, "child process\n", 14);
        _exit(12);
    }
    write(1, "parent process\n", 15);
    
    if ((pid = wait3(&status, 0, &usage)) < 0) {
        perror("wait3");
        return 1;
    }
        if(WIFEXITED(status)){
            printf(
            "pid=%d exited with status = %d\n",
                   (int)pid, WEXITSTATUS(status));
        }
    printf(
    "user time = %ld.%06ld\n"
           "system time = %ld.%06ld\n",
           usage.ru_utime.tv_sec, usage.ru_utime.tv_usec,
           usage.ru_stime.tv_sec, usage.ru_stime.tv_usec);
    return 0;
        
}