1 /* 2 * Copyright (C) Bull S.A. 1996 3 * Copyright (c) International Business Machines Corp., 2001 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation; either version 2 of the License, or 8 * (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 13 * the GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 18 */ 19 /*---------------------------------------------------------------------+ 20 | message_queue_test_02_rcv | 21 | ==================================================================== | 22 | | 23 | Description: Create a message queue to send messages between two | 24 | processes. | 25 | | 26 | Algorithm: o Generate a key value given the project name and id | 27 | | 28 | o Get a unique message queue id | 29 | | 30 | o Print out message queue id | 31 | | 32 | System calls: The following system calls are made | 33 | | 34 | o ftok () - generates a standard IPC key | 35 | o msgget () - gets a message queue identifier | 36 | | 37 | Usage: message_queue_test_02_rcv [-f file] [-i id] | 38 | | 39 | To compile: cc -o message_queue_test_02_rcv \ | 40 | message_queue_test_02_rcv.c | 41 | | 42 | Last update: Ver. 1.2, 2/26/94 14:03:47 | 43 | | 44 | Change Activity | 45 | | 46 | Version Date Name Reason | 47 | 0.1 032789 CST Initial version | 48 | 1.2 111993 DJK Modify for AIX version 4.1 | 49 | 1.3 022694 DJK Moved to Prod directory | 50 | | 51 +---------------------------------------------------------------------*/ 52 53 #include <errno.h> 54 #include <fcntl.h> 55 #include <stdio.h> 56 #include <unistd.h> 57 #include <sys/ipc.h> 58 #ifdef _LINUX_ 59 #include <sys/stat.h> 60 // defines struct msgbuf 61 #define __USE_GNU 62 #endif 63 #include <sys/msg.h> 64 #include <sys/types.h> 65 #include <stdlib.h> 66 #include <string.h> 67 68 /* 69 * Defines 70 * 71 * BUF_SIZE: size of message buffer... 72 */ 73 #define BUF_SIZE 256 74 #define DEFAULT_PROJECT_NAME "/tmp/message_queue_test" 75 #define DEFAULT_PROJECT_ID 20 76 #define USAGE "\nUsage: %s [-f project_name ] [-i project_id ]\n\n" 77 78 /* 79 * Function prototypes 80 * 81 * parse_args (): Parse command line arguments 82 * sys_error (): System error message function 83 * error (): Error message function 84 */ 85 static void parse_args(int, char **); 86 static void sys_error(const char *, int); 87 static void error(const char *, int); 88 89 /* 90 * Global variables 91 * 92 * project_name: Unique path used to create key (ftok) 93 * project_id: Unique number used to create key (ftok) 94 */ 95 char *project_name = DEFAULT_PROJECT_NAME; 96 char project_id = DEFAULT_PROJECT_ID; 97 98 /*---------------------------------------------------------------------+ 99 | main | 100 | ==================================================================== | 101 | | 102 | Function: Main program (see prolog for more details) | 103 | | 104 | Returns: (0) Successful completion | 105 | (-1) Error occurred | 106 | | 107 +---------------------------------------------------------------------*/ 108 int main(int argc, char **argv) 109 { 110 key_t key; /* Unique key */ 111 int msqid; /* Message queue identifier */ 112 struct msgbuf *message; /* Message buffer */ 113 114 /* 115 * Parse command line options 116 */ 117 parse_args(argc, argv); 118 119 if ((key = ftok(project_name, project_id)) < 0) 120 sys_error("ftok failed", __LINE__); 121 122 if ((msqid = msgget(key, S_IRUSR | S_IWUSR)) < 0) 123 sys_error("msgget failed", __LINE__); 124 125 message = 126 (struct msgbuf *)calloc((size_t) (sizeof(struct msgbuf) + BUF_SIZE), 127 sizeof(char)); 128 if (msgrcv(msqid, message, BUF_SIZE, 0, 0) < 0) 129 sys_error("msgrcv failed", __LINE__); 130 131 printf("%s\n", message->mtext); 132 free(message); 133 134 return (0); 135 } 136 137 /*---------------------------------------------------------------------+ 138 | parse_args () | 139 | ==================================================================== | 140 | | 141 | Function: Parse the command line arguments & initialize global | 142 | variables. | 143 | | 144 | Updates: (command line options) | 145 | | 146 | [-f] file: project file | 147 | | 148 | [-i] num: project id | 149 | | 150 +---------------------------------------------------------------------*/ 151 static void parse_args(int argc, char **argv) 152 { 153 int opt; 154 int errflag = 0; 155 char *program_name = *argv; 156 extern char *optarg; /* Command line option */ 157 158 /* 159 * Parse command line options. 160 */ 161 while ((opt = getopt(argc, argv, "f:i:")) != EOF) { 162 switch (opt) { 163 case 'f': /* project file */ 164 project_name = optarg; 165 break; 166 case 'i': /* project id */ 167 project_id = *optarg; 168 break; 169 default: 170 errflag++; 171 break; 172 } 173 } 174 if (errflag) { 175 fprintf(stderr, USAGE, program_name); 176 exit(2); 177 } 178 } 179 180 /*---------------------------------------------------------------------+ 181 | sys_error () | 182 | ==================================================================== | 183 | | 184 | Function: Creates system error message and calls error () | 185 | | 186 +---------------------------------------------------------------------*/ 187 static void sys_error(const char *msg, int line) 188 { 189 char syserr_msg[256]; 190 191 sprintf(syserr_msg, "%s: %s\n", msg, strerror(errno)); 192 error(syserr_msg, line); 193 } 194 195 /*---------------------------------------------------------------------+ 196 | error () | 197 | ==================================================================== | 198 | | 199 | Function: Prints out message and exits... | 200 | | 201 +---------------------------------------------------------------------*/ 202 static void error(const char *msg, int line) 203 { 204 fprintf(stderr, "ERROR [line: %d] %s\n", line, msg); 205 exit(-1); 206 } 207