-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshm.c
120 lines (100 loc) · 2.38 KB
/
shm.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
/* Rhizo-uuardop: Tools to integrate Ardop to UUCP
* Copyright (C) 2019 Rhizomatica
* Author: Rafael Diniz <rafael@riseup.net>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*
* Routines to call uucico when receiving a call
*/
/**
* @file shm.c
* @author Rafael Diniz
* @date 14 Aug 2019
* @brief Shared memory routines
*
* Shared memory routines
*
*/
#include <sys/ioctl.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <signal.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/inotify.h>
#include <libgen.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <grp.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/mman.h>
#include "shm.h"
bool shm_is_created(key_t key, size_t size)
{
int shmid = shmget(key, size, 0);
if (shmid == -1)
{
return false;
}
return true;
}
// check of key is already not created before calling this!
bool shm_create(key_t key, size_t size)
{
int shmid = shmget(key, size, 0666 | IPC_CREAT | IPC_EXCL);
if (shmid == -1)
{
return false;
}
return true;
}
bool shm_destroy(key_t key, size_t size)
{
int shmid = shmget(1, size, 0);
if (shmid == -1)
{
return false;
}
shmctl(shmid,IPC_RMID,NULL);
return true;
}
void *shm_attach(key_t key, size_t size)
{
int shmid = shmget(key, size, 0);
if (shmid == -1)
{
return NULL;
}
return shmat(shmid, NULL, 0);
}
bool shm_dettach(key_t key, size_t size, void *ptr)
{
int shmid = shmget(key, size, 0);
if (shmid == -1)
{
return false;
}
shmdt(ptr);
return true;
}