]>
Commit | Line | Data |
---|---|---|
d4a28ab0 CW |
1 | /***************************************************************************** |
2 | Excerpt from "Linux Programmer's Guide - Chapter 6" | |
3 | (C)opyright 1994-1995, Scott Burkett | |
4 | ***************************************************************************** | |
5 | MODULE: shmtool.c | |
6 | ***************************************************************************** | |
7 | A command line tool for tinkering with shared memory | |
8 | ||
9 | *****************************************************************************/ | |
bd695173 CW |
10 | /* |
11 | * This program is free software; you can redistribute it and/or | |
12 | * modify it under the terms of the GNU General Public License | |
13 | * as published by the Free Software Foundation; either version 2 | |
14 | * of the License, or (at your option) any later version. | |
15 | * | |
16 | * This program is distributed in the hope that it will be useful, | |
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | * GNU General Public License for more details. | |
20 | * | |
21 | * You should have received a copy of the GNU General Public License | |
22 | * along with this program; if not, write to the Free Software | |
23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
24 | * | |
25 | * See the COPYING file for license information. | |
26 | */ | |
27 | ||
28 | #if HAVE_CONFIG_H | |
29 | # include "config.h" | |
30 | #endif | |
d4a28ab0 | 31 | |
d2b03e6a | 32 | #include "common.h" |
bd695173 CW |
33 | |
34 | #if HAVE_SYS_IPC_H | |
35 | # include <sys/ipc.h> | |
36 | #endif | |
37 | #if HAVE_SYS_SHM_H | |
38 | # include <sys/shm.h> | |
39 | #endif | |
d4a28ab0 CW |
40 | |
41 | #define SEGSIZE 100 | |
42 | ||
43 | main(int argc, char *argv[]) | |
44 | { | |
45 | key_t key; | |
46 | int shmid, cntr; | |
47 | char *segptr; | |
48 | ||
49 | if(argc == 1) | |
50 | usage(); | |
51 | ||
52 | /* Create unique key via call to ftok() */ | |
53 | key = ftok(".", 'S'); | |
54 | ||
55 | /* Open the shared memory segment - create if necessary */ | |
56 | if((shmid = shmget(key, SEGSIZE, IPC_CREAT|IPC_EXCL|0666)) == -1) | |
57 | { | |
58 | printf("Shared memory segment exists - opening as client\n"); | |
59 | ||
60 | /* Segment probably already exists - try as a client */ | |
61 | if((shmid = shmget(key, SEGSIZE, 0)) == -1) | |
62 | { | |
63 | perror("shmget"); | |
64 | exit(1); | |
65 | } | |
66 | } | |
67 | else | |
68 | { | |
69 | printf("Creating new shared memory segment\n"); | |
70 | } | |
71 | ||
72 | /* Attach (map) the shared memory segment into the current process */ | |
73 | if((segptr = shmat(shmid, 0, 0)) == ((char *)-1)) | |
74 | { | |
75 | perror("shmat"); | |
76 | exit(1); | |
77 | } | |
78 | ||
79 | switch(tolower(argv[1][0])) | |
80 | { | |
81 | case 'w': writeshm(shmid, segptr, argv[2]); | |
82 | break; | |
83 | case 'r': readshm(shmid, segptr); | |
84 | break; | |
85 | case 'd': removeshm(shmid); | |
86 | break; | |
87 | case 'm': changemode(shmid, argv[2]); | |
88 | break; | |
89 | default: usage(); | |
90 | ||
91 | } | |
92 | } | |
93 | ||
94 | writeshm(int shmid, char *segptr, char *text) | |
95 | { | |
96 | strcpy(segptr, text); | |
97 | printf("Done...\n"); | |
98 | } | |
99 | ||
100 | readshm(int shmid, char *segptr) | |
101 | { | |
102 | printf("segptr: %s\n", segptr); | |
103 | } | |
104 | ||
105 | removeshm(int shmid) | |
106 | { | |
107 | shmctl(shmid, IPC_RMID, 0); | |
108 | printf("Shared memory segment marked for deletion\n"); | |
109 | } | |
110 | ||
111 | changemode(int shmid, char *mode) | |
112 | { | |
113 | struct shmid_ds myshmds; | |
114 | ||
115 | /* Get current values for internal data structure */ | |
116 | shmctl(shmid, IPC_STAT, &myshmds); | |
117 | ||
118 | /* Display old permissions */ | |
119 | printf("Old permissions were: %o\n", myshmds.shm_perm.mode); | |
120 | ||
121 | /* Convert and load the mode */ | |
122 | sscanf(mode, "%o", &myshmds.shm_perm.mode); | |
123 | ||
124 | /* Update the mode */ | |
125 | shmctl(shmid, IPC_SET, &myshmds); | |
126 | ||
127 | printf("New permissions are : %o\n", myshmds.shm_perm.mode); | |
128 | } | |
129 | ||
130 | usage() | |
131 | { | |
132 | fprintf(stderr, "shmtool - A utility for tinkering with shared memory\n"); | |
133 | fprintf(stderr, "\nUSAGE: shmtool (w)rite <text>\n"); | |
134 | fprintf(stderr, " (r)ead\n"); | |
135 | fprintf(stderr, " (d)elete\n"); | |
136 | fprintf(stderr, " (m)ode change <octal mode>\n"); | |
137 | exit(1); | |
138 | } | |
139 |