home *** CD-ROM | disk | FTP | other *** search
/ Der Mediaplex Sampler - Die 6 von Plex / 6_v_plex.zip / 6_v_plex / DISK6 / OS_15 / SH.ZIP / JOBS.C < prev    next >
C/C++ Source or Header  |  1994-01-24  |  7KB  |  353 lines

  1. /* Display running background processes
  2.  * Kai Uwe Rommel
  3.  * Sat 04-Aug-1990, updated to support OS/2 2.x Tue 12-May-1992
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9.  
  10. #include <sys/types.h>
  11. #include <setjmp.h>
  12. #include <unistd.h>
  13. #include <dir.h>
  14. #include "sh.h"
  15.  
  16. #define INCL_NOPM
  17. #define INCL_DOSPROCESS
  18. #define INCL_DOSMODULEMGR
  19. #include <os2.h>
  20.  
  21.  
  22. extern USHORT APIENTRY DosQProcStatus(PVOID pBuf, USHORT cbBuf);
  23.  
  24.  
  25. /* OS/2 1.x 16-bit version */
  26.  
  27.  
  28. struct process
  29. {
  30.   USHORT pid;
  31.   USHORT ppid;
  32.   USHORT threads;
  33.   USHORT children;
  34.   USHORT modhandle;
  35.   USHORT module;
  36. };
  37.  
  38.  
  39. struct module
  40. {
  41.   USHORT modhandle;
  42.   USHORT max_dependents;
  43.   USHORT *dependents;
  44.   UCHAR  *modname;
  45. };
  46.  
  47.  
  48. struct process **procs = NULL;
  49. struct module  **mods  = NULL;
  50.  
  51. USHORT max_procs = 0;
  52. USHORT cur_procs = 0;
  53. USHORT max_mods  = 0;
  54. USHORT cur_mods  = 0;
  55.  
  56.  
  57. int parse_processes(UCHAR * bBuf)
  58. {
  59.   USHORT sel, offs;
  60.   USHORT type, tpid;
  61.   USHORT count, kount;
  62.   UCHAR buffer[256];
  63.   UCHAR *cptr, *ptr;
  64.  
  65.   ptr = bBuf;
  66.   sel = SELECTOROF(ptr);
  67.  
  68.   while ( (type = *(USHORT *) ptr) != 0xFFFFU )
  69.   {
  70.     ptr += 2;
  71.     offs = *(USHORT *) ptr;
  72.     ptr += 2;
  73.  
  74.     switch ( type )
  75.     {
  76.  
  77.     case 0: /* process */
  78.  
  79.       if ( cur_procs >= max_procs )
  80.       {
  81.         max_procs += 50;
  82.  
  83.     if ( !(procs = realloc(procs, max_procs * sizeof(struct process *))) )
  84.           return 1;
  85.       }
  86.  
  87.       if ( !(procs[cur_procs] = calloc(1, sizeof(struct process))) )
  88.         return 1;
  89.  
  90.       procs[cur_procs] -> pid = *(USHORT *) ptr;
  91.       ptr += 2;
  92.       procs[cur_procs] -> ppid = *(USHORT *) ptr;
  93.       ptr += 2;
  94.       ptr += 2;
  95.       procs[cur_procs] -> modhandle = *(USHORT *) ptr;
  96.  
  97.       procs[cur_procs] -> threads = 0;
  98.       ++cur_procs;
  99.  
  100.       break;
  101.  
  102.     case 1: /* thread */
  103.  
  104.       ptr += 2;
  105.       tpid = *(USHORT *) ptr;
  106.  
  107.       for ( count = 0; count < cur_procs; count++ )
  108.     if ( procs[count] -> pid == tpid )
  109.     {
  110.       ++procs[count] -> threads;
  111.       break;
  112.     }
  113.  
  114.       break;
  115.  
  116.     case 2: /* module */
  117.  
  118.       if ( cur_mods >= max_mods )
  119.       {
  120.         max_mods += 50;
  121.  
  122.     if ( !(mods = realloc(mods, max_mods * sizeof(struct module *))) )
  123.           return 1;
  124.       }
  125.  
  126.       if ( !(mods[cur_mods] = calloc(1, sizeof(struct module))) )
  127.         return 1;
  128.  
  129.       mods[cur_mods] -> modhandle = *(USHORT *) ptr;
  130.       ptr += 2;
  131.       mods[cur_mods] -> max_dependents = *(USHORT *) ptr;
  132.       ptr += 2;
  133.       ptr += 2;
  134.       ptr += 2;
  135.  
  136.       if ( mods[cur_mods] -> max_dependents )
  137.       ptr += (mods[cur_mods] -> max_dependents) * 2;
  138.  
  139.       for ( cptr = buffer; *cptr++ = *ptr++; );
  140.  
  141.       if ( !(mods[cur_mods] -> modname = strdup(buffer)) )
  142.         return 1;
  143.  
  144.       ++cur_mods;
  145.  
  146.       break;
  147.  
  148.     case 3: /* system semaphore */
  149.       break;
  150.  
  151.     case 4: /* shared memory */
  152.       break;
  153.  
  154.     }
  155.  
  156.     ptr = MAKEP(sel, offs);
  157.   }
  158.  
  159.   for ( count = 0; count < cur_procs; count++ )
  160.     for ( kount = 0; kount < cur_mods; kount++ )
  161.       if ( procs[count] -> modhandle == mods[kount] -> modhandle )
  162.       {
  163.         procs[count] -> module = kount;
  164.     break;
  165.       }
  166.  
  167.   for ( count = 0; count < cur_procs; count++ )
  168.     for ( kount = 0; kount < cur_procs; kount++ )
  169.       if ( procs[count] -> pid == procs[kount] -> ppid )
  170.     (procs[count] -> children)++;
  171.  
  172.   return 0;
  173. }
  174.  
  175.  
  176. void proctree(int pid, int indent)
  177. {
  178.   USHORT count;
  179.   UCHAR *mName, pName[256];
  180.  
  181.   for (count = 0; count < cur_procs; count++)
  182.     if ( procs[count] -> ppid == pid )
  183.     {
  184.       if ( procs[count] -> module )
  185.       {
  186.         mName = mods[procs[count] -> module] -> modname;
  187.         DosGetModName(procs[count] -> modhandle, sizeof(pName), pName);
  188.       }
  189.       else
  190.       {
  191.         mName = "unknown";  /* Zombie process, i.e. result for DosCwait() */
  192.         pName[0] = 0;
  193.       }
  194.  
  195.       printf("[%d]\t%2d %-8s %*s%s\n", procs[count] -> pid,
  196.         procs[count] -> threads, mName, indent, "", pName);
  197.  
  198.       proctree(procs[count] -> pid, indent + 2);
  199.     }
  200. }
  201.  
  202.  
  203. int compare(struct process **p1, struct process **p2)
  204. {
  205.   return (*p1) -> pid - (*p2) -> pid;
  206. }
  207.  
  208.  
  209. void dojobs1(void)
  210. {
  211.   UCHAR *pBuf;
  212.   USHORT count;
  213.  
  214.   pBuf = malloc(0x2000);
  215.   DosQProcStatus(pBuf, 0x2000);
  216.  
  217.   if ( parse_processes(pBuf) )
  218.   {
  219.     printf("Error: Out of memory.\n");
  220.     DosExit(EXIT_PROCESS, 1);
  221.   }
  222.  
  223.   free(pBuf);
  224.  
  225.   qsort(procs, cur_procs, sizeof(struct process *), compare);
  226.   proctree(getpid(), 0);
  227.  
  228.   for (count = 0; count < cur_procs; count++)
  229.     free(procs[count]);
  230.  
  231.   for (count = 0; count < cur_mods; count++)
  232.   {
  233.     free(mods[count] -> modname);
  234.     free(mods[count]);
  235.   }
  236.  
  237.   free(procs);
  238.   free(mods);
  239.  
  240.   max_procs = max_mods = cur_procs = cur_mods = 0;
  241.   procs = NULL;
  242.   mods = NULL;
  243. }
  244.  
  245.  
  246. /* OS/2 2.x 32-bit version */
  247.  
  248.  
  249. #define PTR(ptr, ofs)  ((void *) ((char *) (((ULONG) procstat & 0xFFFF0000) | (USHORT) (ptr)) + (ofs)))
  250.  
  251.  
  252. struct procstat2
  253. {
  254.   ULONG  summary;
  255.   ULONG  processes;
  256.   ULONG  semaphores;
  257.   ULONG  unknown1;
  258.   ULONG  sharedmemory;
  259.   ULONG  modules;
  260.   ULONG  unknown2;
  261.   ULONG  unknown3;
  262. };
  263.  
  264.  
  265. struct process2
  266. {
  267.   ULONG  type;
  268.   ULONG  threadlist;
  269.   USHORT processid;
  270.   USHORT parentid;
  271.   ULONG  unknown1;
  272.   ULONG  unknown2;
  273.   USHORT sessionid;
  274.   USHORT unknown3;
  275.   USHORT modulehandle;
  276.   USHORT threads;
  277.   USHORT sessiontype;
  278.   /* lots of other unknown data */
  279. };
  280.  
  281.  
  282. struct thread2
  283. {
  284.   ULONG  unknown1;
  285.   USHORT threadid;
  286.   USHORT threadsysid;
  287.   ULONG  blockid;
  288.   USHORT priority;
  289.   USHORT unknown2;
  290.   ULONG  unknown3;
  291.   ULONG  unknown4;
  292.   USHORT status;
  293.   USHORT unknown5;
  294. };
  295.  
  296.  
  297. struct procstat2 *procstat;
  298.  
  299.  
  300. void proctree2(USHORT pid, USHORT indent)
  301. {
  302.   struct process2 *proc;
  303.   UCHAR name[256];
  304.   USHORT prty;
  305.  
  306.   for ( proc = PTR(procstat -> processes, 0);
  307.         proc -> type != 3; /* not sure if there isn't another termination */
  308.         proc = PTR(proc -> threadlist,                          /* method */
  309.                    proc -> threads * sizeof(struct thread2))
  310.       )
  311.     if ( proc -> parentid == pid )
  312.     {
  313.       if ( DosGetModName(proc -> modulehandle, sizeof(name), name) )
  314.         strcpy(name, "<unknown>");
  315.       if ( DosGetPrty(PRTYS_PROCESS, &prty, proc -> processid) )
  316.         prty = 0;
  317.  
  318.       printf("[%d]\t%2d %04X %*s%s\n",
  319.         proc -> processid, proc -> threads, prty, indent, "", name);
  320.  
  321.       proctree2(proc -> processid, indent + 2);
  322.     }
  323. }
  324.  
  325.  
  326. void dojobs2(void)
  327. {
  328.   procstat = malloc(0x8000);
  329.   DosQProcStatus(procstat, 0x8000);
  330.   proctree2(getpid(), 0);
  331.   free(procstat);
  332. }
  333.  
  334.  
  335. /* main code */
  336.  
  337.  
  338. void dojobs(C_Op *t)
  339. {
  340.   if ( _osmajor < 20 )
  341.     dojobs1();
  342.   else
  343.     dojobs2();
  344. }
  345.  
  346.  
  347. #ifdef TEST
  348. void main(void)
  349. {
  350.   dojobs(NULL);
  351. }
  352. #endif
  353.