| |
| /pliant/language/os/linux.pli |
| |
| 1 |
# Copyright Hubert Tonneau hubert.tonneau@pliant.cx | |
| 2 |
# | |
| 3 |
# This program is free software; you can redistribute it and/or | |
| 4 |
# modify it under the terms of the GNU General Public License version 2 | |
| 5 |
# as published by the Free Software Foundation. | |
| 6 |
# | |
| 7 |
# This program is distributed in the hope that it will be useful, | |
| 8 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 9 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 10 |
# GNU General Public License for more details. | |
| 11 |
# | |
| 12 |
# You should have received a copy of the GNU General Public License | |
| 13 |
# version 2 along with this program; if not, write to the Free Software | |
| 14 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 15 |
| |
| 16 |
scope "/pliant/language/" | |
| 17 |
module "/pliant/install/ring2.pli" | |
| 18 |
| |
| 19 |
| |
| 20 |
| |
| 21 |
# testing the distribution | |
| 22 |
| |
| 23 |
| |
| 24 |
function os_compute_version -> version | |
| 25 |
arg Str version | |
| 26 |
var Int h := file_open "/proc/sys/kernel/osrelease" 1 | |
| 27 |
if h=-1 | |
| 28 |
return "" | |
| 29 |
var Str buffer := repeat 80 " " | |
| 30 |
var Int len := file_read h buffer:characters buffer:len | |
| 31 |
version := buffer 0 len | |
| 32 |
version := version 0 (version search "[lf]" version:len) | |
| 33 |
file_close h | |
| 34 |
| |
| 35 |
public | |
| 36 |
constant os_version os_compute_version | |
| 37 |
| |
| 38 |
| |
| 39 |
function fake_utimes filename tv -> err | |
| 40 |
arg Address filename tv ; arg Int err | |
| 41 |
kernel_function 271 | |
| 42 |
| |
| 43 |
constant yield_works (fake_utimes null null)<>(-38) # Linux 2.6 or better | |
| 44 |
| |
| 45 |
| |
| 46 |
| |
| 47 |
# processes | |
| 48 |
| |
| 49 |
function os_clone flags stack -> pid | |
| 50 |
arg Int flags pid ; arg Address stack | |
| 51 |
kernel_function 120 | |
| 52 |
| |
| 53 |
function os_execve cmd args env -> err | |
| 54 |
arg CStr cmd ; arg Address args env ; arg Int err | |
| 55 |
kernel_function 11 | |
| 56 |
| |
| 57 |
function os_getpid -> pid | |
| 58 |
arg Int pid | |
| 59 |
kernel_function 20 | |
| 60 |
| |
| 61 |
# function os_getpgid pid -> gid | |
| 62 |
# arg Int pid gid | |
| 63 |
# kernel_function 132 | |
| 64 |
| |
| 65 |
function os_waitpid pid status options -> pid2 | |
| 66 |
arg Int pid pid2 ; arg_w Int status ; arg uInt options | |
| 67 |
kernel_function 7 | |
| 68 |
| |
| 69 |
function os_setsid | |
| 70 |
kernel_function 66 | |
| 71 |
| |
| 72 |
function os_setuid id -> err | |
| 73 |
arg Int id err | |
| 74 |
kernel_function 23 | |
| 75 |
| |
| 76 |
function os_getuid -> id | |
| 77 |
arg Int id | |
| 78 |
kernel_function 24 | |
| 79 |
| |
| 80 |
function os_setgid id -> err | |
| 81 |
arg Int id err | |
| 82 |
kernel_function 46 | |
| 83 |
| |
| 84 |
function os_getgid -> id | |
| 85 |
arg Int id | |
| 86 |
kernel_function 47 | |
| 87 |
| |
| 88 |
function os_exit retcode | |
| 89 |
arg Int retcode | |
| 90 |
kernel_function 1 | |
| 91 |
| |
| 92 |
export os_clone os_execve os_getpid os_waitpid os_setsid os_setuid os_getuid os_setgid os_getgid os_exit | |
| 93 |
| |
| 94 |
| |
| 95 |
| |
| 96 |
# signals | |
| 97 |
| |
| 98 |
public | |
| 99 |
constant os_SIGHUP 1 | |
| 100 |
constant os_SIGINT 2 | |
| 101 |
constant os_SIGQUIT 3 | |
| 102 |
constant os_SIGABRT 6 | |
| 103 |
constant os_SIGBUS 7 | |
| 104 |
constant os_SIGFPE 8 | |
| 105 |
constant os_SIGKILL 9 | |
| 106 |
constant os_SIGUSR1 10 | |
| 107 |
constant os_SIGSEGV 11 | |
| 108 |
constant os_SIGUSR2 12 | |
| 109 |
constant os_SIGPIPE 13 | |
| 110 |
constant os_SIGALARM 14 | |
| 111 |
constant os_SIGTERM 15 | |
| 112 |
constant os_SIGCONT 18 | |
| 113 |
constant os_SIGSTOP 19 | |
| 114 |
| |
| 115 |
constant os_SIG_IGN 1 | |
| 116 |
| |
| 117 |
public | |
| 118 |
type os_sigaction | |
| 119 |
packed | |
| 120 |
field Address sa_handler | |
| 121 |
field uInt sa_mask | |
| 122 |
field Int sa_flags | |
| 123 |
field Address sa_restorer | |
| 124 |
| |
| 125 |
function build sa | |
| 126 |
arg_w os_sigaction sa | |
| 127 |
memory_clear addressof:sa os_sigaction:size | |
| 128 |
| |
| 129 |
function os_sigaction num newaction oldaction -> err | |
| 130 |
arg Int num err ; arg os_sigaction newaction ; arg_w os_sigaction oldaction | |
| 131 |
kernel_function 67 | |
| 132 |
| |
| 133 |
function os_sigsetmask mask | |
| 134 |
arg uInt mask | |
| 135 |
kernel_function 69 | |
| 136 |
| |
| 137 |
function os_sigsuspend drop1 drop2 mask | |
| 138 |
arg uInt drop1 drop2 mask | |
| 139 |
kernel_function 72 | |
| 140 |
| |
| 141 |
function os_kill pid sig | |
| 142 |
arg Int pid sig | |
| 143 |
kernel_function 37 | |
| 144 |
| |
| 145 |
export os_sigaction | |
| 146 |
export os_sigsetmask os_sigsuspend os_kill | |
| 147 |
| |
| 148 |
| |
| 149 |
| |
| 150 |
# time | |
| 151 |
| |
| 152 |
public | |
| 153 |
type os_timespec | |
| 154 |
packed | |
| 155 |
field Int tv_sec tv_nsec | |
| 156 |
| |
| 157 |
function os_nanosleep req rem | |
| 158 |
arg os_timespec req ; arg_w os_timespec rem | |
| 159 |
kernel_function 162 | |
| 160 |
| |
| 161 |
function os_yield | |
| 162 |
if yield_works | |
| 163 |
kernel_function 158 | |
| 164 |
else | |
| 165 |
var os_timespec ts | |
| 166 |
ts tv_sec := 0 | |
| 167 |
ts tv_nsec := 0 | |
| 168 |
os_nanosleep ts ts | |
| 169 |
| |
| 170 |
function os_setpriority which who prio -> err | |
| 171 |
arg Int which who prio err | |
| 172 |
kernel_function 97 | |
| 173 |
| |
| 174 |
constant os_PRIO_PROCESS 0 | |
| 175 |
| |
| 176 |
export os_nanosleep os_yield os_setpriority os_PRIO_PROCESS | |
| 177 |
| |
| 178 |
| |
| 179 |
public | |
| 180 |
type os_timeval | |
| 181 |
packed | |
| 182 |
field Int tv_sec tv_usec | |
| 183 |
| |
| 184 |
type os_timezone | |
| 185 |
packed | |
| 186 |
field Int tz_minuteswest | |
| 187 |
field Int tz_dsttime | |
| 188 |
| |
| 189 |
function os_gettimeofday tv tz | |
| 190 |
arg_w os_timeval tv ; arg_w os_timezone tz | |
| 191 |
kernel_function 78 | |
| 192 |
| |
| 193 |
function os_settimeofday tv tz | |
| 194 |
arg os_timeval tv ; arg os_timezone tz | |
| 195 |
kernel_function 79 | |
| 196 |
| |
| 197 |
export os_gettimeofday os_settimeofday | |
| 198 |
| |
| 199 |
| |
| 200 |
| |
| 201 |
# files IO | |
| 202 |
| |
| 203 |
function os_read handle address size -> err | |
| 204 |
arg Int handle size err ; arg Address address | |
| 205 |
kernel_function 3 | |
| 206 |
| |
| 207 |
function os_write handle address size -> err | |
| 208 |
arg Int handle size err ; arg Address address | |
| 209 |
kernel_function 4 | |
| 210 |
| |
| 211 |
function os_select n readfds writefds exceptfds timeout -> fd | |
| 212 |
arg Int n ; arg Address readfds writefds exceptfds ; arg_rw os_timeval timeout ; arg Int fd | |
| 213 |
kernel_function 142 | |
| 214 |
| |
| 215 |
public | |
| 216 |
type os_pollfd | |
| 217 |
field Int fd | |
| 218 |
field uInt16 events revents | |
| 219 |
constant os_POLLIN 1 | |
| 220 |
constant os_POLLOUT 4 | |
| 221 |
constant os_POLLERR 8 | |
| 222 |
constant os_POLLHUP 16 | |
| 223 |
constant os_POLLNVAL 20 | |
| 224 |
| |
| 225 |
| |
| 226 |
function os_poll fds nfds timeout -> count | |
| 227 |
arg Address fds ; arg Int nfds timeout ; arg Int count | |
| 228 |
kernel_function 168 | |
| 229 |
| |
| 230 |
public | |
| 231 |
constant os_EAGAIN 11 | |
| 232 |
| |
| 233 |
public | |
| 234 |
constant os_O_RDONLY 0 | |
| 235 |
constant os_O_WRONLY 1 | |
| 236 |
constant os_O_RDWR 2 | |
| 237 |
constant os_O_CREAT 1*8^2 | |
| 238 |
constant os_O_NOCTTY 4*8^2 | |
| 239 |
constant os_O_TRUNC 1*8^3 | |
| 240 |
constant os_O_APPEND 2*8^3 | |
| 241 |
constant os_O_NONBLOCK 4*8^3 | |
| 242 |
constant os_O_LARGEFILE 1*8^5 | |
| 243 |
| |
| 244 |
public | |
| 245 |
constant os_F_SETFL 4 | |
| 246 |
| |
| 247 |
function os_open name flags rights -> handle | |
| 248 |
arg CStr name ; arg Int flags rights handle | |
| 249 |
kernel_function 5 | |
| 250 |
| |
| 251 |
function os_close handle -> err | |
| 252 |
arg Int handle err | |
| 253 |
kernel_function 6 | |
| 254 |
| |
| 255 |
function os_ioctl handle request param -> answer | |
| 256 |
arg Int handle ; arg uInt request ; arg Address param ; arg Int answer | |
| 257 |
kernel_function 54 | |
| 258 |
| |
| 259 |
function os_fcntl handle cmd arg -> answer | |
| 260 |
arg Int handle cmd arg answer | |
| 261 |
kernel_function 55 | |
| 262 |
| |
| 263 |
public | |
| 264 |
constant os_F_SETFD 2 | |
| 265 |
constant os_FD_CLOEXEC 1 | |
| 266 |
| |
| 267 |
public | |
| 268 |
constant os_SEEK_SET 0 | |
| 269 |
constant os_SEEK_CUR 1 | |
| 270 |
constant os_SEEK_END 2 | |
| 271 |
| |
| 272 |
function os_lseek handle offset whence -> new_offset | |
| 273 |
arg Int handle offset whence new_offset | |
| 274 |
kernel_function 19 | |
| 275 |
| |
| 276 |
function os_llseek handle high low result whence -> err | |
| 277 |
arg Int handle ; arg uInt high low ; arg Address result ; arg Int whence ; arg Int err | |
| 278 |
kernel_function 140 | |
| 279 |
| |
| 280 |
function os_fsync handle -> err | |
| 281 |
arg Int handle err | |
| 282 |
kernel_function 118 | |
| 283 |
| |
| 284 |
function os_sync -> err | |
| 285 |
arg Int err | |
| 286 |
kernel_function 36 | |
| 287 |
| |
| 288 |
public | |
| 289 |
type os_pipe_handles | |
| 290 |
field Int in out | |
| 291 |
| |
| 292 |
function os_pipe handles -> err | |
| 293 |
arg_w os_pipe_handles handles ; arg Int err | |
| 294 |
kernel_function 42 | |
| 295 |
| |
| 296 |
function os_dup src -> dest | |
| 297 |
arg Int src dest | |
| 298 |
kernel_function 41 | |
| 299 |
| |
| 300 |
function os_dup2 src dest -> newone | |
| 301 |
arg Int src dest newone | |
| 302 |
kernel_function 63 | |
| 303 |
| |
| 304 |
function os_chdir path -> err | |
| 305 |
arg CStr path ; arg Int err | |
| 306 |
kernel_function 12 | |
| 307 |
| |
| 308 |
function os_chroot path -> err | |
| 309 |
arg CStr path ; arg Int err | |
| 310 |
kernel_function 61 | |
| 311 |
| |
| 312 |
| |
| 313 |
function os_link src dest -> err | |
| 314 |
arg CStr src dest ; arg Int err | |
| 315 |
kernel_function 9 | |
| 316 |
| |
| 317 |
function os_symlink src dest -> err | |
| 318 |
arg CStr src dest ; arg Int err | |
| 319 |
kernel_function 83 | |
| 320 |
| |
| 321 |
function os_unlink name -> err | |
| 322 |
arg CStr name ; arg Int err | |
| 323 |
kernel_function 10 | |
| 324 |
| |
| 325 |
function os_rename oldpath newpath -> err | |
| 326 |
arg CStr oldpath newpath ; arg Int err | |
| 327 |
kernel_function 38 | |
| 328 |
| |
| 329 |
function os_chmod name rights -> err | |
| 330 |
arg CStr name ; arg Int rights err | |
| 331 |
kernel_function 15 | |
| 332 |
| |
| 333 |
function os_lchown name uid gid -> err | |
| 334 |
arg CStr name ; arg Int uid gid err | |
| 335 |
kernel_function 16 | |
| 336 |
| |
| 337 |
public | |
| 338 |
type os_utimbuf | |
| 339 |
packed | |
| 340 |
field uInt32 actime modtime | |
| 341 |
| |
| 342 |
function os_utime name buf -> err | |
| 343 |
arg CStr name ; arg os_utimbuf buf ; arg Int err | |
| 344 |
kernel_function 30 | |
| 345 |
| |
| 346 |
function os_mkdir name mode -> err | |
| 347 |
arg CStr name ; arg Int mode err | |
| 348 |
kernel_function 39 | |
| 349 |
| |
| 350 |
function os_rmdir name -> err | |
| 351 |
arg CStr name ; arg Int err | |
| 352 |
kernel_function 40 | |
| 353 |
| |
| 354 |
| |
| 355 |
public | |
| 356 |
type os_stat | |
| 357 |
packed | |
| 358 |
field uInt16 st_dev __pad1 | |
| 359 |
field uInt st_ino | |
| 360 |
field uInt16 st_mode | |
| 361 |
field uInt16 st_nlink | |
| 362 |
field uInt16 st_uid st_gid | |
| 363 |
field uInt16 st_rdev __pad2 | |
| 364 |
field uInt st_size st_blksize st_blocks | |
| 365 |
field uInt st_atime __unused1 st_mtime __unused2 st_ctime | |
| 366 |
field uInt __unused3 __unused4 __unused5 | |
| 367 |
| |
| 368 |
constant os_S_IFMT 0F000h | |
| 369 |
constant os_S_IFDIR 04000h | |
| 370 |
constant os_S_IFLNK 0A000h | |
| 371 |
| |
| 372 |
function os_S_ISDIR mode -> dir | |
| 373 |
arg Int mode ; arg CBool dir | |
| 374 |
dir := (mode .and. os_S_IFMT)=os_S_IFDIR | |
| 375 |
| |
| 376 |
function os_S_ISLNK mode -> dir | |
| 377 |
arg Int mode ; arg CBool dir | |
| 378 |
dir := (mode .and. os_S_IFMT)=os_S_IFLNK | |
| 379 |
| |
| 380 |
export os_S_ISDIR os_S_ISLNK | |
| 381 |
| |
| 382 |
function os_stat filename stat -> err | |
| 383 |
arg CStr filename ; arg_w os_stat stat ; arg Int err | |
| 384 |
kernel_function 106 | |
| 385 |
| |
| 386 |
function os_lstat filename stat -> err | |
| 387 |
arg CStr filename ; arg_w os_stat stat ; arg Int err | |
| 388 |
kernel_function 107 | |
| 389 |
| |
| 390 |
public | |
| 391 |
type os_dirent | |
| 392 |
field Int d_ino | |
| 393 |
field Int d_off | |
| 394 |
field uInt16 d_reclen | |
| 395 |
field (Array Char 256) d_name | |
| 396 |
| |
| 397 |
function os_getdents fd dirp count -> size | |
| 398 |
arg Int fd ; arg Address dirp ; arg Int count size | |
| 399 |
kernel_function 141 | |
| 400 |
| |
| 401 |
function os_readlink path buf bufsize -> size | |
| 402 |
arg CStr path ; arg Address buf ; arg Int bufsize size | |
| 403 |
kernel_function 85 | |
| 404 |
| |
| 405 |
| |
| 406 |
public | |
| 407 |
constant os_default_directory_mode 7*8^2+5*8+5 | |
| 408 |
constant os_default_file_mode 6*8^2+4*8+4 | |
| 409 |
| |
| 410 |
export os_open os_ioctl os_fcntl os_read os_write os_select os_poll os_close os_lseek os_llseek | |
| 411 |
export os_fsync os_sync os_pipe os_dup os_dup2 os_chdir os_chroot | |
| 412 |
export os_link os_symlink os_unlink os_rename os_chmod os_lchown | |
| 413 |
export os_utime os_mkdir os_rmdir | |
| 414 |
export os_stat os_lstat os_getdents os_readlink | |
| 415 |
| |
| 416 |
| |
| 417 |
| |
| 418 |
# memory | |
| 419 |
| |
| 420 |
public | |
| 421 |
constant os_PROT_READ 1 | |
| 422 |
constant os_PROT_WRITE 2 | |
| 423 |
constant os_PROT_EXEC 4 | |
| 424 |
| |
| 425 |
public | |
| 426 |
constant os_MAP_SHARED 01h | |
| 427 |
constant os_MAP_PRIVATE 02h | |
| 428 |
constant os_MAP_FIXED 10h | |
| 429 |
constant os_MAP_ANONYMOUS 20h | |
| 430 |
constant os_MAP_GROWSDOWN 0100h | |
| 431 |
constant os_MAP_DENYWRITE 0800h | |
| 432 |
constant os_MAP_EXECUTABLE 1000h | |
| 433 |
constant os_MAP_LOCKED 2000h | |
| 434 |
| |
| 435 |
type os_MmapRequest | |
| 436 |
packed | |
| 437 |
field Address map_at_address | |
| 438 |
field Int size prot flags | |
| 439 |
field Int file_handle file_offset | |
| 440 |
| |
| 441 |
function os_mmap1 req -> adr | |
| 442 |
arg os_MmapRequest req ; arg Address adr | |
| 443 |
kernel_function 90 | |
| 444 |
| |
| 445 |
function os_mmap map_at_address size prot flags file_handle file_offset -> adr | |
| 446 |
arg Address map_at_address adr ; arg Int size prot flags file_handle file_offset | |
| 447 |
var os_MmapRequest req | |
| 448 |
req map_at_address := map_at_address | |
| 449 |
req size := size | |
| 450 |
req prot := prot | |
| 451 |
req flags := flags | |
| 452 |
req file_handle := file_handle | |
| 453 |
req file_offset := file_offset | |
| 454 |
adr := os_mmap1 req | |
| 455 |
| |
| 456 |
function os_munmap adr size | |
| 457 |
arg Address adr ; arg Int size | |
| 458 |
kernel_function 91 | |
| 459 |
| |
| 460 |
export os_mmap os_munmap | |
| 461 |
| |
| 462 |
| |
| 463 |
| |
| 464 |
# system informations | |
| 465 |
| |
| 466 |
public | |
| 467 |
type os_sysinfo | |
| 468 |
packed | |
| 469 |
field Int uptime | |
| 470 |
field uInt load1 load5 load15 | |
| 471 |
field uInt totalram freeram sharedram bufferram | |
| 472 |
field uInt totalswap freeswap | |
| 473 |
field uInt16 procs pack1 | |
| 474 |
field Int drop1 drop2 drop3 drop4 drop5 | |
| 475 |
| |
| 476 |
function os_sysinfo info | |
| 477 |
arg_rw os_sysinfo info | |
| 478 |
kernel_function 116 | |
| 479 |
| |
| 480 |
export os_sysinfo | |
| 481 |
| |
| 482 |
| |
| 483 |
| |
| 484 |
# testing the distribution | |
| 485 |
| |
| 486 |
| |
| 487 |
function libc_filename -> filename | |
| 488 |
arg Str filename | |
| 489 |
var os_stat buf ; var Int err | |
| 490 |
if (os_extra_info parse any word:"static" any) | |
| 491 |
filename := "" | |
| 492 |
eif (os_extra_info search "libc6" -1)<>(-1) and (os_stat "/lib/libc.so.6" buf)=0 | |
| 493 |
filename := "/lib/libc.so.6" | |
| 494 |
eif (os_stat "/lib/libc.so.5" buf)=0 | |
| 495 |
filename := "/lib/libc.so.5" | |
| 496 |
else | |
| 497 |
filename := "" | |
| 498 |
if pliant_debugging_level_variable=0 | |
| 499 |
console "This is the static version of Pliant: it cannot use external functions.[lf]" | |
| 500 |
| |
| 501 |
function zlib_filename -> filename | |
| 502 |
arg Str filename | |
| 503 |
var os_stat buf ; var Int err | |
| 504 |
if (os_extra_info parse any word:"static" any) | |
| 505 |
filename := "" | |
| 506 |
eif (os_stat "/lib/libz.so" buf)=0 | |
| 507 |
filename := "/lib/libz.so" | |
| 508 |
eif (os_stat "/usr/lib/libz.so" buf)=0 | |
| 509 |
filename := "/usr/lib/libz.so" | |
| 510 |
eif (os_stat "/lib/libz.so.2" buf)=0 | |
| 511 |
filename := "/lib/libz.so.2" | |
| 512 |
eif (os_stat "/usr/lib/libz.so.2" buf)=0 | |
| 513 |
filename := "/usr/lib/libz.so.2" | |
| 514 |
eif (os_stat "/lib/libz.so.2.0" buf)=0 | |
| 515 |
filename := "/lib/libz.so.2.0" | |
| 516 |
eif (os_stat "/usr/lib/libz.so.2.0" buf)=0 | |
| 517 |
filename := "/usr/lib/libz.so.2.0" | |
| 518 |
eif (os_stat "/lib/libz.so.1" buf)=0 | |
| 519 |
filename := "/lib/libz.so.1" | |
| 520 |
eif (os_stat "/usr/lib/libz.so.1" buf)=0 | |
| 521 |
filename := "/usr/lib/libz.so.1" | |
| 522 |
eif (os_stat "/lib/libz.so.1.0" buf)=0 | |
| 523 |
filename := "/lib/libz.so.1.0" | |
| 524 |
eif (os_stat "/usr/lib/libz.so.1.0" buf)=0 | |
| 525 |
filename := "/usr/lib/libz.so.1.0" | |
| 526 |
else | |
| 527 |
filename := "" | |
| 528 |
| |
| 529 |
public | |
| 530 |
constant os_libc_filename libc_filename | |
| 531 |
constant os_zlib_filename zlib_filename | |
| |