File: sharedlib.c

package info (click to toggle)
emoslib 2%3A4.4.5-2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 359,232 kB
  • ctags: 13,125
  • sloc: fortran: 93,166; ansic: 27,958; sh: 7,500; f90: 5,209; perl: 604; cpp: 305; makefile: 78; python: 53
file content (425 lines) | stat: -rw-r--r-- 9,430 bytes parent folder | download | duplicates (6)
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
417
418
419
420
421
422
423
424
425
/**
* Copyright 1981-2016 ECMWF.
*
* This software is licensed under the terms of the Apache Licence
* Version 2.0 which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>

#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>

#include <sys/sem.h>

#include "sharedlib.h"

/* set the shared memory debug output according to env var */
int sharedlib_dbg()
{
    char* var = getenv("EMOSLIB_SHM_DEBUG");
    return (var != NULL) && (strlen(var) != 0) ? 1 : 0;
}

/* 10 MB */

#define BUFFER_SIZE 10485760
#define MAGIC       1234567890

struct sembuf _lock[] = {
    { 0, 0,  SEM_UNDO }, /* test */
    { 0, 1,  SEM_UNDO }, /* lock */
};

struct sembuf _unlock[] = {
    { 0, -1, SEM_UNDO }, /* ulck */
};

#define INFO_PATH 1024

struct info {
    int ready;
    int magic;
    char path[INFO_PATH];
};



void *share_file(const char* file) {
    char message[1024];
    char path[1024];

    struct  stat64 s;
    struct  stat64 s2;
    int fd = -1;
    int err = 0;
    void *ptr = NULL;
    int shmid = 0;
    key_t key;
    char *addr;
    struct timeval start, end, diff;
    double time;
    int sem;
    int loadfile = 1;
    int locked = 0;
    int page_size = getpagesize();
    struct info *nfo;
    int semcount = 0;

    if(page_size < 0)
    {
        fprintf(stderr,"ERR: sharedmem:get_page_size faile\n");
        return NULL;
    }

    if(strlen(file) + 1 > INFO_PATH)
    {
        fprintf(stderr,"ERR: sharedmem:path too long(%lu) max is %d\n",strlen(file), INFO_PATH);
        return NULL;
    }

    if(strlen(file) + 1 > sizeof(path))
    {
        fprintf(stderr,"ERR: sharedmem:path too long(%lu) max is %lu\n",strlen(file), sizeof(path));
        return NULL;
    }

    if(realpath(file,path) == 0) {
        sprintf(message,"ERR: sharedmem:realpath(%s)",file);
        err = -1;
        goto error;
    }

    if (sharedlib_dbg())
        fprintf(stdout,"sharedmem: sharing %s\n",path);

    if(sizeof(s.st_size) < 8) {
        fprintf(stderr,"ERR: sharedmem:stat.st_size(%lu) is too small for 64bits files\n",sizeof(s.st_size));
        return NULL;
    }


    key = ftok(path,1);

    if(key ==  (key_t)-1) {
        sprintf(message,"ERR: sharedmem:ftok(%s)",path);
        err = -1;
        goto error;
    }


    if((sem = semget(key,1,IPC_CREAT|0600)) < 0)
    {
        sprintf(message,"ERR: sharedmem:semget(%s)",path);
        err = -1;
        goto error;
    }

    if (sharedlib_dbg()) gettimeofday( &start, NULL );

    while (semcount < 30) {
    if(semop(sem,_lock, 2 ) < 0) {

        semcount++;
        sprintf(message,"ERR: sharedmem:semop:lock(%s)",path);
        if (semcount >= 30) {
        err = -1;
        goto error;
        }
        else
        {
           sprintf(message,"WARNING: %d sharedmem:semop:lock(%s)",semcount,path);
           perror(message);
           sleep(2);
           semcount++;
        }
    }
    else
    {
        break;
    }
    }
    locked = 1;

    if (sharedlib_dbg())
    {
        gettimeofday( &end, NULL );

        diff.tv_sec  = end.tv_sec  - start.tv_sec;
        diff.tv_usec = end.tv_usec - start.tv_usec;

        if (diff.tv_usec < 0)
        {
            diff.tv_sec--;
            diff.tv_sec--;
            diff.tv_usec += 1000000;
        }
        time = (double)diff.tv_sec + ((double)diff.tv_usec / 1000000.);

        fprintf( stdout, "sharedmem:semop:lock wait %g secs\n", time);
    }

#ifdef O_LARGEFILE
    if((fd = open(path,O_RDONLY | O_LARGEFILE))  < 0)
#else
    if((fd = open(path,O_RDONLY))  < 0)
#endif
    {

        sprintf(message,"ERR: sharedmem:open(%s)",path);
        err = -1;
        goto error;
    }

    if(stat64(path,&s))
    {
        sprintf(message,"ERR: sharedmem:stat64(%s)",path);
        err = -1;
        goto error;
    }

    size_t shmsize = ((s.st_size + page_size-1)/page_size)*page_size + sizeof(struct info) ;

    if (sharedlib_dbg())
        fprintf(stdout,"sharedmem: calling shmget for key %d of size %ld with page_size %d \n",key, shmsize, page_size);
    if (sharedlib_dbg())
        fprintf(stdout,"sharedmem: sizeof(struct info)=%ld \n", sizeof(struct info));


    if((shmid = shmget(key, shmsize ,IPC_CREAT|0600)) < 0)
    {
        sprintf(message,"ERR: sharedmem:shmget(%s) key = %d shmsize = %ld ",path, key, shmsize);
        err = -1;
        goto error;
    }
    if (sharedlib_dbg())
        fprintf(stdout,"sharedmem: shmget for key %d returns shmid=%d \n",key, shmid);


#ifdef SHM_PAGESIZE
    {

    /* Use 64K pages to back the shared memory region */
    size_t shm_size;
    struct shmid_ds shm_buf = { 0 };
    psize_t psize_64k;
    psize_64k = 64 * 1024;

    shm_buf.shm_pagesize = psize_64k;
    if (shmctl(shmid, SHM_PAGESIZE, &shm_buf))
    {
        /*perror("shmctl(SHM_PAGESIZE) failed");*/
    }
    }

#endif

    /* attach shared memory */

    ptr = shmat( shmid, NULL, 0 );
    if (ptr == (void*)-1) {
        sprintf(message,"sharedmem:shmat(%s)",path);
        err = -1;
        goto error;
    }

    addr = (char*)ptr;
    if (sharedlib_dbg())
        fprintf(stdout,"sharedmem: shmat for ptr %p\n",(void *) ptr);

    nfo  = (struct info*)(addr + (((s.st_size + page_size-1)/page_size)*page_size));

    if(nfo->ready) {
        loadfile = 0;
        if(nfo->magic != MAGIC)
        {
            sprintf(message,"ERR: sharedmem:check: bad magic %d\n",nfo->magic);
            err = -1;
            goto error;
        }

        if(strcmp(nfo->path,path) != 0)
        {
            sprintf(message,"ERR: sharedmem:check: invalid path [%s]\n",nfo->path);
            err = -1;
            goto error;
        }
    }


    if(loadfile) {

        s2.st_size =  s.st_size;

        if (sharedlib_dbg()) gettimeofday( &start, NULL );

        while(s.st_size > 0)
        {
            size_t len = s.st_size > BUFFER_SIZE ? BUFFER_SIZE : s.st_size;
            if(read(fd, addr, len) != len) {
                sprintf(message,"ERR: sharedmem:read(%s)",path);
                err = -1;
                goto error;
            }
            s.st_size -= len;
            addr      += len;
        }

        if (sharedlib_dbg())
        {
            gettimeofday( &end, NULL );

            diff.tv_sec  = end.tv_sec  - start.tv_sec;
            diff.tv_usec = end.tv_usec - start.tv_usec;

            if (diff.tv_usec < 0)
            {
                diff.tv_sec--;
                diff.tv_usec += 1000000;
            }
            time = (double)diff.tv_sec + ((double)diff.tv_usec / 1000000.);

            fprintf( stdout, "sharedmem:read %lld bytes in %g secs\n",s2.st_size, time);
        }

        nfo->magic = MAGIC;
        strcpy(nfo->path,path);
        nfo->ready = 1;
    }
    else
    {
        if (sharedlib_dbg())
        fprintf( stdout, "sharedmem:read file already loaded\n");
    }

    close(fd);

error:

    if(fd>0) {
        close(fd);
    }

    if(err) {
        perror(message);
        if(ptr) shmdt(ptr);
        ptr = NULL;

    }

    if(locked) {
    semcount = 0;
    while (semcount < 30) {
        if(semop(sem,_unlock,1) < 0)
        {
            if (semcount >= 30) {
                sprintf(message,"ERR: sharedmem:UNLOCK semop:unlock(%s)",path);
                perror(message);
                break;
            }
            else
            {
               sprintf(message,"WARNING: %d sharedmem:UNLOCK semop:unlock(%s)",semcount,path);
               perror(message);
               sleep(2);
               semcount++;
            }
       } else {
            break;
       }
   }

   }

    return ptr;
}

