#include < sys/types.h > #include < sys/stat.h > #include < fcntl.h > #include < stdio.h > int main() { int fd_r, fd_w; if ((fd_r = open("/etc/hosts", O_RDONLY)) < 0){ perror("/etc/hosts"); return 1; } if ((fd_w = open("file.txt", O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0){ perror("file.txt"); return 1; } return 0; }
Month: June 2016
setrlimit
#include < sys/time.h > #include < sys/resource.h > #include < unistd.h > #include < stdio.h > int main() { struct rlimit rlim; rlim.rlim_cur = 1; rlim.rlim_max = 2; if (setrlimit(RLIMIT_CPU, &rlim) < 0){ perror("setrlimit"); return 1; } for(;;){ chdir("."); } return 0; }
libsystem_kernel.dylib`chdir:
0x7fff8cd39b38 <+0>: movl $0x200000c, %eax
0x7fff8cd39b3d <+5>: movq %rcx, %r10
0x7fff8cd39b40 <+8>: syscall
-> 0x7fff8cd39b42 <+10>: jae 0x7fff8cd39b4c ; <+20>
0x7fff8cd39b44 <+12>: movq %rax, %rdi
0x7fff8cd39b47 <+15>: jmp 0x7fff8cd34c53 ; cerror_nocancel
0x7fff8cd39b4c <+20>: retq
0x7fff8cd39b4d <+21>: nop
0x7fff8cd39b4e <+22>: nop
0x7fff8cd39b4f <+23>: nop
getrlimit
cpu時間のリミットを取得
#include < sys/time.h > #include < sys/resource.h > #includeint main(){ struct rlimit rlim; if (getrlimit(RLIMIT_CPU, &rlim) < 0){ perror("getrlimit"); return 1; } if (rlim.rlim_cur == RLIM_INFINITY){ printf("rlim_cur = unlimited\n"); } else { printf("rlim_cur = %lld\n", (long long)rlim.rlim_cur); } if(rlim.rlim_max == RLIM_INFINITY){ printf("rlim_max = unlimited\n"); } else { printf("rlim_max = %lld\n", (long long)rlim.rlim_max); } return 0; }
getrusage
#include < sys/time.h > #include < sys/resource.h > #include < unistd.h > #include < stdio.h > int main(){ struct rusage usage; int i; j; j = 1; for (i = 1; i <= 1000000; i++){ chdir("."); j *= i; } if (getrusage(RUSAGE_SELF, &usage) < 0){ perror("getrusage"); return 1; } 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; }
getgroups
#include < sys/types.h > #include < unistd.h > #include < grp.h > #includeint 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; }
getsidでプロセスのセッションIDを取得
引数pidに指定したプロセスのセッションIDを取得可能。
#define _XOPEN_SOURCE 500 #include < unistd.h > #include < stdio.h > int main(){ pid_t sid; if ((sid = getsid(0))< 0){ perror("getsid"); return 1; } printf("sid = %d\n", (int)sid); 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; }
setpgid
引数pid、引数pgidの両方にゼロを指定
#include < unistd.h > #include < stdio.h > int main() { if (setpgid(0, 0) < 0){ perror("setpgid"); 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; }