-
Notifications
You must be signed in to change notification settings - Fork 1
/
syscall_wrapper.f90
416 lines (368 loc) · 12.5 KB
/
syscall_wrapper.f90
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
!@Somajit Dey <[email protected]> January 2021
!
! Copyright (C) 2020-2021 Somajit Dey
! Department of Physics, University of Calcutta
! Email: [email protected]
!
! This file is part of f_
!
! f_ is a free software library: you can redistribute it and/or modify it
! under the terms of the GNU Lesser General Public License as published by the
! Free Software Foundation, either version 2.1 of the License, or (at your
! option) any later version.
!
! f_ is distributed in the hope that it will be useful, but
! WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
! Lesser General Public License for more details.
!
! You should have received a copy of the GNU Lesser General Public License
! along with this program. If not, see <https://www.gnu.org/licenses/>.
!************************************************************************
!************************************************************************
! BRIEF:
! This module contains wrappers for some user-land C system calls. This
! module is meant to be used for making your Fortran code portable. Some
! compilers (e.g. gfortran, ifort) provide their own (i.e. non-standard)
! extensions for these system calls.
!
! These module procedures and interfaces basically bind to functions in
! the glibc library that ships with Linux.
! SIGSEGV RUNTIME ERROR:
! Any such error possibly arises from type mismatch when porting from 32-bit to
! 64-bit. E.g. equating INT32 with C_LONG variable type. C_LONG can take both 32-bit
! and 64-bit form depending on the implementation.
!************************************************************************
!************************************************************************
! TODO:
! Replace trim() and len_trim() with trim(adjustl()) & len_trim(adjustl()).
! Make dummy arguments optional wherever possible by making use of present().
! Give dummy arguments meaningful names that will be used as keywords.
!************************************************************************
module syscall
use iso_c_binding
use iso_fortran_env
implicit none
private
public :: f_signal,f_alarm,f_chmod,f_getpid,f_rename,f_sleep,f_kill,f_unlink,f_symlink,f_link,f_getcwd,f_time,f_nanosleep, &
f_gethostname,f_mkdir,f_rmdir,f_chdir,f_exit
type, bind(c) :: timespec
integer(c_long) :: tv_sec
integer(c_long) :: tv_nsec
end type
abstract interface
subroutine handler(signum)
integer :: signum
end subroutine handler
end interface
interface
function f_getpid() bind(c,name='getpid')
import :: c_int
integer(c_int) :: f_getpid
end function f_getpid
end interface
type handler_pointer
procedure(handler), pointer, nopass :: ptr
end type handler_pointer
integer, parameter :: no_of_signals=64 !Obtained with command: kill -l
type(handler_pointer), dimension(no_of_signals) :: handler_ptr_array
contains
!Drop the 2nd arg below to ignore signal signum. Any sleep or idle-wait would be
!interrupted though when the signal is caught.
!Use system_exit as the handler for aborting on signal signum with exitcode=signum
subroutine f_signal(signum,handler_routine)
integer, intent(in) :: signum
procedure(handler), optional:: handler_routine
type(c_funptr) :: iret
type(c_funptr) :: c_handler
interface
function c_signal(signal, sighandler) bind(c,name='signal')
import :: c_int,c_funptr
integer(c_int), value, intent(in) :: signal
type(c_funptr), value, intent(in) :: sighandler
type(c_funptr) :: c_signal
end function c_signal
end interface
if(present(handler_routine))then
handler_ptr_array(signum)%ptr => handler_routine
else
handler_ptr_array(signum)%ptr => null(handler_ptr_array(signum)%ptr)
endif
c_handler=c_funloc(f_handler)
iret=c_signal(signum,c_handler)
end subroutine f_signal
subroutine f_handler(signum) bind(c)
integer(c_int), intent(in), value :: signum
if(associated(handler_ptr_array(signum)%ptr))call handler_ptr_array(signum)%ptr(signum)
end subroutine f_handler
subroutine f_alarm(seconds,remaining)
integer, intent(in) :: seconds
integer, intent(out), optional :: remaining
integer :: iret
interface
function c_alarm(sec) bind(c,name='alarm') result(ret)
import :: c_int
integer(c_int), value :: sec
integer(c_int) :: ret
end function c_alarm
end interface
iret=c_alarm(seconds)
if(present(remaining))remaining=iret
end subroutine f_alarm
subroutine f_chmod(fname,mode,exitstat)
character(len=*), intent(in) :: fname,mode
integer, intent(out), optional :: exitstat
integer :: iret
interface
function c_chmod(fname,mode) bind(c,name='chmod') result(ret)
import :: c_char,c_int,c_long
character(c_char), dimension(*), intent(in) :: fname
integer(c_long), value :: mode
integer(c_int) :: ret
end function c_chmod
function c_strtol(string,nullchar,base) bind(c,name='strtol') result(ret)
import :: c_long,c_char,c_int
character(c_char), dimension(*) :: string,nullchar
integer(c_int), value :: base
integer(c_long) :: ret
end function c_strtol
end interface
character(len=len_trim(fname)+1) :: c_fname
character(len=len_trim(mode)+1) :: c_string
integer(c_long) :: c_mode
character(len=2)::nullchar
nullchar='0'//c_null_char
c_string=trim(mode)//c_null_char
c_mode=c_strtol(c_string,nullchar,8_c_int)
c_fname=trim(fname)//c_null_char
iret=c_chmod(c_fname,c_mode)
if(present(exitstat))exitstat=iret
end subroutine f_chmod
subroutine f_rename(oldname,newname,exitstat)
character(len=*), intent(in) :: oldname,newname
integer, intent(out), optional :: exitstat
integer :: iret
interface
function c_rename(oldf,newf) bind(c,name='rename') result(ret)
import :: c_char,c_int
character(c_char), dimension(*), intent(in) :: oldf,newf
integer(c_int) :: ret
end function c_rename
end interface
character(len=len_trim(oldname)+1) :: c_oldname
character(len=len_trim(newname)+1) :: c_newname
c_oldname=trim(oldname)//c_null_char
c_newname=trim(newname)//c_null_char
iret=c_rename(c_oldname,c_newname)
if(present(exitstat))exitstat=iret
end subroutine f_rename
subroutine f_sleep(seconds,remaining)
integer, intent(in) :: seconds
integer, intent(out), optional :: remaining
integer :: iret
interface
function c_sleep(sec) bind(c,name='sleep') result(ret)
import :: c_int
integer(c_int), value :: sec
integer(c_int) :: ret
end function c_sleep
end interface
iret=c_sleep(seconds)
if(present(remaining))remaining=iret
end subroutine f_sleep
subroutine f_kill(pid,signal,exitstat)
integer, intent(in) :: pid,signal
integer, intent(out), optional :: exitstat
integer :: iret
interface
function c_kill(pid,sig) bind(c,name='kill') result(ret)
import :: c_int
integer(c_int), value :: pid,sig
integer(c_int) :: ret
end function c_kill
end interface
iret=c_kill(pid,signal)
if(present(exitstat))exitstat=iret
end subroutine f_kill
subroutine f_unlink(path,exitstat)
character(len=*), intent(in) :: path
integer, intent(out), optional :: exitstat
integer :: iret
interface
function c_unlink(pathname) bind(c,name='unlink') result(ret)
import :: c_char,c_int
character(c_char), dimension(*) :: pathname
integer(c_int) :: ret
end function c_unlink
end interface
character(len=len_trim(path)+1) :: c_path
c_path=trim(path)//c_null_char
iret=c_unlink(c_path)
if(present(exitstat))exitstat=iret
end subroutine f_unlink
subroutine f_link(path,link,exitstat)
character(len=*), intent(in) :: path,link
integer, intent(out), optional :: exitstat
integer :: iret
interface
function c_link(path1,path2) bind(c,name='link') result(ret)
import :: c_char,c_int
character(c_char), dimension(*), intent(in) :: path1,path2
integer(c_int) :: ret
end function c_link
end interface
character(len=len_trim(path)+1) :: c_path1
character(len=len_trim(link)+1) :: c_path2
c_path1=trim(path)//c_null_char
c_path2=trim(link)//c_null_char
iret=c_link(c_path1,c_path2)
if(present(exitstat))exitstat=iret
end subroutine f_link
subroutine f_symlink(path,link,exitstat)
character(len=*), intent(in) :: path,link
integer, intent(out), optional :: exitstat
integer :: iret
interface
function c_symlink(path1,path2) bind(c,name='symlink') result(ret)
import :: c_char,c_int
character(c_char), dimension(*), intent(in) :: path1,path2
integer(c_int) :: ret
end function c_symlink
end interface
character(len=len_trim(path)+1) :: c_path1
character(len=len_trim(link)+1) :: c_path2
c_path1=trim(path)//c_null_char
c_path2=trim(link)//c_null_char
iret=c_symlink(c_path1,c_path2)
if(present(exitstat))exitstat=iret
end subroutine f_symlink
subroutine f_getcwd(str)
character(len=*) :: str
integer :: endChar
interface
subroutine c_getcwd(buff,n) bind(c,name='getcwd')
import :: c_char,c_size_t
character(c_char), intent(out) :: buff(*)
integer(c_size_t), value, intent(in) :: n
end subroutine c_getcwd
end interface
str=repeat(' ',len(str))
call c_getcwd(str,len(str,kind=c_size_t))
endChar=len_trim(str)
str(endChar:endChar)=' ' !Because C returns a null-terminated string, we remove c_null_char with a blank character
end subroutine f_getcwd
function f_time()
integer(int32) :: f_time
integer(c_long) ::f_time_c_long
interface
subroutine c_time(tloc) bind(c,name='time')
import :: c_long
integer(c_long) :: tloc
end subroutine c_time
end interface
call c_time(f_time_c_long)
f_time=int(f_time_c_long,kind(f_time))
end function f_time
subroutine f_nanosleep(s,ns,rem_s,rem_ns)
integer(int32), intent(in) :: s,ns
integer(int32), intent(out) :: rem_s,rem_ns
type(timespec) :: f_req,f_rem
interface
subroutine c_nanosleep(req,rem) bind(c,name='nanosleep')
import :: timespec
type(timespec) :: req,rem
end subroutine c_nanosleep
end interface
f_req%tv_sec=int(s,kind=c_long)
f_req%tv_nsec=int(ns,kind=c_long)
call c_nanosleep(f_req,f_rem)
rem_s=int(f_rem%tv_sec,kind=int32)
rem_ns=int(f_rem%tv_nsec,kind=int32)
end subroutine f_nanosleep
subroutine f_gethostname(str)
character(len=*) :: str
integer :: endChar
interface
subroutine c_gethostname(buff,n) bind(c,name='gethostname')
import :: c_char,c_size_t
character(c_char), intent(out) :: buff(*)
integer(c_size_t), value, intent(in) :: n
end subroutine c_gethostname
end interface
str=repeat(' ',len(str))
call c_gethostname(str,len(str,kind=c_size_t))
endChar=len_trim(str)
str(endChar:endChar)=' ' !Because C returns a null-terminated string, we remove c_null_char with a blank character
end subroutine f_gethostname
subroutine f_mkdir(fname,mode,exitstat)
character(len=*), intent(in) :: fname,mode
integer, intent(out), optional :: exitstat
integer :: iret
interface
function c_mkdir(fname,mode) bind(c,name='mkdir') result(ret)
import :: c_char,c_int,c_long
character(c_char), dimension(*), intent(in) :: fname
integer(c_long), value :: mode
integer(c_int) :: ret
end function c_mkdir
function c_strtol(string,nullchar,base) bind(c,name='strtol') result(ret)
import :: c_long,c_char,c_int
character(c_char), dimension(*) :: string,nullchar
integer(c_int), value :: base
integer(c_long) :: ret
end function c_strtol
end interface
character(len=len_trim(fname)+1) :: c_fname
character(len=len_trim(mode)+1) :: c_string
integer(c_long) :: c_mode
character(len=2)::nullchar
nullchar='0'//c_null_char
c_string=trim(mode)//c_null_char
c_mode=c_strtol(c_string,nullchar,8_c_int)
c_fname=trim(fname)//c_null_char
iret=c_mkdir(c_fname,c_mode)
if(present(exitstat))exitstat=iret
end subroutine f_mkdir
subroutine f_rmdir(fname,exitstat)
character(len=*), intent(in) :: fname
integer, intent(out), optional :: exitstat
integer :: iret
interface
function c_rmdir(fname) bind(c,name='rmdir') result(ret)
import :: c_char,c_int
character(c_char), dimension(*), intent(in) :: fname
integer(c_int) :: ret
end function c_rmdir
end interface
character(len=len_trim(fname)+1) :: c_fname
c_fname=trim(fname)//c_null_char
iret=c_rmdir(c_fname)
if(present(exitstat))exitstat=iret
end subroutine f_rmdir
subroutine f_chdir(fname,exitstat)
character(len=*), intent(in) :: fname
integer, intent(out), optional :: exitstat
integer :: iret
interface
function c_chdir(fname) bind(c,name='chdir') result(ret)
import :: c_char,c_int
character(c_char), dimension(*), intent(in) :: fname
integer(c_int) :: ret
end function c_chdir
end interface
character(len=len_trim(fname)+1) :: c_fname
c_fname=trim(fname)//c_null_char
iret=c_chdir(c_fname)
if(present(exitstat))exitstat=iret
end subroutine f_chdir
subroutine f_exit(exitstat)
integer :: exitstat
interface
subroutine c_exit(exitstat) bind(c,name='exit')
import :: c_int
integer(c_int),value::exitstat
end subroutine c_exit
end interface
call c_exit(exitstat)
end subroutine f_exit
end module syscall