home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mint104s.zoo / mint.src / dos.c < prev    next >
C/C++ Source or Header  |  1993-03-08  |  13KB  |  583 lines

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