-
Notifications
You must be signed in to change notification settings - Fork 4
/
syscalls.c
210 lines (176 loc) · 4.8 KB
/
syscalls.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
syscalls.c Test the MINIX 3 (POSIX-compatible) system calls.
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/ptrace.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <termios.h>
#include <errno.h>
int mknod (const char *, mode_t, dev_t);
int brk (void *);
static void
handler (int signum)
{
printf ("\nsignal %d caught, %s\n", signum,
signum == SIGINT ? "exiting..." : "ignoring...");
if (signum == SIGINT)
{
exit (0);
}
}
/**
* Most system calls should be executed
* individually (comment the rest). Comments
* are folded using Emacs. Don't spend all
* your time trying to cleanup manually...
*
* @return 0 on success. Another value otherwise
*/
int
main (void)
{
pid_t proc;
int fd;
const char dname[] = "tty007";
const char fname[] = "file_1";
const char fname2[] = "file_2";
const char msg[] = "bravo!\n";
const mode_t mode = S_IRUSR | S_IWUSR;
int i;
char *ptr;
/* struct sigaction action; */
struct stat file_stat;
int status;
/* -- process management -- */
/* child process creation */
/* if ((proc = fork ()) != 0) */
/* { */
/* int status; */
/* printf ("created child process %u in group %u\n", getpid (), getpgrp ()); */
/* /\* send a kill signal to the child process *\/ */
/* int s; */
/* if (s = kill (getpid (), SIGKILL) != 0) */
/* { */
/* perror ("getpid"); */
/* } */
/* /\* waiting for the child process to finish *\/ */
/* waitpid (proc, &status, 0); */
/* printf ("process %u ended\n", getpid ()); */
/* } */
/* replace the image of a process */
/* int out = 0; */
/* char *const args[] = {"uname", "-a", NULL}; */
/* char *const env[] = {NULL}; */
/* execve ("/bin/uname", args, env); */
/* perror ("execve"); /\* execve normally never returns *\/ */
/* terminate execution */
/* exit (3); */
/* change the size of the data segment */
/* ptr = (char *) malloc (1); */
/* if (brk (ptr + 30) == -1) */
/* { */
/* perror ("brk"); */
/* } */
/* -- signal handling -- */
/* Set up the structure to specify the action. */
/* action.sa_handler = handler; */
/* sigemptyset (&action.sa_mask); */
/* action.sa_flags = 0; */
/* /\* disable the default signal actions *\/ */
/* sigaction (SIGINT, &action, NULL); */
/* sigaction (SIGHUP, &action, NULL); */
/* sigaction (SIGTERM, &action, NULL); */
/* sigaction (SIGQUIT, &action, NULL); */
/* alarm a signal */
/* alarm (3); */
/* wait for signals */
/* while (1) */
/* { */
/* /\* pause the process *\/ */
/* pause (); */
/* } */
/* -- file management -- */
/* create a file (thank you ken) */
/* if (fd = creat (fname, 0664) != -1) */
/* { */
/* printf ("created file %s\n", fname); */
/* } */
/* /\* create an i-node (typically superuser only) *\/ */
/* if (fd = mknod (dname, 020744, 0x0407) != -1) */
/* { */
/* printf ("created device file %s\n", dname); */
/* } */
/* else */
/* { */
/* perror ("mknod"); */
/* } */
/* /\* open a file for read/write *\/ */
/* if ((fd = open (fname, O_RDWR | O_CREAT, mode)) != -1) */
/* { */
/* printf ("opened file %s\n", fname); */
/* /\* seek to the end of file (to append) *\/ */
/* lseek(fd, 0L, SEEK_END); */
/* /\* write to the file *\/ */
/* if ((i = write (fd, msg, strlen (msg))) == -1) */
/* { */
/* perror ("write"); */
/* return 1; */
/* } */
/* /\* close the file *\/ */
/* close (fd); */
/* /\* get file status *\/ */
/* if (stat (fname, &file_stat) == -1) */
/* { */
/* perror ("stat"); */
/* } */
/* printf ("Last file access: %s", ctime (&file_stat.st_atime)); */
/* printf("Mode: %lo (octal)\n", (unsigned long) file_stat.st_mode); */
/* } */
/* ioctl example (TODO: find a better one) */
/* if (fd = open ("/dev/ttyS0", O_RDONLY) == -1)
{
perror ("open");
}
if (ioctl(fd, TIOCMGET, &status) == -1)
{
printf("TIOCMGET failed: %s\n",
strerror(errno));
}
else {
if (status & TIOCM_DTR)
puts("TIOCM_DTR is not set");
else
puts("TIOCM_DTR is set");
}
close(fd);*/
/* check accessibility of a file */
if (access (fname, F_OK) == -1) /* check that file exists */
{
perror("access");
}
if (access (fname, R_OK) == -1) /* check that file can be read */
{
perror ("access");
}
else
{
printf ("File %s can be read\n", fname);
}
/* rename a file */
if (rename (fname, fname2) == -1)
{
perror ("rename");
}
/* fcntl example */
return 0;
}