root/ipc/msg.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. msg_init
  2. real_msgsnd
  3. real_msgrcv
  4. sys_msgsnd
  5. sys_msgrcv
  6. findkey
  7. newque
  8. sys_msgget
  9. freeque
  10. sys_msgctl
  11. kerneld_exit
  12. kerneld_send

   1 /*
   2  * linux/ipc/msg.c
   3  * Copyright (C) 1992 Krishna Balasubramanian 
   4  *
   5  * Kerneld extensions by Bjorn Ekwall <bj0rn@blox.se> in May 1995
   6  *
   7  */
   8 
   9 #include <linux/config.h>
  10 #include <linux/errno.h>
  11 #include <linux/sched.h>
  12 #include <linux/msg.h>
  13 #include <linux/stat.h>
  14 #include <linux/malloc.h>
  15 #include <linux/kerneld.h>
  16 
  17 #include <asm/segment.h>
  18 
  19 extern int ipcperms (struct ipc_perm *ipcp, short msgflg);
  20 
  21 static void freeque (int id);
  22 static int newque (key_t key, int msgflg);
  23 static int findkey (key_t key);
  24 
  25 static struct msqid_ds *msgque[MSGMNI];
  26 static int msgbytes = 0;
  27 static int msghdrs = 0;
  28 static unsigned short msg_seq = 0;
  29 static int used_queues = 0;
  30 static int max_msqid = 0;
  31 static struct wait_queue *msg_lock = NULL;
  32 static int kerneld_msqid = -1;
  33 static int kerneld_pid;
  34 
  35 void msg_init (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  36 {
  37         int id;
  38         
  39         for (id = 0; id < MSGMNI; id++) 
  40                 msgque[id] = (struct msqid_ds *) IPC_UNUSED;
  41         msgbytes = msghdrs = msg_seq = max_msqid = used_queues = 0;
  42         msg_lock = NULL;
  43         return;
  44 }
  45 
  46 static int real_msgsnd (int msqid, struct msgbuf *msgp, size_t msgsz, int msgflg)
     /* [previous][next][first][last][top][bottom][index][help] */
  47 {
  48         int id, err;
  49         struct msqid_ds *msq;
  50         struct ipc_perm *ipcp;
  51         struct msg *msgh;
  52         long mtype;
  53         
  54         if (msgsz > MSGMAX || (long) msgsz < 0 || msqid < 0)
  55                 return -EINVAL;
  56         if (!msgp) 
  57                 return -EFAULT;
  58         /*
  59          * Calls from kernel level (IPC_KERNELD set)
  60          * have the message somewhere in kernel space already!
  61          */
  62         if ((msgflg & IPC_KERNELD))
  63                 mtype = msgp->mtype;
  64         else {
  65                 err = verify_area (VERIFY_READ, msgp->mtext, msgsz);
  66                 if (err) 
  67                         return err;
  68                 if ((mtype = get_user (&msgp->mtype)) < 1)
  69                         return -EINVAL;
  70         }
  71         id = (unsigned int) msqid % MSGMNI;
  72         msq = msgque [id];
  73         if (msq == IPC_UNUSED || msq == IPC_NOID)
  74                 return -EINVAL;
  75         ipcp = &msq->msg_perm; 
  76 
  77  slept:
  78         if (msq->msg_perm.seq != (unsigned int) msqid / MSGMNI) 
  79                 return -EIDRM;
  80         /*
  81          * Non-root processes may send to kerneld! 
  82          * i.e. no permission check if called from the kernel
  83          * otoh we don't want user level non-root snoopers...
  84          */
  85         if ((msgflg & IPC_KERNELD) == 0)
  86                 if (ipcperms(ipcp, S_IWUGO)) 
  87                         return -EACCES;
  88         
  89         if (msgsz + msq->msg_cbytes > msq->msg_qbytes) { 
  90                 /* no space in queue */
  91                 if (msgflg & IPC_NOWAIT)
  92                         return -EAGAIN;
  93                 if (current->signal & ~current->blocked)
  94                         return -EINTR;
  95                 if (intr_count) {
  96                         /* Very unlikely, but better safe than sorry... */
  97                         printk("Ouch, kerneld:msgsnd wants to sleep at interrupt!\n");
  98                         return -EINTR;
  99                 }
 100                 interruptible_sleep_on (&msq->wwait);
 101                 goto slept;
 102         }
 103         
 104         /* allocate message header and text space*/ 
 105         msgh = (struct msg *) kmalloc (sizeof(*msgh) + msgsz,
 106                 (intr_count ? GFP_ATOMIC : GFP_USER));
 107         if (!msgh)
 108                 return -ENOMEM;
 109         msgh->msg_spot = (char *) (msgh + 1);
 110 
 111         /*
 112          * Calls from kernel level (IPC_KERNELD set)
 113          * have the message somewhere in kernel space already!
 114          */
 115         if (msgflg & IPC_KERNELD) {
 116                 struct kerneld_msg *kdmp = (struct kerneld_msg *)msgp;
 117 
 118                 /*
 119                  * Note that the kernel supplies a pointer
 120                  * but the user-level kerneld uses a char array...
 121                  */
 122                 memcpy(msgh->msg_spot, (char *)(&(kdmp->id)), sizeof(long)); 
 123                 memcpy(msgh->msg_spot + sizeof(long), kdmp->text,
 124                         msgsz - sizeof(long)); 
 125         }
 126         else
 127                 memcpy_fromfs (msgh->msg_spot, msgp->mtext, msgsz); 
 128         
 129         if (msgque[id] == IPC_UNUSED || msgque[id] == IPC_NOID
 130                 || msq->msg_perm.seq != (unsigned int) msqid / MSGMNI) {
 131                 kfree(msgh);
 132                 return -EIDRM;
 133         }
 134 
 135         msgh->msg_next = NULL;
 136         if (!msq->msg_first)
 137                 msq->msg_first = msq->msg_last = msgh;
 138         else {
 139                 msq->msg_last->msg_next = msgh;
 140                 msq->msg_last = msgh;
 141         }
 142         msgh->msg_ts = msgsz;
 143         msgh->msg_type = mtype;
 144         msq->msg_cbytes += msgsz;
 145         msgbytes  += msgsz;
 146         msghdrs++;
 147         msq->msg_qnum++;
 148         msq->msg_lspid = current->pid;
 149         msq->msg_stime = CURRENT_TIME;
 150         if (msq->rwait)
 151                 wake_up (&msq->rwait);
 152         return 0;
 153 }
 154 
 155 static int real_msgrcv (int msqid, struct msgbuf *msgp, size_t msgsz, long msgtyp, int msgflg)
     /* [previous][next][first][last][top][bottom][index][help] */
 156 {
 157         struct msqid_ds *msq;
 158         struct ipc_perm *ipcp;
 159         struct msg *tmsg, *leastp = NULL;
 160         struct msg *nmsg = NULL;
 161         int id, err;
 162 
 163         if (msqid < 0 || (long) msgsz < 0)
 164                 return -EINVAL;
 165         if (!msgp || !msgp->mtext)
 166             return -EFAULT;
 167         /*
 168          * Calls from kernel level (IPC_KERNELD set)
 169          * wants the message put in kernel space!
 170          */
 171         if ((msgflg & IPC_KERNELD) == 0) {
 172                 err = verify_area (VERIFY_WRITE, msgp->mtext, msgsz);
 173                 if (err)
 174                         return err;
 175         }
 176 
 177         id = (unsigned int) msqid % MSGMNI;
 178         msq = msgque [id];
 179         if (msq == IPC_NOID || msq == IPC_UNUSED)
 180                 return -EINVAL;
 181         ipcp = &msq->msg_perm; 
 182 
 183         /* 
 184          *  find message of correct type.
 185          *  msgtyp = 0 => get first.
 186          *  msgtyp > 0 => get first message of matching type.
 187          *  msgtyp < 0 => get message with least type must be < abs(msgtype).  
 188          */
 189         while (!nmsg) {
 190                 if (msq->msg_perm.seq != (unsigned int) msqid / MSGMNI)
 191                         return -EIDRM;
 192                 if ((msgflg & IPC_KERNELD) == 0) {
 193                         /*
 194                          * Non-root processes may receive from kerneld! 
 195                          * i.e. no permission check if called from the kernel
 196                          * otoh we don't want user level non-root snoopers...
 197                          */
 198                         if (ipcperms (ipcp, S_IRUGO))
 199                                 return -EACCES;
 200                 }
 201                 if (msgtyp == 0) 
 202                         nmsg = msq->msg_first;
 203                 else if (msgtyp > 0) {
 204                         if (msgflg & MSG_EXCEPT) { 
 205                                 for (tmsg = msq->msg_first; tmsg; 
 206                                      tmsg = tmsg->msg_next)
 207                                         if (tmsg->msg_type != msgtyp)
 208                                                 break;
 209                                 nmsg = tmsg;
 210                         } else {
 211                                 for (tmsg = msq->msg_first; tmsg; 
 212                                      tmsg = tmsg->msg_next)
 213                                         if (tmsg->msg_type == msgtyp)
 214                                                 break;
 215                                 nmsg = tmsg;
 216                         }
 217                 } else {
 218                         for (leastp = tmsg = msq->msg_first; tmsg; 
 219                              tmsg = tmsg->msg_next) 
 220                                 if (tmsg->msg_type < leastp->msg_type) 
 221                                         leastp = tmsg;
 222                         if (leastp && leastp->msg_type <= - msgtyp)
 223                                 nmsg = leastp;
 224                 }
 225                 
 226                 if (nmsg) { /* done finding a message */
 227                         if ((msgsz < nmsg->msg_ts) && !(msgflg & MSG_NOERROR))
 228                                 return -E2BIG;
 229                         msgsz = (msgsz > nmsg->msg_ts)? nmsg->msg_ts : msgsz;
 230                         if (nmsg ==  msq->msg_first)
 231                                 msq->msg_first = nmsg->msg_next;
 232                         else {
 233                                 for (tmsg = msq->msg_first; tmsg; 
 234                                      tmsg = tmsg->msg_next)
 235                                         if (tmsg->msg_next == nmsg) 
 236                                                 break;
 237                                 tmsg->msg_next = nmsg->msg_next;
 238                                 if (nmsg == msq->msg_last)
 239                                         msq->msg_last = tmsg;
 240                         }
 241                         if (!(--msq->msg_qnum))
 242                                 msq->msg_last = msq->msg_first = NULL;
 243                         
 244                         msq->msg_rtime = CURRENT_TIME;
 245                         msq->msg_lrpid = current->pid;
 246                         msgbytes -= nmsg->msg_ts; 
 247                         msghdrs--; 
 248                         msq->msg_cbytes -= nmsg->msg_ts;
 249                         if (msq->wwait)
 250                                 wake_up (&msq->wwait);
 251                         /*
 252                          * Calls from kernel level (IPC_KERNELD set)
 253                          * wants the message copied to kernel space!
 254                          */
 255                         if (msgflg & IPC_KERNELD) {
 256                                 struct kerneld_msg *kdmp = (struct kerneld_msg *) msgp;
 257 
 258                                 memcpy((char *)(&(kdmp->id)),
 259                                         nmsg->msg_spot,
 260                                         sizeof(long)); 
 261                                 /*
 262                                  * Note that kdmp->text is a pointer
 263                                  * when called from kernel space!
 264                                  */
 265                                 if ((msgsz > sizeof(long)) && kdmp->text)
 266                                         memcpy(kdmp->text,
 267                                                 nmsg->msg_spot + sizeof(long),
 268                                                 msgsz - sizeof(long)); 
 269                         }
 270                         else {
 271                                 put_user (nmsg->msg_type, &msgp->mtype);
 272                                 memcpy_tofs (msgp->mtext, nmsg->msg_spot, msgsz);
 273                         }
 274                         kfree(nmsg);
 275                         return msgsz;
 276                 } else {  /* did not find a message */
 277                         if (msgflg & IPC_NOWAIT)
 278                                 return -ENOMSG;
 279                         if (current->signal & ~current->blocked)
 280                                 return -EINTR; 
 281                         if (intr_count) {
 282                                 /* Won't happen... */
 283                                 printk("Ouch, kerneld:msgrcv wants to sleep at interrupt!\n");
 284                                 return -EINTR;
 285                         }
 286                         interruptible_sleep_on (&msq->rwait);
 287                 }
 288         } /* end while */
 289         return -1;
 290 }
 291 
 292 asmlinkage int sys_msgsnd (int msqid, struct msgbuf *msgp, size_t msgsz, int msgflg)
     /* [previous][next][first][last][top][bottom][index][help] */
 293 {
 294         /* IPC_KERNELD is used as a marker for kernel calls */
 295         return real_msgsnd(msqid, msgp, msgsz, msgflg & ~IPC_KERNELD);
 296 }
 297 
 298 asmlinkage int sys_msgrcv (int msqid, struct msgbuf *msgp, size_t msgsz,
     /* [previous][next][first][last][top][bottom][index][help] */
 299         long msgtyp, int msgflg)
 300 {
 301         /* IPC_KERNELD is used as a marker for kernel calls */
 302         return real_msgrcv (msqid, msgp, msgsz, msgtyp, msgflg & ~IPC_KERNELD);
 303 }
 304 
 305 static int findkey (key_t key)
     /* [previous][next][first][last][top][bottom][index][help] */
 306 {
 307         int id;
 308         struct msqid_ds *msq;
 309         
 310         for (id = 0; id <= max_msqid; id++) {
 311                 while ((msq = msgque[id]) == IPC_NOID) 
 312                         interruptible_sleep_on (&msg_lock);
 313                 if (msq == IPC_UNUSED)
 314                         continue;
 315                 if (key == msq->msg_perm.key)
 316                         return id;
 317         }
 318         return -1;
 319 }
 320 
 321 static int newque (key_t key, int msgflg)
     /* [previous][next][first][last][top][bottom][index][help] */
 322 {
 323         int id;
 324         struct msqid_ds *msq;
 325         struct ipc_perm *ipcp;
 326 
 327         for (id = 0; id < MSGMNI; id++) 
 328                 if (msgque[id] == IPC_UNUSED) {
 329                         msgque[id] = (struct msqid_ds *) IPC_NOID;
 330                         goto found;
 331                 }
 332         return -ENOSPC;
 333 
 334 found:
 335         msq = (struct msqid_ds *) kmalloc (sizeof (*msq), GFP_KERNEL);
 336         if (!msq) {
 337                 msgque[id] = (struct msqid_ds *) IPC_UNUSED;
 338                 if (msg_lock)
 339                         wake_up (&msg_lock);
 340                 return -ENOMEM;
 341         }
 342         ipcp = &msq->msg_perm;
 343         ipcp->mode = (msgflg & S_IRWXUGO);
 344         ipcp->key = key;
 345         ipcp->cuid = ipcp->uid = current->euid;
 346         ipcp->gid = ipcp->cgid = current->egid;
 347         msq->msg_perm.seq = msg_seq;
 348         msq->msg_first = msq->msg_last = NULL;
 349         msq->rwait = msq->wwait = NULL;
 350         msq->msg_cbytes = msq->msg_qnum = 0;
 351         msq->msg_lspid = msq->msg_lrpid = 0;
 352         msq->msg_stime = msq->msg_rtime = 0;
 353         msq->msg_qbytes = MSGMNB;
 354         msq->msg_ctime = CURRENT_TIME;
 355         if (id > max_msqid)
 356                 max_msqid = id;
 357         msgque[id] = msq;
 358         used_queues++;
 359         if (msg_lock)
 360                 wake_up (&msg_lock);
 361         return (unsigned int) msq->msg_perm.seq * MSGMNI + id;
 362 }
 363 
 364 asmlinkage int sys_msgget (key_t key, int msgflg)
     /* [previous][next][first][last][top][bottom][index][help] */
 365 {
 366         int id;
 367         struct msqid_ds *msq;
 368         
 369         /*
 370          * If the IPC_KERNELD flag is set, the key is forced to IPC_PRIVATE,
 371          * and a designated kerneld message queue is created/refered to
 372          */
 373         if ((msgflg & IPC_KERNELD)) {
 374                 if (!suser())
 375                         return -EPERM;
 376                 if ((kerneld_msqid == -1) && (kerneld_msqid =
 377                                 newque(IPC_PRIVATE, msgflg & S_IRWXU)) >= 0)
 378                         kerneld_pid = current->pid;
 379                 return kerneld_msqid;
 380         }
 381         /* else it is a "normal" request */
 382         if (key == IPC_PRIVATE) 
 383                 return newque(key, msgflg);
 384         if ((id = findkey (key)) == -1) { /* key not used */
 385                 if (!(msgflg & IPC_CREAT))
 386                         return -ENOENT;
 387                 return newque(key, msgflg);
 388         }
 389         if (msgflg & IPC_CREAT && msgflg & IPC_EXCL)
 390                 return -EEXIST;
 391         msq = msgque[id];
 392         if (msq == IPC_UNUSED || msq == IPC_NOID)
 393                 return -EIDRM;
 394         if (ipcperms(&msq->msg_perm, msgflg))
 395                 return -EACCES;
 396         return (unsigned int) msq->msg_perm.seq * MSGMNI + id;
 397 } 
 398 
 399 static void freeque (int id)
     /* [previous][next][first][last][top][bottom][index][help] */
 400 {
 401         struct msqid_ds *msq = msgque[id];
 402         struct msg *msgp, *msgh;
 403 
 404         msq->msg_perm.seq++;
 405         msg_seq = (msg_seq+1) % ((unsigned)(1<<31)/MSGMNI); /* increment, but avoid overflow */
 406         msgbytes -= msq->msg_cbytes;
 407         if (id == max_msqid)
 408                 while (max_msqid && (msgque[--max_msqid] == IPC_UNUSED));
 409         msgque[id] = (struct msqid_ds *) IPC_UNUSED;
 410         used_queues--;
 411         while (msq->rwait || msq->wwait) {
 412                 if (msq->rwait)
 413                         wake_up (&msq->rwait); 
 414                 if (msq->wwait)
 415                         wake_up (&msq->wwait);
 416                 schedule(); 
 417         }
 418         for (msgp = msq->msg_first; msgp; msgp = msgh ) {
 419                 msgh = msgp->msg_next;
 420                 msghdrs--;
 421                 kfree(msgp);
 422         }
 423         kfree(msq);
 424 }
 425 
 426 asmlinkage int sys_msgctl (int msqid, int cmd, struct msqid_ds *buf)
     /* [previous][next][first][last][top][bottom][index][help] */
 427 {
 428         int id, err;
 429         struct msqid_ds *msq;
 430         struct msqid_ds tbuf;
 431         struct ipc_perm *ipcp;
 432         
 433         if (msqid < 0 || cmd < 0)
 434                 return -EINVAL;
 435         switch (cmd) {
 436         case IPC_INFO: 
 437         case MSG_INFO: 
 438                 if (!buf)
 439                         return -EFAULT;
 440         { 
 441                 struct msginfo msginfo;
 442                 msginfo.msgmni = MSGMNI;
 443                 msginfo.msgmax = MSGMAX;
 444                 msginfo.msgmnb = MSGMNB;
 445                 msginfo.msgmap = MSGMAP;
 446                 msginfo.msgpool = MSGPOOL;
 447                 msginfo.msgtql = MSGTQL;
 448                 msginfo.msgssz = MSGSSZ;
 449                 msginfo.msgseg = MSGSEG;
 450                 if (cmd == MSG_INFO) {
 451                         msginfo.msgpool = used_queues;
 452                         msginfo.msgmap = msghdrs;
 453                         msginfo.msgtql = msgbytes;
 454                 }
 455                 err = verify_area (VERIFY_WRITE, buf, sizeof (struct msginfo));
 456                 if (err)
 457                         return err;
 458                 memcpy_tofs (buf, &msginfo, sizeof(struct msginfo));
 459                 return max_msqid;
 460         }
 461         case MSG_STAT:
 462                 if (!buf)
 463                         return -EFAULT;
 464                 err = verify_area (VERIFY_WRITE, buf, sizeof (*buf));
 465                 if (err)
 466                         return err;
 467                 if (msqid > max_msqid)
 468                         return -EINVAL;
 469                 msq = msgque[msqid];
 470                 if (msq == IPC_UNUSED || msq == IPC_NOID)
 471                         return -EINVAL;
 472                 if (ipcperms (&msq->msg_perm, S_IRUGO))
 473                         return -EACCES;
 474                 id = (unsigned int) msq->msg_perm.seq * MSGMNI + msqid;
 475                 tbuf.msg_perm   = msq->msg_perm;
 476                 tbuf.msg_stime  = msq->msg_stime;
 477                 tbuf.msg_rtime  = msq->msg_rtime;
 478                 tbuf.msg_ctime  = msq->msg_ctime;
 479                 tbuf.msg_cbytes = msq->msg_cbytes;
 480                 tbuf.msg_qnum   = msq->msg_qnum;
 481                 tbuf.msg_qbytes = msq->msg_qbytes;
 482                 tbuf.msg_lspid  = msq->msg_lspid;
 483                 tbuf.msg_lrpid  = msq->msg_lrpid;
 484                 memcpy_tofs (buf, &tbuf, sizeof(*buf));
 485                 return id;
 486         case IPC_SET:
 487                 if (!buf)
 488                         return -EFAULT;
 489                 err = verify_area (VERIFY_READ, buf, sizeof (*buf));
 490                 if (err)
 491                         return err;
 492                 memcpy_fromfs (&tbuf, buf, sizeof (*buf));
 493                 break;
 494         case IPC_STAT:
 495                 if (!buf)
 496                         return -EFAULT;
 497                 err = verify_area (VERIFY_WRITE, buf, sizeof(*buf));
 498                 if (err)
 499                         return err;
 500                 break;
 501         }
 502 
 503         id = (unsigned int) msqid % MSGMNI;
 504         msq = msgque [id];
 505         if (msq == IPC_UNUSED || msq == IPC_NOID)
 506                 return -EINVAL;
 507         if (msq->msg_perm.seq != (unsigned int) msqid / MSGMNI)
 508                 return -EIDRM;
 509         ipcp = &msq->msg_perm;
 510 
 511         switch (cmd) {
 512         case IPC_STAT:
 513                 if (ipcperms (ipcp, S_IRUGO))
 514                         return -EACCES;
 515                 tbuf.msg_perm   = msq->msg_perm;
 516                 tbuf.msg_stime  = msq->msg_stime;
 517                 tbuf.msg_rtime  = msq->msg_rtime;
 518                 tbuf.msg_ctime  = msq->msg_ctime;
 519                 tbuf.msg_cbytes = msq->msg_cbytes;
 520                 tbuf.msg_qnum   = msq->msg_qnum;
 521                 tbuf.msg_qbytes = msq->msg_qbytes;
 522                 tbuf.msg_lspid  = msq->msg_lspid;
 523                 tbuf.msg_lrpid  = msq->msg_lrpid;
 524                 memcpy_tofs (buf, &tbuf, sizeof (*buf));
 525                 return 0;
 526         case IPC_SET:
 527                 if (!suser() && current->euid != ipcp->cuid && 
 528                     current->euid != ipcp->uid)
 529                         return -EPERM;
 530                 if (tbuf.msg_qbytes > MSGMNB && !suser())
 531                         return -EPERM;
 532                 msq->msg_qbytes = tbuf.msg_qbytes;
 533                 ipcp->uid = tbuf.msg_perm.uid;
 534                 ipcp->gid =  tbuf.msg_perm.gid;
 535                 ipcp->mode = (ipcp->mode & ~S_IRWXUGO) | 
 536                         (S_IRWXUGO & tbuf.msg_perm.mode);
 537                 msq->msg_ctime = CURRENT_TIME;
 538                 return 0;
 539         case IPC_RMID:
 540                 if (!suser() && current->euid != ipcp->cuid && 
 541                     current->euid != ipcp->uid)
 542                         return -EPERM;
 543                 /*
 544                  * There is only one kerneld message queue,
 545                  * mark it as non-existant
 546                  */
 547                 if ((kerneld_msqid >= 0) && (msqid == kerneld_msqid))
 548                         kerneld_pid = kerneld_msqid = -1;
 549                 freeque (id); 
 550                 return 0;
 551         default:
 552                 return -EINVAL;
 553         }
 554 }
 555 
 556 /*
 557  * We do perhaps need a "flush" for waiting processes,
 558  * so that if they are terminated, a call from do_exit
 559  * will minimize the possibility of orphaned received
 560  * messages in the queue.  For now we just make sure
 561  * that the queue is shut down whenever kerneld dies.
 562  */
 563 void kerneld_exit(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 564 {
 565         if ((current->pid == kerneld_pid) && (kerneld_msqid != -1))
 566                 sys_msgctl(kerneld_msqid, IPC_RMID, NULL);
 567 }
 568 
 569 /*
 570  * Kerneld internal message format/syntax:
 571  *
 572  * The message type from the kernel to kerneld is used to specify _what_
 573  * function we want kerneld to perform. 
 574  *
 575  * The "normal" message area is divided into a long, followed by a char array.
 576  * The long is used to hold the sequence number of the request, which will
 577  * be used as the return message type from kerneld back to the kernel.
 578  * In the return message, the long will be used to store the exit status
 579  * of the kerneld "job", or task.
 580  * The character array is used to pass parameters to kerneld and (optional)
 581  * return information from kerneld back to the kernel.
 582  * It is the responsibility of kerneld and the kernel level caller
 583  * to set usable sizes on the parameter/return value array, since
 584  * that information is _not_ included in the message format
 585  */
 586 
 587 /*
 588  * The basic kernel level entry point to kerneld.
 589  *      msgtype should correspond to a task type for (a) kerneld
 590  *      ret_size is the size of the (optional) return _value,
 591  *              OR-ed with KERNELD_WAIT if we want an answer
 592  *      msgsize is the size (in bytes) of the message, not including
 593  *              the long that is always sent first in a kerneld message
 594  *      text is the parameter for the kerneld specific task
 595  *      ret_val is NULL or the kernel address where an expected answer
 596  *              from kerneld should be placed.
 597  *
 598  * See <linux/kerneld.h> for usage (inline convenience functions)
 599  *
 600  */
 601 int kerneld_send(int msgtype, int ret_size, int msgsz,
     /* [previous][next][first][last][top][bottom][index][help] */
 602                 const char *text, const char *ret_val)
 603 {
 604         int status = -ENOSYS;
 605 #ifdef CONFIG_KERNELD
 606         static int id = KERNELD_MINSEQ;
 607         struct kerneld_msg kmsp = { msgtype, 0, (char *)text };
 608         int msgflg = S_IRUSR | S_IWUSR | IPC_KERNELD | MSG_NOERROR;
 609 
 610         if (kerneld_msqid == -1)
 611                 return -ENODEV;
 612 
 613         /* Do not wait for an answer at interrupt-time! */
 614         if (intr_count)
 615                 ret_size &= ~KERNELD_WAIT;
 616 
 617         msgsz += sizeof(long);
 618         if (ret_size & KERNELD_WAIT) {
 619                 if (++id <= 0)
 620                         id = KERNELD_MINSEQ;
 621                 kmsp.id = id;
 622         }
 623 
 624         status = real_msgsnd(kerneld_msqid, (struct msgbuf *)&kmsp, msgsz, msgflg);
 625         if ((status >= 0) && (ret_size & KERNELD_WAIT)) {
 626                 ret_size &= ~KERNELD_WAIT;
 627                 kmsp.text = (char *)ret_val;
 628                 status = real_msgrcv(kerneld_msqid, (struct msgbuf *)&kmsp,
 629                                 sizeof(long) + ((ret_val)?ret_size:0),
 630                                 kmsp.id, msgflg);
 631                 if (status > 0) /* a valid answer contains at least a long */
 632                         status = kmsp.id;
 633         }
 634 
 635 #endif /* CONFIG_KERNELD */
 636         return status;
 637 }

/* [previous][next][first][last][top][bottom][index][help] */