1
|
1
|
|
2 /* Written by Walter Bright, Christopher E. Miller, and many others.
|
|
3 * www.digitalmars.com
|
|
4 * Placed into public domain.
|
|
5 * Linux(R) is the registered trademark of Linus Torvalds in the U.S. and other
|
|
6 * countries.
|
|
7 */
|
|
8
|
|
9 module std.c.linux.linux;
|
|
10
|
|
11 public import std.c.linux.linuxextern;
|
|
12 public import std.c.linux.pthread;
|
|
13
|
|
14 private import std.c.stdio;
|
|
15
|
|
16 alias int pid_t;
|
|
17 alias int off_t;
|
|
18 alias uint mode_t;
|
|
19
|
|
20 alias uint uid_t;
|
|
21 alias uint gid_t;
|
|
22
|
|
23 static if (size_t.sizeof == 4)
|
|
24 alias int ssize_t;
|
|
25 else
|
|
26 alias long ssize_t;
|
|
27
|
|
28 enum : int
|
|
29 {
|
|
30 SIGHUP = 1,
|
|
31 SIGINT = 2,
|
|
32 SIGQUIT = 3,
|
|
33 SIGILL = 4,
|
|
34 SIGTRAP = 5,
|
|
35 SIGABRT = 6,
|
|
36 SIGIOT = 6,
|
|
37 SIGBUS = 7,
|
|
38 SIGFPE = 8,
|
|
39 SIGKILL = 9,
|
|
40 SIGUSR1 = 10,
|
|
41 SIGSEGV = 11,
|
|
42 SIGUSR2 = 12,
|
|
43 SIGPIPE = 13,
|
|
44 SIGALRM = 14,
|
|
45 SIGTERM = 15,
|
|
46 SIGSTKFLT = 16,
|
|
47 SIGCHLD = 17,
|
|
48 SIGCONT = 18,
|
|
49 SIGSTOP = 19,
|
|
50 SIGTSTP = 20,
|
|
51 SIGTTIN = 21,
|
|
52 SIGTTOU = 22,
|
|
53 SIGURG = 23,
|
|
54 SIGXCPU = 24,
|
|
55 SIGXFSZ = 25,
|
|
56 SIGVTALRM = 26,
|
|
57 SIGPROF = 27,
|
|
58 SIGWINCH = 28,
|
|
59 SIGPOLL = 29,
|
|
60 SIGIO = 29,
|
|
61 SIGPWR = 30,
|
|
62 SIGSYS = 31,
|
|
63 SIGUNUSED = 31,
|
|
64 }
|
|
65
|
|
66 enum
|
|
67 {
|
|
68 O_RDONLY = 0,
|
|
69 O_WRONLY = 1,
|
|
70 O_RDWR = 2,
|
|
71 O_CREAT = 0100,
|
|
72 O_EXCL = 0200,
|
|
73 O_TRUNC = 01000,
|
|
74 O_APPEND = 02000,
|
|
75 }
|
|
76
|
|
77 struct struct_stat // distinguish it from the stat() function
|
|
78 {
|
|
79 ulong st_dev; /// device
|
|
80 ushort __pad1;
|
|
81 uint st_ino; /// file serial number
|
|
82 uint st_mode; /// file mode
|
|
83 uint st_nlink; /// link count
|
|
84 uint st_uid; /// user ID of file's owner
|
|
85 uint st_gid; /// user ID of group's owner
|
|
86 ulong st_rdev; /// if device then device number
|
|
87 ushort __pad2;
|
|
88 int st_size; /// file size in bytes
|
|
89 int st_blksize; /// optimal I/O block size
|
|
90 int st_blocks; /// number of allocated 512 byte blocks
|
|
91 int st_atime;
|
|
92 uint st_atimensec;
|
|
93 int st_mtime;
|
|
94 uint st_mtimensec;
|
|
95 int st_ctime;
|
|
96 uint st_ctimensec;
|
|
97
|
|
98 uint __unused4;
|
|
99 uint __unused5;
|
|
100 }
|
|
101
|
|
102 static assert(struct_stat.sizeof == 88);
|
|
103
|
|
104 enum : int
|
|
105 {
|
|
106 S_IFIFO = 0010000,
|
|
107 S_IFCHR = 0020000,
|
|
108 S_IFDIR = 0040000,
|
|
109 S_IFBLK = 0060000,
|
|
110 S_IFREG = 0100000,
|
|
111 S_IFLNK = 0120000,
|
|
112 S_IFSOCK = 0140000,
|
|
113
|
|
114 S_IFMT = 0170000,
|
|
115
|
|
116 S_IREAD = 0000400,
|
|
117 S_IWRITE = 0000200,
|
|
118 S_IEXEC = 0000100,
|
|
119 }
|
|
120
|
|
121 extern (C)
|
|
122 {
|
|
123 int access(char*, int);
|
|
124 int open(char*, int, ...);
|
|
125 int read(int, void*, int);
|
|
126 int write(int, void*, int);
|
|
127 int close(int);
|
|
128 int lseek(int, int, int);
|
|
129 int fstat(int, struct_stat*);
|
|
130 int lstat(char*, struct_stat*);
|
|
131 int stat(char*, struct_stat*);
|
|
132 int chdir(char*);
|
|
133 int mkdir(char*, int);
|
|
134 int rmdir(char*);
|
|
135 char* getcwd(char*, int);
|
|
136 int chmod(char*, mode_t);
|
|
137 int fork();
|
|
138 int dup(int);
|
|
139 int dup2(int, int);
|
|
140 int pipe(int[2]);
|
|
141 pid_t wait(int*);
|
|
142 int waitpid(pid_t, int*, int);
|
|
143
|
|
144 uint alarm(uint);
|
|
145 char* basename(char*);
|
|
146 //wint_t btowc(int);
|
|
147 int chown(char*, uid_t, gid_t);
|
|
148 int chroot(char*);
|
|
149 size_t confstr(int, char*, size_t);
|
|
150 int creat(char*, mode_t);
|
|
151 char* ctermid(char*);
|
|
152 int dirfd(DIR*);
|
|
153 char* dirname(char*);
|
|
154 int fattach(int, char*);
|
|
155 int fchmod(int, mode_t);
|
|
156 int fdatasync(int);
|
|
157 int ffs(int);
|
|
158 int fmtmsg(int, char*, int, char*, char*, char*);
|
|
159 int fpathconf(int, int);
|
|
160 int fseeko(FILE*, off_t, int);
|
|
161 off_t ftello(FILE*);
|
|
162
|
|
163 extern char** environ;
|
|
164 }
|
|
165
|
|
166 struct timeval
|
|
167 {
|
|
168 int tv_sec;
|
|
169 int tv_usec;
|
|
170 }
|
|
171
|
|
172 struct struct_timezone
|
|
173 {
|
|
174 int tz_minuteswest;
|
|
175 int tz_dstime;
|
|
176 }
|
|
177
|
|
178 struct tm
|
|
179 {
|
|
180 int tm_sec;
|
|
181 int tm_min;
|
|
182 int tm_hour;
|
|
183 int tm_mday;
|
|
184 int tm_mon;
|
|
185 int tm_year;
|
|
186 int tm_wday;
|
|
187 int tm_yday;
|
|
188 int tm_isdst;
|
|
189 int tm_gmtoff;
|
|
190 int tm_zone;
|
|
191 }
|
|
192
|
|
193 extern (C)
|
|
194 {
|
|
195 int gettimeofday(timeval*, struct_timezone*);
|
|
196 __time_t time(__time_t*);
|
|
197 char* asctime(tm*);
|
|
198 char* ctime(__time_t*);
|
|
199 tm* gmtime(__time_t*);
|
|
200 tm* localtime(__time_t*);
|
|
201 __time_t mktime(tm*);
|
|
202 char* asctime_r(tm* t, char* buf);
|
|
203 char* ctime_r(__time_t* timep, char* buf);
|
|
204 tm* gmtime_r(__time_t* timep, tm* result);
|
|
205 tm* localtime_r(__time_t* timep, tm* result);
|
|
206 }
|
|
207
|
|
208 /**************************************************************/
|
|
209 // Memory mapping from <sys/mman.h> and <bits/mman.h>
|
|
210
|
|
211 enum
|
|
212 {
|
|
213 PROT_NONE = 0,
|
|
214 PROT_READ = 1,
|
|
215 PROT_WRITE = 2,
|
|
216 PROT_EXEC = 4,
|
|
217 }
|
|
218
|
|
219 // Memory mapping sharing types
|
|
220
|
|
221 enum
|
|
222 { MAP_SHARED = 1,
|
|
223 MAP_PRIVATE = 2,
|
|
224 MAP_TYPE = 0x0F,
|
|
225 MAP_FIXED = 0x10,
|
|
226 MAP_FILE = 0,
|
|
227 MAP_ANONYMOUS = 0x20,
|
|
228 MAP_ANON = 0x20,
|
|
229 MAP_GROWSDOWN = 0x100,
|
|
230 MAP_DENYWRITE = 0x800,
|
|
231 MAP_EXECUTABLE = 0x1000,
|
|
232 MAP_LOCKED = 0x2000,
|
|
233 MAP_NORESERVE = 0x4000,
|
|
234 MAP_POPULATE = 0x8000,
|
|
235 MAP_NONBLOCK = 0x10000,
|
|
236 }
|
|
237
|
|
238 // Values for msync()
|
|
239
|
|
240 enum
|
|
241 { MS_ASYNC = 1,
|
|
242 MS_INVALIDATE = 2,
|
|
243 MS_SYNC = 4,
|
|
244 }
|
|
245
|
|
246 // Values for mlockall()
|
|
247
|
|
248 enum
|
|
249 {
|
|
250 MCL_CURRENT = 1,
|
|
251 MCL_FUTURE = 2,
|
|
252 }
|
|
253
|
|
254 // Values for mremap()
|
|
255
|
|
256 enum
|
|
257 {
|
|
258 MREMAP_MAYMOVE = 1,
|
|
259 }
|
|
260
|
|
261 // Values for madvise
|
|
262
|
|
263 enum
|
|
264 { MADV_NORMAL = 0,
|
|
265 MADV_RANDOM = 1,
|
|
266 MADV_SEQUENTIAL = 2,
|
|
267 MADV_WILLNEED = 3,
|
|
268 MADV_DONTNEED = 4,
|
|
269 }
|
|
270
|
|
271 extern (C)
|
|
272 {
|
|
273 void* mmap(void*, size_t, int, int, int, off_t);
|
|
274 const void* MAP_FAILED = cast(void*)-1;
|
|
275
|
|
276 int munmap(void*, size_t);
|
|
277 int mprotect(void*, size_t, int);
|
|
278 int msync(void*, size_t, int);
|
|
279 int madvise(void*, size_t, int);
|
|
280 int mlock(void*, size_t);
|
|
281 int munlock(void*, size_t);
|
|
282 int mlockall(int);
|
|
283 int munlockall();
|
|
284 void* mremap(void*, size_t, size_t, int);
|
|
285 int mincore(void*, size_t, ubyte*);
|
|
286 int remap_file_pages(void*, size_t, int, size_t, int);
|
|
287 int shm_open(char*, int, int);
|
|
288 int shm_unlink(char*);
|
|
289 }
|
|
290
|
|
291 extern(C)
|
|
292 {
|
|
293
|
|
294 enum
|
|
295 {
|
|
296 DT_UNKNOWN = 0,
|
|
297 DT_FIFO = 1,
|
|
298 DT_CHR = 2,
|
|
299 DT_DIR = 4,
|
|
300 DT_BLK = 6,
|
|
301 DT_REG = 8,
|
|
302 DT_LNK = 10,
|
|
303 DT_SOCK = 12,
|
|
304 DT_WHT = 14,
|
|
305 }
|
|
306
|
|
307 struct dirent
|
|
308 {
|
|
309 int d_ino;
|
|
310 off_t d_off;
|
|
311 ushort d_reclen;
|
|
312 ubyte d_type;
|
|
313 char[256] d_name;
|
|
314 }
|
|
315
|
|
316 struct DIR
|
|
317 {
|
|
318 // Managed by OS.
|
|
319 }
|
|
320
|
|
321 DIR* opendir(char* name);
|
|
322 int closedir(DIR* dir);
|
|
323 dirent* readdir(DIR* dir);
|
|
324 void rewinddir(DIR* dir);
|
|
325 off_t telldir(DIR* dir);
|
|
326 void seekdir(DIR* dir, off_t offset);
|
|
327 }
|
|
328
|
|
329
|
|
330 extern(C)
|
|
331 {
|
|
332 private import std.intrinsic;
|
|
333
|
|
334
|
|
335 int select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* errorfds, timeval* timeout);
|
|
336 int fcntl(int s, int f, ...);
|
|
337
|
|
338
|
|
339 enum
|
|
340 {
|
|
341 EINTR = 4,
|
|
342 EINPROGRESS = 115,
|
|
343 }
|
|
344
|
|
345
|
|
346 const uint FD_SETSIZE = 1024;
|
|
347 //const uint NFDBITS = 8 * int.sizeof; // DMD 0.110: 8 * (int).sizeof is not an expression
|
|
348 const int NFDBITS = 32;
|
|
349
|
|
350
|
|
351 struct fd_set
|
|
352 {
|
|
353 int[FD_SETSIZE / NFDBITS] fds_bits;
|
|
354 alias fds_bits __fds_bits;
|
|
355 }
|
|
356
|
|
357
|
|
358 int FDELT(int d)
|
|
359 {
|
|
360 return d / NFDBITS;
|
|
361 }
|
|
362
|
|
363
|
|
364 int FDMASK(int d)
|
|
365 {
|
|
366 return 1 << (d % NFDBITS);
|
|
367 }
|
|
368
|
|
369
|
|
370 // Removes.
|
|
371 void FD_CLR(int fd, fd_set* set)
|
|
372 {
|
|
373 btr(cast(uint*)&set.fds_bits.ptr[FDELT(fd)], cast(uint)(fd % NFDBITS));
|
|
374 }
|
|
375
|
|
376
|
|
377 // Tests.
|
|
378 int FD_ISSET(int fd, fd_set* set)
|
|
379 {
|
|
380 return bt(cast(uint*)&set.fds_bits.ptr[FDELT(fd)], cast(uint)(fd % NFDBITS));
|
|
381 }
|
|
382
|
|
383
|
|
384 // Adds.
|
|
385 void FD_SET(int fd, fd_set* set)
|
|
386 {
|
|
387 bts(cast(uint*)&set.fds_bits.ptr[FDELT(fd)], cast(uint)(fd % NFDBITS));
|
|
388 }
|
|
389
|
|
390
|
|
391 // Resets to zero.
|
|
392 void FD_ZERO(fd_set* set)
|
|
393 {
|
|
394 set.fds_bits[] = 0;
|
|
395 }
|
|
396 }
|
|
397
|
|
398 extern (C)
|
|
399 {
|
|
400 /* From <dlfcn.h>
|
|
401 * See http://www.opengroup.org/onlinepubs/007908799/xsh/dlsym.html
|
|
402 */
|
|
403
|
|
404 const int RTLD_NOW = 0x00002; // Correct for Red Hat 8
|
|
405
|
|
406 void* dlopen(char* file, int mode);
|
|
407 int dlclose(void* handle);
|
|
408 void* dlsym(void* handle, char* name);
|
|
409 char* dlerror();
|
|
410 }
|
|
411
|
|
412 extern (C)
|
|
413 {
|
|
414 /* from <pwd.h>
|
|
415 */
|
|
416
|
|
417 struct passwd
|
|
418 {
|
|
419 char *pw_name;
|
|
420 char *pw_passwd;
|
|
421 uid_t pw_uid;
|
|
422 gid_t pw_gid;
|
|
423 char *pw_gecos;
|
|
424 char *pw_dir;
|
|
425 char *pw_shell;
|
|
426 }
|
|
427
|
|
428 const size_t _SIGSET_NWORDS = 1024 / (8 * uint.sizeof);
|
|
429 struct sigset_t
|
|
430 {
|
|
431 uint[_SIGSET_NWORDS] __val;
|
|
432 }
|
|
433
|
|
434 int getpwnam_r(char*, passwd*, void*, size_t, passwd**);
|
|
435 passwd* getpwnam(char*);
|
|
436 passwd* getpwuid(uid_t);
|
|
437 int getpwuid_r(uid_t, passwd*, char*, size_t, passwd**);
|
|
438 int kill(pid_t, int);
|
|
439 int sem_close(sem_t*);
|
|
440 int sigemptyset(sigset_t*);
|
|
441 int sigfillset(sigset_t*);
|
|
442 int sigismember(sigset_t*, int);
|
|
443 int sigsuspend(sigset_t*);
|
|
444 }
|
|
445
|
|
446 extern (C)
|
|
447 {
|
|
448 /* from semaphore.h
|
|
449 */
|
|
450
|
|
451 struct sem_t
|
|
452 {
|
|
453 _pthread_fastlock __sem_lock;
|
|
454 int __sem_value;
|
|
455 void* __sem_waiting;
|
|
456 }
|
|
457
|
|
458 int sem_init(sem_t*, int, uint);
|
|
459 int sem_wait(sem_t*);
|
|
460 int sem_trywait(sem_t*);
|
|
461 int sem_post(sem_t*);
|
|
462 int sem_getvalue(sem_t*, int*);
|
|
463 int sem_destroy(sem_t*);
|
|
464 }
|
|
465
|
|
466 extern (C)
|
|
467 {
|
|
468 /* from utime.h
|
|
469 */
|
|
470
|
|
471 struct utimbuf
|
|
472 {
|
|
473 __time_t actime;
|
|
474 __time_t modtime;
|
|
475 }
|
|
476
|
|
477 int utime(char* filename, utimbuf* buf);
|
|
478 }
|