]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/mq_notify.3
libc.7: Add a note on why glibc 2.x uses the soname libc.so.6
[thirdparty/man-pages.git] / man3 / mq_notify.3
CommitLineData
80a99f39 1'\" t
c11b1abf 2.\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
80a99f39 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
80a99f39
MK
5.\" Permission is granted to make and distribute verbatim copies of this
6.\" manual provided the copyright notice and this permission notice are
7.\" preserved on all copies.
8.\"
9.\" Permission is granted to copy and distribute modified versions of this
10.\" manual under the conditions for verbatim copying, provided that the
11.\" entire resulting derived work is distributed under the terms of a
12.\" permission notice identical to this one.
c13182ef 13.\"
80a99f39
MK
14.\" Since the Linux kernel and libraries are constantly changing, this
15.\" manual page may be incorrect or out-of-date. The author(s) assume no
16.\" responsibility for errors or omissions, or for damages resulting from
10d76543
MK
17.\" the use of the information contained herein. The author(s) may not
18.\" have taken the same level of care in the production of this manual,
19.\" which is licensed free of charge, as they might when working
20.\" professionally.
c13182ef 21.\"
80a99f39
MK
22.\" Formatted or processed versions of this manual, if unaccompanied by
23.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 24.\" %%%LICENSE_END
80a99f39 25.\"
97986708 26.TH MQ_NOTIFY 3 2016-03-15 "Linux" "Linux Programmer's Manual"
80a99f39
MK
27.SH NAME
28mq_notify \- register for notification when a message is available
29.SH SYNOPSIS
30.nf
31.B #include <mqueue.h>
32.sp
0d5f6a62 33.BI "int mq_notify(mqd_t " mqdes ", const struct sigevent *" sevp );
80a99f39 34.fi
1b2d3fca
MK
35.sp
36Link with \fI\-lrt\fP.
80a99f39
MK
37.SH DESCRIPTION
38.BR mq_notify ()
c13182ef
MK
39allows the calling process to register or unregister for delivery of
40an asynchronous notification when a new message arrives on
d9cb0d7d 41the empty message queue referred to by the message queue descriptor
80a99f39
MK
42.IR mqdes .
43
c13182ef 44The
0d5f6a62 45.I sevp
c13182ef 46argument is a pointer to a
80a99f39 47.I sigevent
168e3472
MK
48structure.
49For the definition and general details of this structure, see
50.BR sigevent (7).
08140a81 51.PP
c13182ef 52If
0d5f6a62 53.I sevp
b437fdd9 54is a non-null pointer, then
80a99f39
MK
55.BR mq_notify ()
56registers the calling process to receive message notification.
c13182ef
MK
57The
58.I sigev_notify
80a99f39
MK
59field of the
60.I sigevent
168e3472 61structure to which
0d5f6a62 62.I sevp
80a99f39
MK
63points specifies how notification is to be performed.
64This field has one of the following values:
65.TP
66.B SIGEV_NONE
c13182ef 67A "null" notification: the calling process is registered as the target
80a99f39
MK
68for notification, but when a message arrives, no notification is sent.
69.\" When is SIGEV_NONE useful?
70.TP
71.B SIGEV_SIGNAL
72Notify the process by sending the signal specified in
7cd24403 73.IR sigev_signo .
168e3472
MK
74See
75.BR sigevent (7)
76for general details.
77The
80a99f39 78.I si_code
168e3472
MK
79field of the
80.I siginfo_t
81structure will be set to
82.BR SI_MESGQ .
83In addition,
80a99f39
MK
84.\" I don't know of other implementations that set
85.\" si_pid and si_uid -- MTK
86.I si_pid
168e3472 87will be set to the PID of the process that sent the message, and
80a99f39 88.I si_uid
168e3472 89will be set to the real user ID of the sending process.
80a99f39
MK
90.TP
91.B SIGEV_THREAD
168e3472
MK
92Upon message delivery, invoke
93.I sigev_notify_function
94as if it were the start function of a new thread.
95See
96.BR sigevent (7)
97for details.
80a99f39
MK
98.PP
99Only one process can be registered to receive notification
100from a message queue.
101
c13182ef 102If
0d5f6a62 103.I sevp
80a99f39
MK
104is NULL, and the calling process is currently registered to receive
105notifications for this message queue, then the registration is removed;
c13182ef 106another process can then register to receive a message notification
80a99f39
MK
107for this queue.
108
33a0ccb2 109Message notification occurs only when a new message arrives and
80a99f39
MK
110the queue was previously empty.
111If the queue was not empty at the time
112.BR mq_notify ()
33a0ccb2 113was called, then a notification will occur only after
80a99f39
MK
114the queue is emptied and a new message arrives.
115
116If another process or thread is waiting to read a message
117from an empty queue using
fb186734 118.BR mq_receive (3),
c13182ef 119then any message notification registration is ignored:
80a99f39 120the message is delivered to the process or thread calling
fb186734 121.BR mq_receive (3),
80a99f39
MK
122and the message notification registration remains in effect.
123
c13182ef
MK
124Notification occurs once: after a notification is delivered,
125the notification registration is removed,
80a99f39
MK
126and another process can register for message notification.
127If the notified process wishes to receive the next notification,
c13182ef 128it can use
80a99f39
MK
129.BR mq_notify ()
130to request a further notification.
131This should be done before emptying all unread messages from the queue.
ff40dbb3 132(Placing the queue in nonblocking mode is useful for emptying
80a99f39
MK
133the queue of messages without blocking once it is empty.)
134.SH RETURN VALUE
135On success
136.BR mq_notify ()
137returns 0; on error, \-1 is returned, with
c13182ef 138.I errno
80a99f39
MK
139set to indicate the error.
140.SH ERRORS
141.TP
142.B EBADF
d9cb0d7d 143The message queue descriptor specified in
80a99f39
MK
144.I mqdes
145is invalid.
146.TP
147.B EBUSY
148Another process has already registered to receive notification
149for this message queue.
150.TP
151.B EINVAL
0d5f6a62 152.I sevp\->sigev_notify
80a99f39 153is not one of the permitted values; or
0d5f6a62 154.I sevp\->sigev_notify
c13182ef
MK
155is
156.B SIGEV_SIGNAL
157and
0d5f6a62 158.I sevp\->sigev_signo
1954b6a9 159is not a valid signal number.
80a99f39
MK
160.TP
161.B ENOMEM
162Insufficient memory.
8ea4ea95
MK
163.PP
164POSIX.1-2008 says that an implementation
165.I may
166generate an
167.B EINVAL
168.\" Linux does not do this
169error if
0d5f6a62 170.I sevp
8ea4ea95
MK
171is NULL, and the caller is not currently registered to receive
172notifications for the queue
173.IR mqdes .
3964d56d
ZL
174.SH ATTRIBUTES
175For an explanation of the terms used in this section, see
176.BR attributes (7).
177.TS
178allbox;
179lb lb lb
180l l l.
181Interface Attribute Value
182T{
183.BR mq_notify ()
184T} Thread safety MT-Safe
185.TE
186
80a99f39
MK
187.SH CONFORMING TO
188POSIX.1-2001.
c5aee027
MK
189.SH NOTES
190.\"
0722a578 191.SS C library/kernel differences
c5aee027
MK
192In the glibc implementation, the
193.BR mq_notify ()
194library function is implemented on top of the system call of the same name.
195When
196.I sevp
197is NULL, or specifies a notification mechanism other than
198.BR SIGEV_THREAD ,
199the library function directly invokes the system call.
200For
201.BR SIGEV_THREAD ,
202much of the implementation resides within the library,
203rather than the kernel.
204(This is necessarily so,
205since the thread involved in handling the notification is one
206that must be managed by the C library POSIX threads implementation.)
207The implementation involves the use of a raw
208.BR netlink (7)
209socket and creates a new thread for each notification that is
210delivered to the process.
80a99f39
MK
211.SH EXAMPLE
212The following program registers a notification request for the
213message queue named in its command-line argument.
214Notification is performed by creating a thread.
c13182ef 215The thread executes a function which reads one message from the
80a99f39 216queue and then terminates the process.
f30b7415 217.SS Program source
80a99f39 218.nf
80a99f39
MK
219#include <pthread.h>
220#include <mqueue.h>
80a99f39
MK
221#include <stdio.h>
222#include <stdlib.h>
223#include <unistd.h>
224
6a578b88
MK
225#define handle_error(msg) \\
226 do { perror(msg); exit(EXIT_FAILURE); } while (0)
80a99f39
MK
227
228static void /* Thread start function */
229tfunc(union sigval sv)
230{
231 struct mq_attr attr;
232 ssize_t nr;
233 void *buf;
234 mqd_t mqdes = *((mqd_t *) sv.sival_ptr);
235
236 /* Determine max. msg size; allocate buffer to receive msg */
237
29059a65 238 if (mq_getattr(mqdes, &attr) == \-1)
6a578b88 239 handle_error("mq_getattr");
80a99f39 240 buf = malloc(attr.mq_msgsize);
29059a65 241 if (buf == NULL)
6a578b88 242 handle_error("malloc");
80a99f39
MK
243
244 nr = mq_receive(mqdes, buf, attr.mq_msgsize, NULL);
29059a65 245 if (nr == \-1)
6a578b88 246 handle_error("mq_receive");
80a99f39 247
8c5fcd21 248 printf("Read %zd bytes from MQ\\n", nr);
80a99f39
MK
249 free(buf);
250 exit(EXIT_SUCCESS); /* Terminate the process */
251}
252
253int
254main(int argc, char *argv[])
255{
256 mqd_t mqdes;
0d5f6a62 257 struct sigevent sev;
80a99f39 258
6b34fb3f 259 if (argc != 2) {
5a6194a4
MK
260 fprintf(stderr, "Usage: %s <mq\-name>\\n", argv[0]);
261 exit(EXIT_FAILURE);
6b34fb3f 262 }
80a99f39
MK
263
264 mqdes = mq_open(argv[1], O_RDONLY);
29059a65 265 if (mqdes == (mqd_t) \-1)
6a578b88 266 handle_error("mq_open");
80a99f39 267
0d5f6a62
MK
268 sev.sigev_notify = SIGEV_THREAD;
269 sev.sigev_notify_function = tfunc;
270 sev.sigev_notify_attributes = NULL;
271 sev.sigev_value.sival_ptr = &mqdes; /* Arg. to thread func. */
272 if (mq_notify(mqdes, &sev) == \-1)
6a578b88 273 handle_error("mq_notify");
80a99f39
MK
274
275 pause(); /* Process will be terminated by thread function */
276}
277.fi
47297adb 278.SH SEE ALSO
80a99f39
MK
279.BR mq_close (3),
280.BR mq_getattr (3),
281.BR mq_open (3),
282.BR mq_receive (3),
283.BR mq_send (3),
284.BR mq_unlink (3),
168e3472
MK
285.BR mq_overview (7),
286.BR sigevent (7)