]> cygwin.com Git - cygwin-apps/cygutils.git/blame - src/ipc/msgtool.c
Convert many programs to GPLv3+.
[cygwin-apps/cygutils.git] / src / ipc / msgtool.c
CommitLineData
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 9/*
7156ebbc
CW
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
bd695173
CW
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
7156ebbc 21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
bd695173 22 *
7156ebbc 23 * See the COPYING file for full license information.
bd695173
CW
24 */
25
26#if HAVE_CONFIG_H
fe3a7d70 27# include "config.h"
bd695173 28#endif
d4a28ab0 29
d2b03e6a 30#include "common.h"
bd695173
CW
31
32#if HAVE_SYS_IPC_H
fe3a7d70 33# include <sys/ipc.h>
bd695173
CW
34#endif
35#if HAVE_SYS_MSG_H
fe3a7d70 36# include <sys/msg.h>
bd695173 37#endif
d4a28ab0
CW
38
39#define MAX_SEND_SIZE 80
40
fe3a7d70
CW
41struct mymsgbuf
42{
43 long mtype;
44 char mtext[MAX_SEND_SIZE];
d4a28ab0
CW
45};
46
fe3a7d70
CW
47void send_message (int qid, struct mymsgbuf *qbuf, long type, char *text);
48void read_message (int qid, struct mymsgbuf *qbuf, long type);
49void remove_queue (int qid);
50void change_queue_mode (int qid, char *mode);
51void usage (void);
d4a28ab0
CW
52
53
fe3a7d70
CW
54int
55main (int argc, char *argv[])
d4a28ab0 56{
fe3a7d70
CW
57 key_t key;
58 int msgqueue_id;
59 struct mymsgbuf qbuf;
60
61 if (argc == 1)
62 usage ();
63
64 /* Create unique key via call to ftok() */
65 key = ftok (".", 'm');
66
67 /* Open the queue - create if necessary */
68 if ((msgqueue_id = msgget (key, IPC_CREAT | 0660)) == -1)
69 {
70 perror ("msgget");
71 exit (1);
72 }
73
74 switch (tolower (argv[1][0]))
75 {
76 case 's':
77 send_message (msgqueue_id, (struct mymsgbuf *) &qbuf,
78 atol (argv[2]), argv[3]);
79 break;
80 case 'r':
81 read_message (msgqueue_id, &qbuf, atol (argv[2]));
82 break;
83 case 'd':
84 remove_queue (msgqueue_id);
85 break;
86 case 'm':
87 change_queue_mode (msgqueue_id, argv[2]);
88 break;
89
90 default:
91 usage ();
92
93 }
94
95 return (0);
d4a28ab0
CW
96}
97
fe3a7d70
CW
98void
99send_message (int qid, struct mymsgbuf *qbuf, long type, char *text)
d4a28ab0 100{
fe3a7d70
CW
101 /* Send a message to the queue */
102 printf ("Sending a message ...\n");
103 qbuf->mtype = type;
104 strcpy (qbuf->mtext, text);
105
106 if ((msgsnd (qid, (struct msgbuf *) qbuf,
107 strlen (qbuf->mtext) + 1, 0)) == -1)
108 {
109 perror ("msgsnd");
110 exit (1);
111 }
d4a28ab0
CW
112}
113
fe3a7d70
CW
114void
115read_message (int qid, struct mymsgbuf *qbuf, long type)
d4a28ab0 116{
fe3a7d70
CW
117 /* Read a message from the queue */
118 printf ("Reading a message ...\n");
119 qbuf->mtype = type;
120 msgrcv (qid, (struct msgbuf *) qbuf, MAX_SEND_SIZE, type, 0);
121
122 printf ("Type: %ld Text: %s\n", qbuf->mtype, qbuf->mtext);
d4a28ab0
CW
123}
124
fe3a7d70
CW
125void
126remove_queue (int qid)
d4a28ab0 127{
fe3a7d70
CW
128 /* Remove the queue */
129 msgctl (qid, IPC_RMID, 0);
d4a28ab0
CW
130}
131
fe3a7d70
CW
132void
133change_queue_mode (int qid, char *mode)
d4a28ab0 134{
fe3a7d70 135 struct msqid_ds myqueue_ds;
d4a28ab0 136
fe3a7d70
CW
137 /* Get current info */
138 msgctl (qid, IPC_STAT, &myqueue_ds);
d4a28ab0 139
fe3a7d70
CW
140 /* Convert and load the mode */
141 sscanf (mode, "%ho", &myqueue_ds.msg_perm.mode);
d4a28ab0 142
fe3a7d70
CW
143 /* Update the mode */
144 msgctl (qid, IPC_SET, &myqueue_ds);
d4a28ab0
CW
145}
146
fe3a7d70
CW
147void
148usage (void)
d4a28ab0 149{
fe3a7d70
CW
150 fprintf (stderr, "msgtool - A utility for tinkering with msg queues\n");
151 fprintf (stderr, "\nUSAGE: msgtool (s)end <type> <messagetext>\n");
152 fprintf (stderr, " (r)ecv <type>\n");
153 fprintf (stderr, " (d)elete\n");
154 fprintf (stderr, " (m)ode <octal mode>\n");
155 exit (1);
d2b03e6a 156}
This page took 0.038577 seconds and 5 git commands to generate.