-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathegs_c_utils_unix.c
187 lines (170 loc) · 5.46 KB
/
egs_c_utils_unix.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
/*
###############################################################################
#
# EGSnrc file locking functions for unix
# Copyright (C) 2015 National Research Council Canada
#
# This file is part of EGSnrc.
#
# EGSnrc is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# EGSnrc 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 Affero General Public License for
# more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
#
# Author: Iwan Kawrakow, 2003
#
# Contributors: Ernesto Mainegra-Hing
#
###############################################################################
*/
#include "egs_c_utils.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
static int __my_fd = -1;
static int __is_locked = 0;
static int __is_initialized = 0;
struct flock fl_write, fl_unlock;
/*! Init the lock/unlock structures */
void __init_locking() {
#ifdef DEBUG
fprintf( stderr,"__init_locking()\n");
#endif
fl_write.l_type = F_WRLCK;
fl_write.l_whence = SEEK_SET;
fl_write.l_start = 0;
fl_write.l_len = 0;
fl_unlock.l_type = F_UNLCK;
fl_unlock.l_whence = SEEK_SET;
fl_unlock.l_start = 0;
fl_unlock.l_len = 0;
__is_initialized = 1;
}
void egsCreateControlFile(const char *fname, int *status, int len) {
#ifdef DEBUG
fprintf( stderr,"create_control_file: file name = %s\n",fname);
#endif
if( !__is_initialized ) __init_locking();
*status = 0;
if( __my_fd > 0 ) { close(__my_fd); __my_fd = -1; }
__my_fd = open(fname,O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
if( __my_fd < 0 ) {
#ifdef DEBUG
fprintf( stderr,"create_control_file: failed to open file %d\n",errno);
perror("error is");
#endif
*status = errno; return;
}
egsLockControlFile(status);
}
void egsOpenControlFile(const char *fname, int *status, int len) {
int t; *status = 0;
if( !__is_initialized ) __init_locking();
if( __my_fd > 0 ) { close(__my_fd); __my_fd = -1; }
/* This function is called from jobs > 1. Job 1 creates the control file.
But if for some reasons it took job 1 longer than subsequent job,
the control file may not be there yet => we try several times and
sleep in between.
*/
for(t=0; t<15; t++) {
__my_fd = open(fname,O_RDWR); if( __my_fd > 0 ) break;
sleep(1);
}
if( __my_fd < 0 ) *status = errno;
}
void egsCloseControlFile(int *status) {
if( __my_fd > 0 ) *status = close(__my_fd); else *status = 0;
}
void egsLockControlFile(int *status) {
int i1,i2;
#ifdef DEBUG
fprintf(stderr,"lock_control_file: fd = %d is_locked = %d\n",__my_fd,
__is_locked);
#endif
if( __is_locked == 1 ) { *status = 0; return; }
if( __my_fd < 0 ) { *status = -1; return; }
for(i1=0; i1<5; i1++) {
for(i2=0; i2<12; i2++) {
*status = fcntl(__my_fd,F_SETLK,&fl_write);
if( *status == 0 ) { __is_locked = 1; return; }
sleep(1);
}
printf("egsLockControlFile: failed to lock file for 12 seconds...\n");
}
/*
*status = fcntl(__my_fd,F_SETLKW,&fl_write);
if( *status == 0 ) __is_locked = 1;
*/
#ifdef DEBUG
if ( *status != 0 ) perror("error was");
#endif
printf("egsLockControlFile: failed to lock file after 1 minute wait!\n");
}
void egsUnlockControlFile(int *status) {
#ifdef DEBUG
fprintf(stderr,"unlock_control_file: fd = %d is_locked = %d\n",__my_fd,
__is_locked);
#endif
if( __is_locked == 0 ) { *status = 0; return; }
if( __my_fd < 0 ) { *status = -1; return; }
*status = fcntl(__my_fd,F_SETLKW,&fl_unlock);
#ifdef DEBUG
if ( *status != 0 ) perror("error was");
#endif
if( *status == 0 ) __is_locked = 0;
}
void egsRewindControlFile(int *status) {
if( __my_fd < 0 ) { *status = -1; return; }
*status=0;
if( __is_locked == 0 ) egsLockControlFile(status);
if( *status != 0 ) return;
*status = lseek(__my_fd,0,SEEK_SET);
}
void egsWriteControlFile(const char *buf, const int *n, int *status, int len) {
if( __my_fd < 0 ) { *status = 0; return; }
*status = write(__my_fd,buf,*n);
}
void egsReadControlFile(char *buf, const int *n, int *status, int len) {
if( __my_fd < 0 ) { *status = 0; return; }
*status = read(__my_fd,buf,*n);
}
void egsSleep(const int *secs) { sleep(*secs); }
void egsFtoString(const int *size, int *n, char *str,void *a, int len) {
size_t x = *n;
if( *size == 4 ) {
float *tmp = (float *) a;
/* Well, it seems snprintf is not supported by all Unixes (e.g. DEC).
I'm too lazy to make yet another test during the installation =>
just use sprintf instead.
if( x > 0 ) *n = snprintf(str,x,"%g",*tmp);
else *n = sprintf(str,"%g",*tmp); */
*n = sprintf(str,"%g",*tmp);
}
else if( *size == 8 ) {
double *tmp = (double *) a;
/*
if( x > 0 ) *n = snprintf(str,x,"%lg",*tmp);
else *n = sprintf(str,"%lg",*tmp);
*/
*n = sprintf(str,"%lg",*tmp);
}
else *n = 0;
}
void egsPerror(const char *str, int len) { perror(str); }
void egsRemoveFile(const char *fname, int *status, int len) {
*status = unlink(fname);
}