]>
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: msgtool.c | |
6 | ***************************************************************************** | |
7 | A command line tool for tinkering with SysV style Message Queues | |
8 | *****************************************************************************/ | |
bd695173 CW |
9 | /* |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public License | |
12 | * as published by the Free Software Foundation; either version 2 | |
13 | * of the License, or (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
23 | * | |
24 | * See the COPYING file for license information. | |
25 | */ | |
26 | ||
27 | #if HAVE_CONFIG_H | |
fe3a7d70 | 28 | # include "config.h" |
bd695173 | 29 | #endif |
d4a28ab0 | 30 | |
d2b03e6a | 31 | #include "common.h" |
bd695173 CW |
32 | |
33 | #if HAVE_SYS_IPC_H | |
fe3a7d70 | 34 | # include <sys/ipc.h> |
bd695173 CW |
35 | #endif |
36 | #if HAVE_SYS_MSG_H | |
fe3a7d70 | 37 | # include <sys/msg.h> |
bd695173 | 38 | #endif |
d4a28ab0 CW |
39 | |
40 | #define MAX_SEND_SIZE 80 | |
41 | ||
fe3a7d70 CW |
42 | struct mymsgbuf |
43 | { | |
44 | long mtype; | |
45 | char mtext[MAX_SEND_SIZE]; | |
d4a28ab0 CW |
46 | }; |
47 | ||
fe3a7d70 CW |
48 | void send_message (int qid, struct mymsgbuf *qbuf, long type, char *text); |
49 | void read_message (int qid, struct mymsgbuf *qbuf, long type); | |
50 | void remove_queue (int qid); | |
51 | void change_queue_mode (int qid, char *mode); | |
52 | void usage (void); | |
d4a28ab0 CW |
53 | |
54 | ||
fe3a7d70 CW |
55 | int |
56 | main (int argc, char *argv[]) | |
d4a28ab0 | 57 | { |
fe3a7d70 CW |
58 | key_t key; |
59 | int msgqueue_id; | |
60 | struct mymsgbuf qbuf; | |
61 | ||
62 | if (argc == 1) | |
63 | usage (); | |
64 | ||
65 | /* Create unique key via call to ftok() */ | |
66 | key = ftok (".", 'm'); | |
67 | ||
68 | /* Open the queue - create if necessary */ | |
69 | if ((msgqueue_id = msgget (key, IPC_CREAT | 0660)) == -1) | |
70 | { | |
71 | perror ("msgget"); | |
72 | exit (1); | |
73 | } | |
74 | ||
75 | switch (tolower (argv[1][0])) | |
76 | { | |
77 | case 's': | |
78 | send_message (msgqueue_id, (struct mymsgbuf *) &qbuf, | |
79 | atol (argv[2]), argv[3]); | |
80 | break; | |
81 | case 'r': | |
82 | read_message (msgqueue_id, &qbuf, atol (argv[2])); | |
83 | break; | |
84 | case 'd': | |
85 | remove_queue (msgqueue_id); | |
86 | break; | |
87 | case 'm': | |
88 | change_queue_mode (msgqueue_id, argv[2]); | |
89 | break; | |
90 | ||
91 | default: | |
92 | usage (); | |
93 | ||
94 | } | |
95 | ||
96 | return (0); | |
d4a28ab0 CW |
97 | } |
98 | ||
fe3a7d70 CW |
99 | void |
100 | send_message (int qid, struct mymsgbuf *qbuf, long type, char *text) | |
d4a28ab0 | 101 | { |
fe3a7d70 CW |
102 | /* Send a message to the queue */ |
103 | printf ("Sending a message ...\n"); | |
104 | qbuf->mtype = type; | |
105 | strcpy (qbuf->mtext, text); | |
106 | ||
107 | if ((msgsnd (qid, (struct msgbuf *) qbuf, | |
108 | strlen (qbuf->mtext) + 1, 0)) == -1) | |
109 | { | |
110 | perror ("msgsnd"); | |
111 | exit (1); | |
112 | } | |
d4a28ab0 CW |
113 | } |
114 | ||
fe3a7d70 CW |
115 | void |
116 | read_message (int qid, struct mymsgbuf *qbuf, long type) | |
d4a28ab0 | 117 | { |
fe3a7d70 CW |
118 | /* Read a message from the queue */ |
119 | printf ("Reading a message ...\n"); | |
120 | qbuf->mtype = type; | |
121 | msgrcv (qid, (struct msgbuf *) qbuf, MAX_SEND_SIZE, type, 0); | |
122 | ||
123 | printf ("Type: %ld Text: %s\n", qbuf->mtype, qbuf->mtext); | |
d4a28ab0 CW |
124 | } |
125 | ||
fe3a7d70 CW |
126 | void |
127 | remove_queue (int qid) | |
d4a28ab0 | 128 | { |
fe3a7d70 CW |
129 | /* Remove the queue */ |
130 | msgctl (qid, IPC_RMID, 0); | |
d4a28ab0 CW |
131 | } |
132 | ||
fe3a7d70 CW |
133 | void |
134 | change_queue_mode (int qid, char *mode) | |
d4a28ab0 | 135 | { |
fe3a7d70 | 136 | struct msqid_ds myqueue_ds; |
d4a28ab0 | 137 | |
fe3a7d70 CW |
138 | /* Get current info */ |
139 | msgctl (qid, IPC_STAT, &myqueue_ds); | |
d4a28ab0 | 140 | |
fe3a7d70 CW |
141 | /* Convert and load the mode */ |
142 | sscanf (mode, "%ho", &myqueue_ds.msg_perm.mode); | |
d4a28ab0 | 143 | |
fe3a7d70 CW |
144 | /* Update the mode */ |
145 | msgctl (qid, IPC_SET, &myqueue_ds); | |
d4a28ab0 CW |
146 | } |
147 | ||
fe3a7d70 CW |
148 | void |
149 | usage (void) | |
d4a28ab0 | 150 | { |
fe3a7d70 CW |
151 | fprintf (stderr, "msgtool - A utility for tinkering with msg queues\n"); |
152 | fprintf (stderr, "\nUSAGE: msgtool (s)end <type> <messagetext>\n"); | |
153 | fprintf (stderr, " (r)ecv <type>\n"); | |
154 | fprintf (stderr, " (d)elete\n"); | |
155 | fprintf (stderr, " (m)ode <octal mode>\n"); | |
156 | exit (1); | |
d2b03e6a | 157 | } |