home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1999 May / pcp151c.iso / misc / src / install / modutils / kerneld / kdsend.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-06  |  2.4 KB  |  109 lines

  1. #define CONFIG_KERNELD
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/ipc.h>
  7. #include <sys/msg.h>
  8. #include <linux/kerneld.h>
  9.  
  10. #define MSIZE 1024
  11.  
  12. #ifndef KDHDR
  13. # ifdef NEW_KERNELD_PROTOCOL
  14. #  define KDHDR (sizeof(long) + sizeof(short) + sizeof(short))
  15. # else
  16. #  define KDHDR sizeof(long)
  17. # endif
  18. #endif
  19.  
  20. /*
  21.  * Copyright (C) 1995, Bjorn Ekwall <bj0rn@blox.se>
  22.  *
  23.  * See the file "COPYING" for your rights.
  24.  *
  25.  * Fake a call to kerneld.
  26.  * This is just used to debug kerneld...
  27.  *
  28.  * All calls are with id=0, i.e. no response from kerneld, unless
  29.  * the option "-r" is used, which will use reply type = current pid + 1024.
  30.  * Note: this message type will _not_ be intercepted by the kernel
  31.  * or kerneld, since the kernel listens for messages >= 0x7fff0000
  32.  * and kerneld listens for messages <= 255 !
  33.  *
  34.  * Usage: kdsend type message
  35.  * where type should be the numeric value of the request
  36.  * Example: "kdsend 2 ppp" will load the ppp stack
  37.  *          "kdsend 4 ppp" will remove the ppp stack (if possible) after 60 sec
  38.  *        "kdsend -r 1 ls" will execute ls and return the result.
  39.  *
  40.  * See <linux/kerneld.h> for possible types...
  41.  */
  42.  
  43. #define REPLY_ID 1024
  44.  
  45. int
  46. main(argc, argv)
  47. int argc;
  48. char **argv;
  49. {
  50.     struct kerneld_msg *msgp;
  51.     int msgtyp;
  52.     int qid;
  53.     int status;
  54.     int reply = 0;
  55.  
  56.     if ((argc > 1) && (strcmp(argv[1], "-r") == 0)) {
  57.         reply = 1;
  58.         ++argv;
  59.         --argc;
  60.     }
  61.     if (argc < 2) {
  62.         fprintf(stderr, "use: kdsend [-r] type message\n");
  63.         exit(1);
  64.     }
  65.  
  66.     qid = msgget(IPC_PRIVATE, 0600 | IPC_KERNELD);
  67.     if (qid < 0) {
  68.         perror("kdsend");
  69.         exit(1);
  70.     }
  71.  
  72.     msgp = (struct kerneld_msg *)malloc(sizeof(struct kerneld_msg) + MSIZE);
  73. #ifdef NEW_KERNELD_PROTOCOL
  74.     msgp->version = 2;
  75.     msgp->pid = getpid();
  76. #endif
  77.  
  78.     if ((msgtyp = atol(argv[1])) == 0)
  79.         msgtyp = 1; /* ksystem */
  80.     msgp->mtype = msgtyp;
  81.     if (reply)
  82.         reply = msgp->id = REPLY_ID + getpid();
  83.     else
  84.         msgp->id = 0;
  85.     if (argc == 3)
  86.         strcpy(msgp->text, argv[2]);
  87.     else
  88.         msgp->text[0] = '\0';
  89.  
  90.     status = msgsnd(qid, (struct msgbuf *)msgp,
  91.         KDHDR + strlen(msgp->text), 0);
  92.     if (status < 0)
  93.         perror("kdsend");
  94.  
  95.     if (reply) {
  96.         status = msgrcv(qid, (struct msgbuf *)msgp,
  97.             KDHDR + MSIZE, reply, MSG_NOERROR);
  98.         if (status > 0) {
  99.             msgp->text[status - KDHDR] = '\0';
  100.             printf("recieved %d bytes: '%s'\n", status, msgp->text);
  101.         }
  102.         else if (status < 0)
  103.             perror("kdsend");
  104.         else
  105.             printf("nothing recieved\n");
  106.     }
  107.     return status;
  108. }
  109.