home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / mint / mint095s / dos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-03  |  11.5 KB  |  556 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith. All rights reserved.
  3. */
  4.  
  5. /* miscellaneous DOS functions, and the DOS initialization function */
  6.  
  7. #include "mint.h"
  8.  
  9. #define DOS_MAX 0x140
  10.  
  11. Func dos_tab[DOS_MAX];
  12. short dos_max = DOS_MAX;
  13.  
  14. static void alarmme P_((PROC *));
  15.  
  16. long
  17. s_version()
  18. {
  19.     TRACE("Sversion");
  20.     return Sversion();
  21. }
  22.  
  23. /*
  24.  * Super(new_ssp): change to supervisor mode.
  25.  */
  26.  
  27. long
  28. s_uper(new_ssp)
  29.     long new_ssp;
  30. {
  31.     int in_super;
  32.     long r;
  33.  
  34.     TRACE("Super");
  35.     in_super = curproc->ctxt[SYSCALL].sr & 0x2000;
  36.  
  37.     if (new_ssp == 1) {
  38.         r = in_super ? -1L : 0;
  39.     }
  40.     else {
  41.         curproc->ctxt[SYSCALL].sr ^= 0x2000;
  42.         r = curproc->ctxt[SYSCALL].ssp;
  43.         if (in_super) {
  44.             if (new_ssp == 0) {
  45.                 DEBUG("bad Super call");
  46.                 raise(SIGSYS);
  47.             }
  48.             else {
  49.                 curproc->ctxt[SYSCALL].usp = 
  50.                     curproc->ctxt[SYSCALL].ssp;
  51.                 curproc->ctxt[SYSCALL].ssp = new_ssp;
  52.             }
  53.         }
  54.         else {
  55.             curproc->ctxt[SYSCALL].ssp = 
  56.                 new_ssp ? new_ssp : curproc->ctxt[SYSCALL].usp;
  57.         }
  58.     }
  59.     return r;
  60. }
  61.  
  62. /*
  63.  * get/set time and date functions
  64.  */
  65. long t_getdate() { return datestamp; }
  66. long t_gettime() { return timestamp; }
  67.  
  68. long t_setdate(date)
  69.     int date;
  70. {
  71.     long r = Tsetdate(date);
  72.     datestamp = Tgetdate();
  73.     return r;
  74. }
  75.  
  76. long t_settime(time)
  77.     int time;
  78. {
  79.     long r = Tsettime(time);
  80.     timestamp = Tgettime();
  81.     return r;
  82. }
  83.  
  84. /*
  85.  * GEMDOS extension: Syield(): give up the processor if any other
  86.  * processes are waiting. Always returns 0.
  87.  */
  88.  
  89. long
  90. s_yield()
  91. {
  92.     TRACE("S_yield");
  93.  
  94. /* reward the nice process */
  95.     curproc->curpri = curproc->pri;
  96.     sleep(READY_Q, curproc->wait_cond);
  97.     return 0;
  98. }
  99.  
  100. /*
  101.  * GEMDOS extension:
  102.  * Prenice(pid, delta) sets the process priority level for process pid.
  103.  * A "nice" value < 0 increases priority, one > 0 decreases it.
  104.  * Always returns the new priority (so Prenice(pid, 0) queries the current
  105.  * priority).
  106.  *
  107.  * NOTE: for backward compatibility, Pnice(delta) is provided and is equivalent
  108.  * to Prenice(Pgetpid(), delta)
  109.  */
  110.  
  111. long
  112. p_renice(pid, delta)
  113.     int pid, delta;
  114. {
  115.     PROC *p;
  116.  
  117.     if (pid <= 0 || !(p = pid2proc(pid))) {
  118.         return EFILNF;
  119.     }
  120.  
  121.     if (curproc->euid && curproc->euid != p->ruid
  122.         && curproc->ruid != p->ruid) {
  123.         DEBUG("Prenice: process ownership error");
  124.         return EACCDN;
  125.     }
  126.     p->pri -= delta;
  127.     if (p->pri < MIN_NICE) p->pri = MIN_NICE;
  128.     if (p->pri > MAX_NICE) p->pri = MAX_NICE;
  129.     p->curpri = p->pri;
  130.     return ((long)p->pri) & 0x0ffff;
  131. }
  132.  
  133. long
  134. p_nice(delta)
  135.     int delta;
  136. {
  137.     return p_renice(curproc->pid,delta);
  138. }
  139.  
  140. /*
  141.  * GEMDOS extensions: routines for getting/setting process i.d.'s and
  142.  * user i.d.'s
  143.  */
  144.  
  145. long p_getpid() { return curproc->pid; }
  146.  
  147. long p_getppid() { return curproc->ppid; }
  148.  
  149. long p_getpgrp() { return curproc->pgrp; }
  150.  
  151. /* note: Psetpgrp(0, ...) is equivalent to Psetpgrp(Pgetpid(), ...) */
  152. /* also note: Psetpgrp(x, 0) is equivalent to Psetpgrp(x, x) */
  153.  
  154. long p_setpgrp(pid, newgrp)
  155.     int pid, newgrp;
  156. {
  157.     PROC *p;
  158.  
  159.     if (pid == 0)
  160.         p = curproc;
  161.     else if (!(p = pid2proc(pid)))
  162.         return EFILNF;
  163.     if ( (curproc->euid) && (p->ruid != curproc->ruid)
  164.           && (p->ppid != curproc->pid) )
  165.         return EACCDN;
  166.  
  167.     if (newgrp == 0)
  168.         newgrp = p->pid;
  169.  
  170.     return (p->pgrp = newgrp);
  171. }
  172.  
  173. long p_getuid() { return curproc->ruid; }
  174. long p_getgid() { return curproc->rgid; }
  175. long p_geteuid() { return curproc->euid; }
  176. long p_getegid() { return curproc->egid; }
  177.  
  178. long
  179. p_setuid(id)
  180.     int id;
  181. {
  182.     if (curproc->euid == 0) {
  183.         curproc->ruid = curproc->euid = id;
  184.         return id;
  185.     }
  186.     return EACCDN;
  187. }
  188.  
  189. long
  190. p_setgid(id)
  191.     int id;
  192. {
  193.     if (curproc->euid == 0 || curproc->egid == 0) {
  194.         curproc->egid = curproc->rgid = id;
  195.         return id;
  196.     }
  197.     return EACCDN;
  198. }
  199.  
  200. /*
  201.  * a way to get/set process-specific user information. the user information
  202.  * longword is set to "arg", unless arg is -1. In any case, the old
  203.  * value of the longword is returned.
  204.  */
  205.  
  206. long
  207. p_usrval(arg)
  208.     long arg;
  209. {
  210.     long r;
  211.  
  212.     TRACE("Pusrval");
  213.     r = curproc->usrdata;
  214.     if (arg != -1L)
  215.         curproc->usrdata = arg;
  216.     return r;
  217. }
  218.  
  219. /*
  220.  * set the file creation mask to "mode". Returns the old value of the
  221.  * mask.
  222.  */
  223. long p_umask(mode)
  224.     unsigned mode;
  225. {
  226.     long oldmask = curproc->umask;
  227.  
  228.     curproc->umask = mode & (~S_IFMT);
  229.     return oldmask;
  230. }
  231.  
  232. /*
  233.  * get/set the domain of a process. domain 0 is the default (TOS) domain.
  234.  * domain 1 is the MiNT domain. for now, domain affects read/write system
  235.  * calls and filename translation.
  236.  */
  237.  
  238. long
  239. p_domain(arg)
  240.     int arg;
  241. {
  242.     long r;
  243.     TRACE("Pdomain(%d)", arg);
  244.  
  245.     r = curproc->domain;
  246.     if (arg >= 0)
  247.         curproc->domain = arg;
  248.     return r;
  249. }
  250.  
  251. /*
  252.  * get process resource usage. 8 longwords are returned, as follows:
  253.  *     r[0] == system time used by process
  254.  *     r[1] == user time used by process
  255.  *     r[2] == system time used by process' children
  256.  *     r[3] == user time used by process' children
  257.  *     r[4] == memory used by process
  258.  *     r[5] - r[7]: reserved for future use
  259.  */
  260.  
  261. long
  262. p_rusage(r)
  263.     long *r;
  264. {
  265.     r[0] = curproc->systime;
  266.     r[1] = curproc->usrtime;
  267.     r[2] = curproc->chldstime;
  268.     r[3] = curproc->chldutime;
  269.     r[4] = memused(curproc);
  270.     return 0;
  271. }
  272.  
  273. /*
  274.  * get/set resource limits i to value v. The old limit is always returned;
  275.  * if v == -1, the limit is unchanged, otherwise it is set to v. Possible
  276.  * values for i are:
  277.  *    1:  max. cpu time    (milliseconds)
  278.  *    2:  max. core memory allowed
  279.  *    3:  max. amount of malloc'd memory allowed
  280.  */
  281. long
  282. p_setlimit(i, v)
  283.     int i;
  284.     long v;
  285. {
  286.     long oldlimit;
  287.  
  288.     switch(i) {
  289.     case 1:
  290.         oldlimit = curproc->maxcpu;
  291.         if (v >= 0) curproc->maxcpu = v;
  292.         break;
  293.     case 2:
  294.         oldlimit = curproc->maxcore;
  295.         if (v >= 0) {
  296.             curproc->maxcore = v;
  297.             recalc_maxmem(curproc);
  298.         }
  299.         break;
  300.     case 3:
  301.         oldlimit = curproc->maxdata;
  302.         if (v >= 0) {
  303.             curproc->maxdata = v;
  304.             recalc_maxmem(curproc);
  305.         }
  306.         break;
  307.     default:
  308.         DEBUG("Psetlimit: invalid mode %d", i);
  309.         return EINVFN;
  310.     }
  311.     TRACE("p_setlimit(%d, %ld): oldlimit = %ld", i, v, oldlimit);
  312.     return oldlimit;
  313. }
  314.  
  315. /*
  316.  * pause: just sleeps on IO_Q, with wait_cond == -1. only a signal will
  317.  * wake us up
  318.  */
  319.  
  320. long
  321. p_pause()
  322. {
  323.     sleep(IO_Q, -1L);
  324.     return 0;
  325. }
  326.  
  327. /*
  328.  * helper function for t_alarm: this will be called when the timer goes
  329.  * off, and raises SIGALRM
  330.  */
  331.  
  332. static void
  333. alarmme(p)
  334.     PROC *p;
  335. {
  336.     p->alarmtim = 0;
  337.     post_sig(p, SIGALRM);
  338. }
  339.  
  340. /*
  341.  * t_alarm(x): set the alarm clock to go off in "x" seconds. returns the
  342.  * old value of the alarm clock
  343.  */
  344.  
  345. long
  346. t_alarm(x)
  347.     long x;
  348. {
  349.     long oldalarm;
  350.     TIMEOUT *t;
  351.  
  352. /* see how many milliseconds there were to the alarm timeout */
  353.     oldalarm = 0;
  354.  
  355.     if (curproc->alarmtim) {
  356.         for (t = tlist; t; t = t->next) {
  357.             oldalarm += t->when;
  358.             if (t == curproc->alarmtim)
  359.                 goto foundalarm;
  360.         }
  361.         DEBUG("Talarm: old alarm not found!");
  362.         oldalarm = 0;
  363.         curproc->alarmtim = 0;
  364. foundalarm:
  365.         ;
  366.     }
  367.  
  368.     oldalarm = (oldalarm+999) / 1000;    /* convert to seconds */
  369.  
  370. /* we were just querying the alarm */
  371.     if (x < 0)
  372.         return oldalarm;
  373.  
  374. /* cancel old alarm */
  375.     if (curproc->alarmtim)
  376.         canceltimeout(curproc->alarmtim);
  377.  
  378. /* add a new alarm, to occur in 1000*x milliseconds */
  379.     if (x)
  380.         curproc->alarmtim = addtimeout(1000*x, alarmme);
  381.     else
  382.         curproc->alarmtim = 0;
  383.  
  384.     return oldalarm;
  385. }
  386.  
  387. /*
  388.  * sysconf(which): returns information about system configuration.
  389.  * "which" specifies which aspect of the system configuration is to
  390.  * be returned:
  391.  *    -1    max. value of "which" allowed
  392.  *    0    max. number of memory regions per proc
  393.  *    1    max. length of Pexec() execution string {ARG_MAX}
  394.  *    2    max. number of open files per process    {OPEN_MAX}
  395.  *    3    number of supplementary group id's    {currently 0}
  396.  *    4    max. number of processes per uid    {CHILD_MAX}
  397.  *
  398.  * unlimited values (e.g. CHILD_MAX) are returned as 0x7fffffffL
  399.  *
  400.  * See also Dpathconf() in dosdir.c.
  401.  */
  402.  
  403. long
  404. s_ysconf(which)
  405.     int which;
  406. {
  407.     if (which == -1)
  408.         return 4;
  409.  
  410.     switch(which) {
  411.         case 0:
  412.             return UNLIMITED;
  413.         case 1:
  414.             return 126;
  415.         case 2:
  416.             return MAX_OPEN;
  417.         case 3:
  418.             return 0;
  419.         case 4:
  420.             return UNLIMITED;
  421.         default:
  422.             return EINVFN;
  423.     }
  424. }
  425.  
  426. /*
  427.  * routine for initializing DOS
  428.  *
  429.  * NOTE: before adding new functions, check the definition of
  430.  * DOS_MAX at the top of this file to make sure that there
  431.  * is room; if not, increase DOS_MAX.
  432.  */
  433.  
  434. void
  435. init_dos()
  436. {
  437. /* miscellaneous initialization goes here */
  438.     timestamp = Tgettime();
  439.     datestamp = Tgetdate();
  440.  
  441. /* dos table initialization */
  442.     dos_tab[0x00] = p_term0;
  443.     dos_tab[0x01] = c_conin;
  444.     dos_tab[0x02] = c_conout;
  445.     dos_tab[0x03] = c_auxin;
  446.     dos_tab[0x04] = c_auxout;
  447.     dos_tab[0x05] = c_prnout;
  448.     dos_tab[0x06] = c_rawio;
  449.     dos_tab[0x07] = c_rawcin;
  450.     dos_tab[0x08] = c_necin;
  451.     dos_tab[0x09] = c_conws;
  452.     dos_tab[0x0a] = c_conrs;
  453.     dos_tab[0x0b] = c_conis;
  454.     dos_tab[0x0e] = d_setdrv;
  455.     dos_tab[0x10] = c_conos;
  456.     dos_tab[0x11] = c_prnos;
  457.     dos_tab[0x12] = c_auxis;
  458.     dos_tab[0x13] = c_auxos;
  459.     dos_tab[0x14] = m_addalt;
  460.     dos_tab[0x19] = d_getdrv;
  461.     dos_tab[0x1a] = f_setdta;
  462.     dos_tab[0x20] = s_uper;
  463.     dos_tab[0x2a] = t_getdate;
  464.     dos_tab[0x2b] = t_setdate;
  465.     dos_tab[0x2c] = t_gettime;
  466.     dos_tab[0x2d] = t_settime;
  467.     dos_tab[0x2f] = f_getdta;
  468.     dos_tab[0x30] = s_version;
  469.     dos_tab[0x31] = p_termres;
  470.     dos_tab[0x36] = d_free;
  471.     dos_tab[0x39] = d_create;
  472.     dos_tab[0x3a] = d_delete;
  473.     dos_tab[0x3b] = d_setpath;
  474.     dos_tab[0x3c] = f_create;
  475.     dos_tab[0x3d] = f_open;
  476.     dos_tab[0x3e] = f_close;
  477.     dos_tab[0x3f] = f_read;
  478.     dos_tab[0x40] = f_write;
  479.     dos_tab[0x41] = f_delete;
  480.     dos_tab[0x42] = f_seek;
  481.     dos_tab[0x43] = f_attrib;
  482.     dos_tab[0x44] = m_xalloc;
  483.     dos_tab[0x45] = f_dup;
  484.     dos_tab[0x46] = f_force;
  485.     dos_tab[0x47] = d_getpath;
  486.     dos_tab[0x48] = m_alloc;
  487.     dos_tab[0x49] = m_free;
  488.     dos_tab[0x4a] = m_shrink;
  489.     dos_tab[0x4b] = p_exec;
  490.     dos_tab[0x4c] = p_term;
  491.     dos_tab[0x4e] = f_sfirst;
  492.     dos_tab[0x4f] = f_snext;
  493.     dos_tab[0x56] = f_rename;
  494.     dos_tab[0x57] = f_datime;
  495.     dos_tab[0x5c] = f_lock;
  496.  
  497. /* MiNT extensions to GEMDOS */
  498.  
  499.     dos_tab[0xff] = s_yield;
  500.     dos_tab[0x100] = f_pipe;
  501.     dos_tab[0x104] = f_cntl;
  502.     dos_tab[0x105] = file_instat;
  503.     dos_tab[0x106] = file_outstat;
  504.     dos_tab[0x107] = file_getchar;
  505.     dos_tab[0x108] = file_putchar;
  506.     dos_tab[0x109] = p_wait;
  507.     dos_tab[0x10a] = p_nice;
  508.     dos_tab[0x10b] = p_getpid;
  509.     dos_tab[0x10c] = p_getppid;
  510.     dos_tab[0x10d] = p_getpgrp;
  511.     dos_tab[0x10e] = p_setpgrp;
  512.     dos_tab[0x10f] = p_getuid;
  513.     dos_tab[0x110] = p_setuid;
  514.     dos_tab[0x111] = p_kill;
  515.     dos_tab[0x112] = p_signal;
  516.     dos_tab[0x113] = p_vfork;
  517.     dos_tab[0x114] = p_getgid;
  518.     dos_tab[0x115] = p_setgid;
  519.     dos_tab[0x116] = p_sigblock;
  520.     dos_tab[0x117] = p_sigsetmask;
  521.     dos_tab[0x118] = p_usrval;
  522.     dos_tab[0x119] = p_domain;
  523.     dos_tab[0x11a] = p_sigreturn;
  524.     dos_tab[0x11b] = p_fork;
  525.     dos_tab[0x11c] = p_wait3;
  526.     dos_tab[0x11d] = f_select;
  527.     dos_tab[0x11e] = p_rusage;
  528.     dos_tab[0x11f] = p_setlimit;
  529.     dos_tab[0x120] = t_alarm;
  530.     dos_tab[0x121] = p_pause;
  531.     dos_tab[0x122] = s_ysconf;
  532.     dos_tab[0x123] = p_sigpending;
  533.     dos_tab[0x124] = d_pathconf;
  534.     dos_tab[0x125] = p_msg;
  535.     dos_tab[0x126] = f_midipipe;
  536.     dos_tab[0x127] = p_renice;
  537.     dos_tab[0x128] = d_opendir;
  538.     dos_tab[0x129] = d_readdir;
  539.     dos_tab[0x12a] = d_rewind;
  540.     dos_tab[0x12b] = d_closedir;
  541.     dos_tab[0x12c] = f_xattr;
  542.     dos_tab[0x12d] = f_link;
  543.     dos_tab[0x12e] = f_symlink;
  544.     dos_tab[0x12f] = f_readlink;
  545.     dos_tab[0x130] = d_cntl;
  546.     dos_tab[0x131] = f_chown;
  547.     dos_tab[0x132] = f_chmod;
  548.     dos_tab[0x133] = p_umask;
  549.     dos_tab[0x134] = p_semaphore;
  550.     dos_tab[0x135] = d_lock;
  551.     dos_tab[0x136] = p_sigpause;
  552.     dos_tab[0x137] = p_sigaction;
  553.     dos_tab[0x138] = p_geteuid;
  554.     dos_tab[0x139] = p_getegid;
  555. }
  556.