]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/unix.factor
Make unix-system-call retry the call immediately upon hitting EINTR. Use unix-system...
[factor.git] / basis / unix / unix.factor
1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! Copyright (C) 2008 Eduardo Cavazos.
3 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors alien alien.c-types alien.libraries
5 alien.syntax byte-arrays classes.struct combinators
6 combinators.short-circuit combinators.smart continuations
7 generalizations io kernel libc locals macros math namespaces
8 sequences stack-checker strings system unix.time unix.types
9 vocabs vocabs.loader ;
10 IN: unix
11
12 CONSTANT: PROT_NONE   0
13 CONSTANT: PROT_READ   1
14 CONSTANT: PROT_WRITE  2
15 CONSTANT: PROT_EXEC   4
16                        
17 CONSTANT: MAP_FILE    0
18 CONSTANT: MAP_SHARED  1
19 CONSTANT: MAP_PRIVATE 2
20
21 CONSTANT: SEEK_SET 0
22 CONSTANT: SEEK_CUR 1
23 CONSTANT: SEEK_END 2
24
25 : MAP_FAILED ( -- alien ) -1 <alien> ; inline
26
27 CONSTANT: NGROUPS_MAX 16
28
29 CONSTANT: DT_UNKNOWN   0
30 CONSTANT: DT_FIFO      1
31 CONSTANT: DT_CHR       2
32 CONSTANT: DT_DIR       4
33 CONSTANT: DT_BLK       6
34 CONSTANT: DT_REG       8
35 CONSTANT: DT_LNK      10
36 CONSTANT: DT_SOCK     12
37 CONSTANT: DT_WHT      14
38
39 LIBRARY: libc
40
41 FUNCTION: char* strerror ( int errno ) ;
42
43 ERROR: unix-error errno message ;
44
45 : (io-error) ( -- * ) errno dup strerror unix-error ;
46
47 : io-error ( n -- ) 0 < [ (io-error) ] when ;
48
49 ERROR: unix-system-call-error args errno message word ;
50
51 : unix-call-failed? ( ret -- ? )
52     {
53         [ { [ integer? ] [ 0 < ] } 1&& ]
54         [ not ]
55     } 1|| ;
56
57 MACRO:: unix-system-call ( quot -- )
58     quot inputs :> n
59     quot first :> word
60     0 :> ret!
61     f :> failed!
62     [
63         [
64             n ndup quot call ret!
65             ret {
66                 [ unix-call-failed? dup failed! ]
67                 [ drop errno EINTR = ]
68             } 1&&
69         ] loop
70         failed [
71             n narray
72             errno dup strerror
73             word unix-system-call-error
74         ] [
75             n ndrop
76             ret
77         ] if
78     ] ;
79
80 HOOK: open-file os ( path flags mode -- fd )
81
82 <<
83
84 {
85     { [ os linux? ] [ "unix.linux" require ] }
86     { [ os bsd? ] [ "unix.bsd" require ] }
87     { [ os solaris? ] [ "unix.solaris" require ] }
88 } cond
89
90 "debugger" vocab [
91     "unix.debugger" require
92 ] when
93
94 >>
95
96 STRUCT: group
97     { gr_name char* }
98     { gr_passwd char* }
99     { gr_gid int }
100     { gr_mem char** } ;
101
102 FUNCTION: int accept ( int s, void* sockaddr, socklen_t* socklen ) ;
103 FUNCTION: int bind ( int s, void* name, socklen_t namelen ) ;
104 FUNCTION: int chdir ( char* path ) ;
105 FUNCTION: int chmod ( char* path, mode_t mode ) ;
106 FUNCTION: int fchmod ( int fd, mode_t mode ) ;
107 FUNCTION: int chown ( char* path, uid_t owner, gid_t group ) ;
108 FUNCTION: int chroot ( char* path ) ;
109
110 FUNCTION: int close ( int fd ) ;
111 FUNCTION: int closedir ( DIR* dirp ) ;
112
113 : close-file ( fd -- ) [ close ] unix-system-call drop ;
114
115 FUNCTION: int connect ( int s, void* name, socklen_t namelen ) ;
116 FUNCTION: int dup2 ( int oldd, int newd ) ;
117 ! FUNCTION: int dup ( int oldd ) ;
118 : _exit ( status -- * )
119     #! We throw to give this a terminating stack effect.
120     int f "_exit" { int } alien-invoke "Exit failed" throw ;
121 FUNCTION: void endpwent ( ) ;
122 FUNCTION: int fchdir ( int fd ) ;
123 FUNCTION: int fchown ( int fd, uid_t owner, gid_t group ) ;
124 FUNCTION: int fcntl ( int fd, int cmd, int arg ) ;
125 FUNCTION: int flock ( int fd, int operation ) ;
126 FUNCTION: void freeaddrinfo ( addrinfo* ai ) ;
127 FUNCTION: int futimes ( int id, timeval[2] times ) ;
128 FUNCTION: char* gai_strerror ( int ecode ) ;
129 FUNCTION: int getaddrinfo ( char* hostname, char* servname, addrinfo* hints, addrinfo** res ) ;
130 FUNCTION: char* getcwd ( char* buf, size_t size ) ;
131 FUNCTION: pid_t getpid ;
132 FUNCTION: int getdtablesize ;
133 FUNCTION: gid_t getegid ;
134 FUNCTION: uid_t geteuid ;
135 FUNCTION: gid_t getgid ;
136 FUNCTION: char* getenv ( char* name ) ;
137
138 FUNCTION: int getgrgid_r ( gid_t gid, group* grp, char* buffer, size_t bufsize, group** result ) ;
139 FUNCTION: int getgrnam_r ( char* name, group* grp, char* buffer, size_t bufsize, group** result ) ;
140 FUNCTION: passwd* getpwent ( ) ;
141 FUNCTION: passwd* getpwuid ( uid_t uid ) ;
142 FUNCTION: passwd* getpwnam ( char* login ) ;
143 FUNCTION: int getpwnam_r ( char* login, passwd* pwd, char* buffer, size_t bufsize, passwd** result ) ;
144 FUNCTION: int getgroups ( int gidsetlen, gid_t* gidset ) ;
145 FUNCTION: int getgrouplist ( char* name, int basegid, int* groups, int* ngroups ) ;
146 FUNCTION: int getrlimit ( int resource, rlimit* rlp ) ;
147 FUNCTION: int setrlimit ( int resource, rlimit* rlp ) ;
148
149 FUNCTION: int getpriority ( int which, id_t who ) ;
150 FUNCTION: int setpriority ( int which, id_t who, int prio ) ;
151
152 FUNCTION: int getrusage ( int who, rusage* r_usage ) ;
153
154 FUNCTION: group* getgrent ;
155 FUNCTION: int gethostname ( char* name, int len ) ;
156 FUNCTION: int getsockname ( int socket, sockaddr* address, socklen_t* address_len ) ;
157 FUNCTION: int getpeername ( int socket, sockaddr* address, socklen_t* address_len ) ;
158 FUNCTION: uid_t getuid ;
159 FUNCTION: uint htonl ( uint n ) ;
160 FUNCTION: ushort htons ( ushort n ) ;
161 ! FUNCTION: int issetugid ;
162 FUNCTION: int ioctl ( int fd, ulong request, char* argp ) ;
163 FUNCTION: int lchown ( char* path, uid_t owner, gid_t group ) ;
164 FUNCTION: int listen ( int s, int backlog ) ;
165 FUNCTION: off_t lseek ( int fildes, off_t offset, int whence ) ;
166 FUNCTION: int mkdir ( char* path, mode_t mode ) ;
167 FUNCTION: void* mmap ( void* addr, size_t len, int prot, int flags, int fd, off_t offset ) ;
168 FUNCTION: int munmap ( void* addr, size_t len ) ;
169 FUNCTION: uint ntohl ( uint n ) ;
170 FUNCTION: ushort ntohs ( ushort n ) ;
171 FUNCTION: int shutdown ( int fd, int how ) ;
172
173 FUNCTION: int open ( char* path, int flags, int prot ) ;
174
175 M: unix open-file [ open ] unix-system-call ;
176
177 FUNCTION: DIR* opendir ( char* path ) ;
178
179 STRUCT: utimbuf
180     { actime time_t }
181     { modtime time_t } ;
182
183 FUNCTION: int utime ( char* path, utimbuf* buf ) ;
184
185 : touch ( filename -- ) f [ utime ] unix-system-call drop ;
186
187 : change-file-times ( filename access modification -- )
188     utimbuf <struct>
189         swap >>modtime
190         swap >>actime
191         [ utime ] unix-system-call drop ;
192
193 FUNCTION: int pclose ( void* file ) ;
194 FUNCTION: int pipe ( int* filedes ) ;
195 FUNCTION: void* popen ( char* command, char* type ) ;
196 FUNCTION: ssize_t read ( int fd, void* buf, size_t nbytes ) ;
197
198 FUNCTION: dirent* readdir ( DIR* dirp ) ;
199 FUNCTION: int readdir_r ( void* dirp, dirent* entry, dirent** result ) ;
200 FUNCTION: ssize_t readlink ( char* path, char* buf, size_t bufsize ) ;
201
202 CONSTANT: PATH_MAX 1024
203
204 : read-symbolic-link ( path -- path )
205     PATH_MAX <byte-array> dup [
206         PATH_MAX
207         [ readlink ] unix-system-call
208     ] dip swap head-slice >string ;
209
210 FUNCTION: ssize_t recv ( int s, void* buf, size_t nbytes, int flags ) ;
211 FUNCTION: ssize_t recvfrom ( int s, void* buf, size_t nbytes, int flags, sockaddr-in* from, socklen_t* fromlen ) ;
212 FUNCTION: int rename ( char* from, char* to ) ;
213 FUNCTION: int rmdir ( char* path ) ;
214 FUNCTION: int select ( int nfds, void* readfds, void* writefds, void* exceptfds, timeval* timeout ) ;
215 FUNCTION: ssize_t sendto ( int s, void* buf, size_t len, int flags, sockaddr-in* to, socklen_t tolen ) ;
216 FUNCTION: int setenv ( char* name, char* value, int overwrite ) ;
217 FUNCTION: int unsetenv ( char* name ) ;
218 FUNCTION: int setegid ( gid_t egid ) ;
219 FUNCTION: int seteuid ( uid_t euid ) ;
220 FUNCTION: int setgid ( gid_t gid ) ;
221 FUNCTION: int setgroups ( int ngroups, gid_t* gidset ) ;
222 FUNCTION: int setregid ( gid_t rgid, gid_t egid ) ;
223 FUNCTION: int setreuid ( uid_t ruid, uid_t euid ) ;
224 FUNCTION: int setsockopt ( int s, int level, int optname, void* optval, socklen_t optlen ) ;
225 FUNCTION: int setuid ( uid_t uid ) ;
226 FUNCTION: int socket ( int domain, int type, int protocol ) ;
227 FUNCTION: int symlink ( char* path1, char* path2 ) ;
228 FUNCTION: int link ( char* path1, char* path2 ) ;
229 FUNCTION: int system ( char* command ) ;
230
231 FUNCTION: int unlink ( char* path ) ;
232
233 : unlink-file ( path -- ) [ unlink ] unix-system-call drop ;
234
235 FUNCTION: int utimes ( char* path, timeval[2] times ) ;
236
237 FUNCTION: ssize_t write ( int fd, void* buf, size_t nbytes ) ;
238
239 "librt" "librt.so" "cdecl" add-library