int release_shared_file(void *ptr) {
    int err = 0 ;
    err = shmdt(ptr);
    return err ;
}

int remove_shared_file(const char* file) {
    char message[1024];
    char path[1024];
    int err = 0;
    int shmid = 0;
    key_t key;
    int sem;

    if(strlen(file) + 1 > sizeof(path))
    {
        fprintf(stderr,"ERR: sharedmem:path too long(%lu) max is %lu\n",strlen(file), sizeof(path));
        return -1;
    }

    if(realpath(file,path) == 0) {
        sprintf(message,"ERR: sharedmem:realpath(%s)",file);
        err = -1;
        goto error;
    }

    key  = ftok(path,1);

    if(key ==  (key_t)-1) {
        sprintf(message,"ERR: sharedmem:ftok(%s)",path);
        err = -1;
        goto error;
    }

    if((shmid = shmget(key,0,0600)) < 0)
    {
        sprintf(message,"ERR: sharedmem:shmget(%s)",path);
        err = -1;
        goto error;
    }

    fprintf(stdout,"sharedmem: removing shared memory for %s\n",path);

    if(shmctl(shmid, IPC_RMID, NULL) < 0)
    {
        sprintf(message,"ERR: sharedmem:shmctl:IPC_RMID,(%s)",path);
        err = -1;
        goto error;
    }


    if((sem = semget(key,1,0600)) <0 )
    {
        sprintf(message,"ERR: sharedmem:semget(%s)",path);
        err = -1;
        goto error;
    }

    if(semctl(sem, 0, IPC_RMID, NULL) < 0)
    {
        sprintf(message,"ERR: sharedmem:semctl:IPC_RMID,(%s)",path);
        err = -1;
        goto error;
    }



error:



    if(err)
        perror(message);

    return err;

}