-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshmtool.c
More file actions
96 lines (89 loc) · 2.2 KB
/
shmtool.c
File metadata and controls
96 lines (89 loc) · 2.2 KB
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
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#define SEGSIZE 100
main (int argc, char *argv[])
{
key_t key;
int shmid, cntr;
char *segptr;
if (argc == 1)
usage ();
/* Create unique key via call to ftok() */
key = ftok (".", 'S');
/* Open the shared memory segment - create if necessary */
if ((shmid = shmget (key, SEGSIZE, IPC_CREAT | IPC_EXCL | 0666)) == -1)
{
printf ("Shared memory segment exists - opening as client\n");
/* Segment probably already exists - try as a client */
if ((shmid = shmget (key, SEGSIZE, 0)) == -1)
{
perror ("shmget");
exit (1);
}
}
else
{
}
printf ("Creating new shared memory segment\n");
/* Attach (map) the shared memory segment into the current process */
if ((segptr = shmat (shmid, 0, 0)) == -1)
{
perror ("shmat");
exit (1);
}
switch (tolower (argv[1][0]))
{
case 'w':
writeshm (shmid, segptr, argv[2]);
break;
case 'r':
readshm (shmid, segptr);
break;
case 'd':
removeshm (shmid);
break;
case 'm':
changemode (shmid, argv[2]);
break;
default:
usage ();
}
}
writeshm (int shmid, char *segptr, char *text)
{
strcpy (segptr, text);
printf ("Done...\n");
}
readshm (int shmid, char *segptr)
{
printf ("segptr: %s\n", segptr);
}
removeshm (int shmid)
{
shmctl (shmid, IPC_RMID, 0);
printf ("Shared memory segment marked for deletion\n");
}
changemode (int shmid, char *mode)
{
struct shmid_ds myshmds;
/* Get current values for internal data structure */
shmctl (shmid, IPC_STAT, &myshmds);
/* Display old permissions */
printf ("Old permissions were: %o\n", myshmds.shm_perm.mode);
/* Convert and load the mode */
sscanf (mode, "%o", &myshmds.shm_perm.mode);
/* Update the mode */
shmctl (shmid, IPC_SET, &myshmds);
printf ("New permissions are : %o\n", myshmds.shm_perm.mode);
}
usage ()
{
fprintf (stderr, "shmtool - A utility for tinkering with shared memory\n");
fprintf (stderr, "\nUSAGE: shmtool (w)rite <text>\n");
fprintf (stderr, " (r)ead\n");
fprintf (stderr, " (d)elete\n");
fprintf (stderr, " (m)ode change <octal mode>\n");
exit (1);
}