-
C/C++ seteuid(2), setegid(2)C,C++ & Linux 2020. 6. 5. 04:31728x90
함수 기능
현재 프로세스의 Effective User ID 혹은 Effective Group ID 를 수정하는 함수입니다.
함수 원형
#include <unistd.h> #include <sys/types.h> int seteuid(uid_t euid); int setegid(gid_t egid);
매개변수
설정하고자 하는 euid, egid
반환값
성공 시 0 리턴
에러 시 -1 리턴하고 errno 설정
예제
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> int main(int argc, char *argv[]) { int fd, state; // euid를 id -u 명령어를 통해 자신의 uid 를 알아내고 그 번호를 등록 state = seteuid(501); if (state < 0) { fprintf(stderr, "seteuid error\n"); exit(1); } if ((fd = open("ssu_test.txt", O_CREAT | O_RDWR, S_IRWXU)) < 0) { fprintf(stderr, "open error\n"); exit(1); } close(fd); exit(1); }
결과
첫 번째 결과는 euid(501)을 지우고 돌린 결과입니다. 소유자가 root 로 나타나는 모습
두 번째 결과는 euid(501)을 지우지 않고 돌린 결과입니다. sudo 명령어를 통해 실행했지만 euid 가 501이기에 파일의 소유자가 dongkyoo로 뜨는 모습
리눅스시스템프로그래밍 저자 : 홍지만
https://book.naver.com/bookdb/book_detail.nhn?bid=14623672
책에 기술된 예제 프로그램입니다. 책 내부에는 훨씬 더 많은 자료가 있습니다. (개인적으로 좋았습니다.)'C,C++ & Linux' 카테고리의 다른 글
top 명령어 정리 (0) 2020.09.09 C/C++ setreuid(2), setregid(2) (0) 2020.06.05 C/C++ setuid(2), setgid(2) (0) 2020.06.04 C/C++ wait3(2), wait4(2) (0) 2020.06.04 C/C++ execl(3), execv(3), execle(3), execve(2), execlp(3), execvp(3) (0) 2020.06.04