📄 mqueue1.c
字号:
/*========================================================================
//
// mqueue1.c
//
// POSIX Message queues tests
//
//========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or inline functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. However the source code for this file must still be made available
// in accordance with section (3) of the GNU General Public License.
//
// This exception does not invalidate any other reasons why a work based on
// this file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): jlarmour
// Contributors:
// Date: 2000-05-18
// Purpose: This file provides tests for POSIX mqueues
// Description:
// Usage:
//
//####DESCRIPTIONEND####
//
//======================================================================
*/
/* CONFIGURATION */
#include <pkgconf/posix.h>
#ifndef CYGPKG_POSIX_MQUEUES
# define NA_MSG "Message queues not configured"
#endif
#ifdef NA_MSG
#include <cyg/infra/testcase.h> // test API
void
cyg_user_start(void)
{
CYG_TEST_NA( NA_MSG );
}
#else
/* INCLUDES */
#include <fcntl.h> // O_*
#include <errno.h> // errno
#include <sys/stat.h> // file modes
#include <mqueue.h> // Mqueue Header
#include <cyg/infra/testcase.h> // test API
/* FUNCTIONS */
static int
my_memcmp(const void *m1, const void *m2, size_t n)
{
char *s1 = (char *)m1;
char *s2 = (char *)m2;
while (n--) {
if (*s1 != *s2)
return *s1 - *s2;
s1++;
s2++;
}
return 0;
} // my_memcmp()
//************************************************************************
int
main(void)
{
mqd_t q1, q2;
char buf[20];
ssize_t recvlen;
unsigned int prio;
struct mq_attr attr, oattr;
mode_t mode;
int err;
CYG_TEST_INIT();
CYG_TEST_INFO( "Starting POSIX message test 1" );
q1 = mq_open( "/mq1", O_RDWR );
CYG_TEST_PASS_FAIL( q1 == (mqd_t)-1, "error for non-existent queue" );
CYG_TEST_PASS_FAIL( ENOENT == errno,
"errno correct for non-existent queue" );
attr.mq_flags = 0;
attr.mq_maxmsg = 4;
attr.mq_msgsize = 20;
mode = S_IRWXU|S_IRWXG|S_IRWXO; // rwx for all
q1 = mq_open( "/mq1", O_CREAT|O_NONBLOCK|O_WRONLY, mode, &attr );
CYG_TEST_PASS_FAIL( q1 != (mqd_t)-1, "simple mq_open (write only)" );
err = mq_getattr( q1, &attr );
CYG_TEST_PASS_FAIL( 0 == err, "simple mq_getattr" );
CYG_TEST_PASS_FAIL( (4 == attr.mq_maxmsg) &&
(20 == attr.mq_msgsize) &&
(O_NONBLOCK == (attr.mq_flags & O_NONBLOCK)) &&
(O_RDONLY != (attr.mq_flags & O_RDONLY)) &&
(O_WRONLY == (attr.mq_flags & O_WRONLY)) &&
(O_RDWR != (attr.mq_flags & O_RDWR)) &&
(0 == attr.mq_curmsgs ), "getattr attributes correct" );
err = mq_send( q1, "Vik is brill", sizeof("Vik is brill"), 10 );
CYG_TEST_PASS_FAIL( 0 == err, "simple mq_send" );
err = mq_getattr( q1, &attr );
CYG_TEST_PASS_FAIL( 0 == err, "simple mq_getattr after send" );
CYG_TEST_PASS_FAIL( (4 == attr.mq_maxmsg) &&
(20 == attr.mq_msgsize) &&
(O_NONBLOCK == (attr.mq_flags & O_NONBLOCK)) &&
(O_RDONLY != (attr.mq_flags & O_RDONLY)) &&
(O_WRONLY == (attr.mq_flags & O_WRONLY)) &&
(O_RDWR != (attr.mq_flags & O_RDWR)) &&
(1 == attr.mq_curmsgs ),
"getattr attributes correct #2" );
q2 = mq_open( "/mq1", O_RDONLY|O_CREAT|O_EXCL );
CYG_TEST_PASS_FAIL( q2 == (mqd_t)-1,
"error for exclusive open of existing queue" );
CYG_TEST_PASS_FAIL( EEXIST == errno,
"errno correct for exclusive open of existing queue" );
q2 = mq_open( "/mq1", O_RDONLY );
CYG_TEST_PASS_FAIL( q2 != (mqd_t)-1, "simple mq_open (read only)" );
err = mq_getattr( q2, &attr );
CYG_TEST_PASS_FAIL( 0 == err, "simple mq_getattr, different mqd_t" );
CYG_TEST_PASS_FAIL( (4 == attr.mq_maxmsg) &&
(20 == attr.mq_msgsize) &&
(O_NONBLOCK != (attr.mq_flags & O_NONBLOCK)) &&
(O_RDONLY == (attr.mq_flags & O_RDONLY)) &&
(O_WRONLY != (attr.mq_flags & O_WRONLY)) &&
(O_RDWR != (attr.mq_flags & O_RDWR)) &&
(1 == attr.mq_curmsgs ),
"getattr attributes correct #3" );
err = mq_close( q2 );
CYG_TEST_PASS_FAIL( 0 == err, "simple mq_close" );
q2 = mq_open( "/mq1", O_RDONLY );
CYG_TEST_PASS_FAIL( q2 != (mqd_t)-1, "mq_open reopen (read only)" );
err = mq_getattr( q2, &attr );
CYG_TEST_PASS_FAIL( 0 == err, "simple mq_getattr, different mqd_t" );
CYG_TEST_PASS_FAIL( (4 == attr.mq_maxmsg) &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -