| Commit | Line | Data |
|---|---|---|
| 322a3d9c SK |
1 | /* |
| 2 | * Copyright (c) 2009, Stathis Kamperis | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * | |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 15 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 16 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 17 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 18 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 20 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 22 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 24 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 25 | * SUCH DAMAGE. | |
| 26 | */ | |
| 27 | ||
| 8d564867 SK |
28 | #include <assert.h> |
| 29 | #include <errno.h> | |
| 30 | #include <fcntl.h> | |
| 31 | #include <mqueue.h> | |
| 32 | #include <stdio.h> | |
| 33 | #include <stdlib.h> | |
| 322a3d9c | 34 | #include <string.h> /* for memset() */ |
| 8d564867 | 35 | |
| 322a3d9c | 36 | #define MQNAME "/t_mq_setattr" |
| 8d564867 SK |
37 | |
| 38 | int main(void) | |
| 39 | { | |
| 3b54a39a SK |
40 | /* Invalid (or so we hope) message queue descriptor. */ |
| 41 | struct mq_attr attr, curattr; | |
| 8d564867 | 42 | |
| bb24b42b | 43 | assert(mq_setattr((mqd_t)-1, &attr, NULL ) == -1 && errno == EBADF); |
| 44 | assert(mq_setattr((mqd_t)-1, &attr, &curattr) == -1 && errno == EBADF); | |
| 8d564867 | 45 | |
| 3b54a39a SK |
46 | /* |
| 47 | * mq_getattr() must report the same exact values as those we provided | |
| 48 | * to mq_open() upon the queue creation. | |
| 49 | */ | |
| 8d564867 | 50 | memset(&attr, 0, sizeof(attr)); |
| 3b54a39a SK |
51 | attr.mq_maxmsg = 10; /* Maximum number of messages. */ |
| 52 | attr.mq_msgsize = 1024; /* Maximum message size. */ | |
| 53 | ||
| 54 | mqd_t md; | |
| 8d564867 | 55 | md = mq_open(MQNAME, O_CREAT | O_EXCL | O_RDWR, 0700, &attr); |
| 3b54a39a SK |
56 | assert(md != (mqd_t)-1); |
| 57 | ||
| 58 | assert(mq_getattr(md, &curattr) != -1); | |
| 59 | assert(curattr.mq_maxmsg == 10); | |
| 60 | assert(curattr.mq_msgsize == 1024); | |
| 61 | assert(curattr.mq_curmsgs == 0); | |
| 8d564867 | 62 | |
| 3b54a39a SK |
63 | /* |
| 64 | * Use mq_setattr() to set attributes. | |
| 65 | * POSIX says that the values of the mq_maxmsg, mq_msgsize, and | |
| 66 | * mq_curmsgs members of the mq_attr structure shall be ignored by | |
| 67 | * mq_setattr(). | |
| 68 | */ | |
| 69 | struct mq_attr newattr, oldattr; | |
| 8d564867 | 70 | |
| 3b54a39a SK |
71 | newattr.mq_maxmsg = 101; |
| 72 | newattr.mq_msgsize = 3000; | |
| 73 | newattr.mq_curmsgs = 2; | |
| 74 | assert(mq_setattr(md, &newattr, &oldattr) != -1); | |
| 75 | ||
| 76 | assert(oldattr.mq_maxmsg == 10); | |
| 77 | assert(oldattr.mq_msgsize == 1024); | |
| 78 | assert(oldattr.mq_curmsgs == 0); | |
| 79 | ||
| 80 | assert(curattr.mq_maxmsg == 10); | |
| 81 | assert(curattr.mq_msgsize == 1024); | |
| 82 | assert(curattr.mq_curmsgs == 0); | |
| 83 | ||
| 84 | /* Disassociate message queue from system. */ | |
| 322a3d9c | 85 | assert(mq_close(md) != -1); |
| 3b54a39a SK |
86 | |
| 87 | /* Remove message queue from system. */ | |
| 322a3d9c | 88 | assert(mq_unlink(MQNAME) != -1); |
| 8d564867 SK |
89 | |
| 90 | printf("passed\n"); | |
| 91 | ||
| 92 | return (EXIT_SUCCESS); | |
| 93 | } |