1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| //
// SOURCE : msg_recup.c
//
// OBJET : msg_recup
//
// BUT : Récupérer les messages d'une boite aux lettres
//
// PRINCIPE : Le premier argument est la clef de la boite (hexadecimal)
// Si argument pas clef valide, il devient identifiant (décimal)
// Le second (facultatif) est le type du message (decimal)
// Le programme boucle sur le nombre initial de messages
//
// USAGE : prog clef/id type
// - Pas d'option
//
#include <sys/types.h> // Types prédéfinis "c"
#include <sys/ipc.h> // Internal Process Comm.
#include <sys/msg.h> // Boite aux lettres I.P.C.
#include <stdio.h> // I/O fichiers classiques
#include <errno.h> // Erreurs système
extern const char* const sys_errlist[]; // Liste messages erreurs
#define SZ_MESSAGE (255) // Taille max. message
typedef struct {
int no; // No d'ordre
char txt[SZ_MESSAGE]; // Texte du message
}t_corps; // Type "corps du message"
typedef struct {
mtyp_t mtype; // Type de message
t_corps mcorps; // Corps du message
}t_msgbuf; // Type "message" au format "ipc"
// Fonction principale
main(
int argc, // Nbre arguments
char *argv[]) // Ptr arguments
{
// Déclaration des variables
unsigned short nb_msg; // Nombre de messages présents
int msg_id; // Identifiant de la boite
int status; // Status de retour fonction
mtyp_t msg_type; // Type message demandé
key_t msg_key; // Clef de la boite
t_msgbuf msg_buf; // Message à récupérer
struct msqid_ds info; // Information boite
// Vérification au moins un argument
if (argc <= 1)
{
fprintf(stderr, "Usage: %s clef/id [type]\n", argv[0]);
exit(errno);
}
// Conversion argument 1 (hexa) en nombre
msg_key=strtoul(argv[1], NULL, 16);
// Récupération argument 2 (si existant) pour type
msg_type=(argc > 2) ?atol(argv[2]) :0;
// Ouverture de la boite si clef valide
if ((msg_id=msgget(msg_key, IPC_ALLOC)) == (-1))
{
fprintf(stderr, "ligne %u - msgget(0x%08x) - %s\n", __LINE__, msg_key, sys_errlist[errno]);
msg_id=strtoul(argv[1], NULL, 10);
printf("Clef 0x%08x invalide => Utilisation identifiant %d\n", msg_key, msg_id);
}
printf("Boite aux lettres 0x%x ouverte - valeur %d\n", msg_key, msg_id);
// Récuperation info boite
if (msgctl(msg_id, IPC_STAT, &info) == (-1))
{
fprintf(stderr, "ligne %u - msgctl(%d) - %s\n", __LINE__, msg_id, sys_errlist[errno]);
exit(errno);
}
printf("Boite contient %hu messages (id=%d)\n", info.msg_qnum, msg_id);
// Boucle sur le nombre de messages
for (nb_msg=info.msg_qnum; nb_msg > 0; nb_msg--)
{
// Récupération du message
status=msgrcv(msg_id, &msg_buf, sizeof(t_corps), msg_type, IPC_NOWAIT|MSG_NOERROR);
// Si récupération impossible mais pas par manque de place
if (status == (-1))
{
// Si erreur n'est pas "plus de message"
if (errno != ENOMSG)
{
fprintf(stderr, "ligne %u - msgrcv() - %s\n", __LINE__, sys_errlist[errno]);
exit(errno);
}
// On sort de la boucle
break;
}
// Affichage chaine
printf("\tmessage(type=%d): '%d %s' - status %d\n", msg_buf.mtype, msg_buf.mcorps.no, msg_buf.mcorps.txt, status);
}
// Fin du programme
printf("Fin des messages de type %d (0x%x)\n", msg_type, msg_key);
} |
Partager