Skip to content

Commit 5dd9690

Browse files
committed
study
1 parent 96f33a1 commit 5dd9690

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

10.1.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <signal.h>
4+
5+
static void sig_usr(int);
6+
7+
int main(void)
8+
{
9+
if (signal(SIGUSR1, sig_usr) == SIG_ERR) {
10+
printf("can't catch SIGUSR1");
11+
exit(-1);
12+
}
13+
14+
if (signal(SIGUSR2, sig_usr) == SIG_ERR) {
15+
printf("can't catch SIGUSR2");
16+
exit(-1);
17+
}
18+
19+
for ( ; ; )
20+
pause();
21+
}
22+
23+
static void sig_usr(int signo)
24+
{
25+
if (signo == SIGUSR1) {
26+
printf("received SIGUSR1\n");
27+
} else if (signo == SIGUSR2) {
28+
printf("received SIGUSR2");
29+
} else {
30+
printf("received signal %d\n", signo);
31+
}
32+
}

10.2.c

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <signal.h>
4+
#include <pwd.h>
5+
#include <string.h>
6+
7+
static void my_alarm(int signo)
8+
{
9+
struct passwd *rootptr;
10+
11+
printf("in signal handler\n");
12+
if (NULL == (rootptr = getpwnam("root"))) {
13+
printf("getpwnam(root) error\n");
14+
exit(-1);
15+
}
16+
17+
alarm(1);
18+
}
19+
20+
int main(void)
21+
{
22+
struct passwd *ptr;
23+
24+
signal(SIGALRM, my_alarm);
25+
alarm(1);
26+
27+
for (; ;) {
28+
if (NULL == (ptr = getpwnam("vagrant"))) {
29+
printf("getpwnam error\n");
30+
exit(-1);
31+
}
32+
33+
if (strcmp(ptr->pw_name, "vagrant") != 0) {
34+
printf("return value corrupted, pw_name = %s\n", ptr->pw_name);
35+
}
36+
}
37+
}

8.8.c

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <sys/wait.h>
5+
6+
char *env_init[] = {"USER=unknown", "PATH=/tmp", NULL};
7+
8+
int main(void)
9+
{
10+
pid_t pid;
11+
12+
if ((pid = fork()) < 0) {
13+
printf("fork error\n");
14+
exit(-1);
15+
} else if (0 == pid) {
16+
if (execle("/home/sar/bin/echoall", "echoall", "myarg1", "MY ARG2", (char *)0, env_init) < 0) {
17+
printf("execle error\n");
18+
exit(-1);
19+
}
20+
}
21+
22+
if (waitpid(pid, NULL, 0) < 0) {
23+
printf("wait error\n");
24+
exit(-1);
25+
}
26+
27+
if ((pid = fork()) < 0) {
28+
printf("fork error\n");
29+
exit(-1);
30+
} else if (0 == pid) {
31+
if (execlp("echoall", "echoall", "only 1 arg", (char *)0) < 0) {
32+
printf("ececlp error\n");
33+
exit(-1);
34+
}
35+
}
36+
37+
return 0;
38+
}

0 commit comments

Comments
 (0)