home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / kill.zip / kill.c < prev    next >
C/C++ Source or Header  |  1996-06-26  |  6KB  |  174 lines

  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #define INCL_DOS
  6. #define INCL_ERRORS
  7. #define INCL_DOSPROCESS
  8. #define INCL_DOSEXCEPTIONS
  9. #include <os2.h>
  10.  
  11. #define SIGKILL     9
  12.  
  13.  
  14. int main(int argc, char **argv)
  15. {
  16.   int x, retval = 0, signal = SIGTERM;
  17.   ULONG action, plen;
  18.   USHORT param;
  19.   HFILE hdevice;
  20.   APIRET rc;
  21.   PID pid;
  22.  
  23.   if(argc == 1)
  24.   {
  25.     printf("Syntax:  kill [-l] [-signal] PID's\n\n"
  26.       "         where PID's are in C format (octal/decimal/hexadecimal).\n\n"
  27.       "         Use \"kill -l\" to list available signals.\n");
  28.     return 0;
  29.   }
  30.  
  31.   if((rc = DosOpen((PSZ) "/dev/fastio$", (PHFILE) &hdevice, (PULONG) &action,
  32.     0L, FILE_SYSTEM, FILE_OPEN, OPEN_SHARE_DENYNONE | OPEN_FLAGS_NOINHERIT |
  33.     OPEN_ACCESS_READONLY, (ULONG) 0L)))
  34.     hdevice = NULL;
  35.  
  36.   for(x = 1; x < argc; x++)
  37.     switch(*argv[x])
  38.     {
  39.       case '-':
  40.         if(!strcmp(argv[x], "-l"))
  41.         {
  42.           printf("Available signals:\n"
  43.             "         4 (TERM)  - Software termination (default).\n"
  44.             "         6 (INT)   - Interrupt (Ctrl-C).\n");
  45.           if(hdevice)
  46.             printf("         9 (KILL)  - Kill (via xf86sup.sys, "
  47.               "/dev/fastio$).\n");
  48.           printf("        10 (BREAK) - Break (Ctrl-Break).\n");
  49.           break;
  50.         }
  51.         else if((!strcmp(argv[x], "-9")) || (!strcmp(argv[x], "-KILL")))
  52.         {
  53.           if(hdevice)
  54.             signal = SIGKILL;
  55.           else
  56.           {
  57.             printf("Error:  SIGKILL not available, please install "
  58.               "xf86sup.sys.\n");
  59.             retval = (int) rc;
  60.           }
  61.           break;
  62.         }
  63.         else if((!strcmp(argv[x], "-4")) || (!strcmp(argv[x], "-TERM")))
  64.         {
  65.           signal = SIGTERM;
  66.           break;
  67.         }
  68.         else if((!strcmp(argv[x], "-6")) || (!strcmp(argv[x], "-INT")))
  69.         {
  70.           signal = SIGINT;
  71.           break;
  72.         }
  73.         else if((!strcmp(argv[x], "-10")) || (!strcmp(argv[x], "-BREAK")))
  74.         {
  75.           signal = SIGBREAK;
  76.           break;
  77.         }
  78.  
  79.       default:
  80.         printf("Error:  Unrecognized argument \"%s\".\n", argv[x]);
  81.         rc = -1;
  82.         x = argc;
  83.         break;
  84.  
  85.       case '0':
  86.       case '1':
  87.       case '2':
  88.       case '3':
  89.       case '4':
  90.       case '5':
  91.       case '6':
  92.       case '7':
  93.       case '8':
  94.       case '9':
  95.         pid = (PID) strtol(argv[x], NULL, 0);
  96.         if((!pid) || (pid > 32767))
  97.           printf("Error:  Invalid process ID %s.\n", argv[x]);
  98.         else
  99.         {
  100.           switch(signal)
  101.           {
  102.             case SIGKILL:
  103.               param = (USHORT) pid;
  104.               if((retval = (int) (rc = DosDevIOCtl(hdevice, 0x76L, 0x65L,
  105.                 (PULONG*) ¶m, sizeof(USHORT), &plen, NULL, 0, NULL))))
  106.               {
  107.                 if(retval == 87)
  108.                   printf("Error:  Invalid process ID %s.\n", argv[x]);
  109.                 else
  110.                   printf("Error:  fastio$ driver error on process %s, "
  111.                     "code %d.\n", argv[1], retval);
  112.               }
  113.               else
  114.                 printf("Process %s killed successfully.\n", argv[x]);
  115.               break;
  116.             case SIGTERM:
  117.               if((retval = (int) (rc = DosKillProcess(DKP_PROCESS, pid))))
  118.               {
  119.                 if(retval == 303)
  120.                   printf("Error:  Invalid process ID %s.\n", argv[x]);
  121.                 else if(retval == 162)
  122.                   printf("Error:  Signal already pending on process %s "
  123.                     "(signal handler hung?).\n", argv[x]);
  124.                 else
  125.                   printf("Error:  DosKillProcess() failed on process %s, "
  126.                     "code %d.\n", argv[x], retval);
  127.               }
  128.               else
  129.                 printf("Process %s terminated successfully.\n", argv[x]);
  130.               break;
  131.             case SIGINT:
  132.               if((retval = (int) (rc = DosSendSignalException(pid,
  133.                 XCPT_SIGNAL_INTR))))
  134.               {
  135.                 if(retval == 303)
  136.                   printf("Error:  Invalid process ID %s.\n", argv[x]);
  137.                 else if(retval == 162)
  138.                   printf("Error:  Signal already pending on process %s "
  139.                     "(signal handler hung?).\n", argv[x]);
  140.                 else
  141.                   printf("Error:  DosSendSignalException() failed on "
  142.                     "process %s, code %d.\n", argv[x], retval);
  143.               }
  144.               else
  145.                 printf("Process %s interrupted successfully.\n", argv[x]);
  146.               break;
  147.             case SIGBREAK:
  148.               if((retval = (int) (rc = DosSendSignalException(pid,
  149.                 XCPT_SIGNAL_BREAK))))
  150.               {
  151.                 if(retval == 303)
  152.                   printf("Error:  Invalid process ID %s.\n", argv[x]);
  153.                 else if(retval == 162)
  154.                   printf("Error:  Signal already pending on process %s "
  155.                     "(signal handler hung?).\n", argv[x]);
  156.                 else
  157.                   printf("Error:  DosSendSignalException() failed on "
  158.                     "process %s, code %d.\n", argv[x], retval);
  159.               }
  160.               else
  161.                 printf("Process %s broken successfully.\n", argv[x]);
  162.               break;
  163.           }
  164.         }
  165.         break;
  166.     }
  167.  
  168.   if(hdevice)
  169.     DosClose(hdevice);
  170.  
  171.   return retval;
  172. }
  173.  
  174.