/
4-6.c
62 lines (53 loc) · 1.09 KB
/
4-6.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <string.h>
#include <pwd.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#define TARGET_NAME "harada"
#define OTHER_NAME "root"
void handler(int);
int main(void){
struct passwd pwbuf, *ptr;
char *buf;
int len = 3000;
buf=malloc(len);
if(!buf){
perror("malloc");
exit(EXIT_FAILURE);
}
if(signal(SIGALRM, handler)==SIG_ERR){
perror("signal");
exit(EXIT_FAILURE);
}
alarm(1);
for(;;){
if (getpwnam_r(TARGET_NAME, &pwbuf, buf, len, &ptr) != 0) {
perror("getpwnam");
exit(EXIT_FAILURE);
}
if(strcmp(ptr->pw_name,TARGET_NAME) != 0){
printf("return value corrupted! pw_name =%s\n",ptr->pw_name);
exit(EXIT_FAILURE);
}
}
free(buf);
}
void handler(int signum){
struct passwd pwbuf, *ptr;
char *buf;
int len = 3000;
buf=malloc(len);
if(!buf){
perror("malloc");
exit(EXIT_FAILURE);
}
printf("in signal handler\n");
if (getpwnam_r(OTHER_NAME, &pwbuf, buf, len, &ptr) != 0) {
perror("getpwnam");
exit(EXIT_FAILURE);
}
free(buf);
alarm(1);
}