moss.user
Class MPosixIf

java.lang.Object
  extended by moss.user.MPosixIf

public class MPosixIf
extends java.lang.Object

the MPosixIF interface provides the glue between applications and the kernel.


Field Summary
static int STDERR
          standard error descriptor
static int STDIN
          standard input descriptor
static int STDOUT
          standard output descriptor
 
Constructor Summary
MPosixIf()
           
 
Method Summary
static int accept(int fd, MSocketAddr addr)
          accepts an incoming connection from a listening socket
static int access(java.lang.String path, int amode)
          test to see if a file can be opened in the given mode.
static int bind(int fd, MSocketAddr addr)
          binds a socket to an address
static int close(int fd)
          This method closes the given descriptor.
static int connect(int fd, MSocketAddr addr)
          connects a socket to a remote host
static void exit(int exitcode)
          this is called by a process to terminate.
static int fcntl(int fd, int op, int arg)
          file-handle control.
static int forkexec(java.lang.String cmd, java.lang.String[] args)
          this performs a typical fork()/exec() to start a new process, but tries to find it via the file-system and MExec
static int forkexecc(java.lang.String cname, java.lang.String[] args)
          this performs a typical fork()/exec() used to start a new process
static int getpid()
          used to retrieve the current process ID
static int getppid()
          used to retrieve the parent process ID
static int kill(int pid, int sig)
          sends a signal to a process
static int listen(int fd, int backlog)
          enables a socket to listen for incoming connections
static int loadmodule(java.lang.String mod, java.lang.String[] args)
          this loads a new kernel module (something that implements MKernelProcess)
static int mkdir(java.lang.String path, int mode)
          creates a directory
static int mount(java.lang.String path, java.lang.String fstype, java.lang.String[] options)
          this is used to mount a file-system.
static int open_device(java.lang.String name, int flags)
          this opens a device by name (until we get a file-system..)
static int open(java.lang.String path, int flags)
          This method is used to open a file
static int open(java.lang.String path, int flags, int mode)
          This method is used to open a file, with a file-mode if needed
static int opendir(java.lang.String path)
          This method is used to open a directory
static int opennmq(java.lang.String name)
          opens a named message-queue.
static int pause()
          this is called to put a process to sleep (indefinitely)
static int pipe(int[] fds)
          This method creates a new pair of pipe descriptors (and the pipe).
static MProcess[] process_list()
          used to get the current process list.
static int read(int fd, byte[] buffer, int count)
          this performs a read from the given descriptor.
static int readdir(int fd, MDirEnt dirent)
          reads a directory entry from an open directory.
static int readmsg(int fd, MNamedMsgQ.MNamedMsg msgret)
          reads a message from a named message-queue
static java.lang.Object recvmsg(int pid, int type)
          this is used to receive a message from another (or any) process
static void reschedule()
          this performes a rescheduling operation.
static int semop(int op, int key, int value)
          this is used to perform a semaphore operation.
static int sendmsg(int pid, int type, java.lang.Object message)
          this is used to send a message to another process
static int setpriority(int pri)
          sets the priority of the calling process
static int signal(int sig, int action)
          sets a process's handling of a particular signal
static int sleep(long millis)
          this is used to put a process to sleep for a given amount of time
static int socket(int domain, int type)
          this creates a new socket
static int stat(java.lang.String path, MInode statbuf)
          stats a file or directory
static int umask(int mask)
          sets a process's umask.
static int umount(java.lang.String path)
          this is used to unmount a file-system.
static int unlink(java.lang.String path)
          unlinks (removes) a file or directory
static int[] wait(boolean nohang)
          this method waits for a child process to exit (nohang is false), or polls for an exited child (nohang is true).
static int write(int fd, byte[] buffer, int count)
          this performs a write to the given descriptor.
static int writeklog(java.lang.String str)
          writes a string to the kernel log
static int writemsg(int fd, int type, java.lang.Object msg)
          sends a message to a named message-queue
static int writestring(int fd, java.lang.String str)
          writes a Java "String" to the given descriptor (more useful than necessary)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

STDIN

public static final int STDIN
standard input descriptor

See Also:
Constant Field Values

STDOUT

public static final int STDOUT
standard output descriptor

See Also:
Constant Field Values

STDERR

public static final int STDERR
standard error descriptor

See Also:
Constant Field Values
Constructor Detail

MPosixIf

public MPosixIf()
Method Detail

pipe

public static int pipe(int[] fds)
This method creates a new pair of pipe descriptors (and the pipe). The current implementation of the pipe is symmetric -- a process can fill the pipe and read it back all on one descriptor (not serious, though).

Parameters:
fds - array where returned descriptors are stored. fds[0] is for reading, fds[1] for writing.
Returns:
on success, 0 is returned, otherwise < 0 indicating error.

open_device

public static int open_device(java.lang.String name,
                              int flags)
this opens a device by name (until we get a file-system..)

Parameters:
name - name of device to open
flags - open flags
Returns:
file-handle on success, otherwise -ve value indicating error

open

public static int open(java.lang.String path,
                       int flags)
This method is used to open a file

Parameters:
path - path to file
flags - open flags (MFileOps.OPEN_...)
Returns:
file-descriptor on success, otherwise < 0 indicating error

open

public static int open(java.lang.String path,
                       int flags,
                       int mode)
This method is used to open a file, with a file-mode if needed

Parameters:
path - path to file
flags - open flags (MFileOps.OPEN_...)
mode - mode if creating file
Returns:
file-descriptor on success, otherwise < 0 indicating error

opendir

public static int opendir(java.lang.String path)
This method is used to open a directory

Parameters:
path - path to directory
Returns:
file-descriptor on success, otherwise < 0 indicating error

close

public static int close(int fd)
This method closes the given descriptor.

Parameters:
fd - file-descriptor
Returns:
0 on success, otherwise < 0 error-code

read

public static int read(int fd,
                       byte[] buffer,
                       int count)
this performs a read from the given descriptor.

Parameters:
fd - file-descriptor to read from
buffer - buffer where data will be stored
count - maximum number of bytes to read
Returns:
number of bytes read on success, 0 on end-of-file, or -ve on error

readdir

public static int readdir(int fd,
                          MDirEnt dirent)
reads a directory entry from an open directory.

Parameters:
fd - file-descriptor referring to an open directory
dirent - structure where the directory information will be placed
Returns:
0 on success, otherwise < 0 indicating error.

write

public static int write(int fd,
                        byte[] buffer,
                        int count)
this performs a write to the given descriptor.

Parameters:
fd - file-descriptor to write to
buffer - data to write
count - maximum number of bytes to write
Returns:
number of bytes written on success, or -ve on error

writestring

public static int writestring(int fd,
                              java.lang.String str)
writes a Java "String" to the given descriptor (more useful than necessary)

Parameters:
fd - file-descriptor
str - string to write
Returns:
number of bytes written on success, of -ve on error

fcntl

public static int fcntl(int fd,
                        int op,
                        int arg)
file-handle control. Used to get/set certain options on file-descriptors.

Parameters:
fd - file-descriptor of some fcntl capable stream
op - operation (F_... in MFileOps)
arg - operation-specific argument
Returns:
>= 0 on success, or < 0 indicating error

unlink

public static int unlink(java.lang.String path)
unlinks (removes) a file or directory

Parameters:
path - path to the file/directory to remove
Returns:
0 on success, or < 0 indicating error

opennmq

public static int opennmq(java.lang.String name)
opens a named message-queue. this returns a file descriptor, although the application/kernel must use it with readmsg() and writemsg()


writemsg

public static int writemsg(int fd,
                           int type,
                           java.lang.Object msg)
sends a message to a named message-queue

Parameters:
fd - file-handle of the named message-queue
msg - object message
type - application-defined type
Returns:
0 on success, or < 0 indicating error

readmsg

public static int readmsg(int fd,
                          MNamedMsgQ.MNamedMsg msgret)
reads a message from a named message-queue

Parameters:
fd - file-handle of the named message-queue
msgret - message return structure
Returns:
0 on success, or < 0 indicating error

socket

public static int socket(int domain,
                         int type)
this creates a new socket

Parameters:
domain - domain of this socket
type - type of this socket
Returns:
file-descriptor on success or < 0 indicating error

bind

public static int bind(int fd,
                       MSocketAddr addr)
binds a socket to an address

Parameters:
fd - file-descriptor for a socket
addr - address to bind
Returns:
0 on success, or < 0 indicating error

listen

public static int listen(int fd,
                         int backlog)
enables a socket to listen for incoming connections

Parameters:
fd - file-descriptor for a bound socket
backlog - connection backlog
Returns:
0 on success, or < 0 indicating error

accept

public static int accept(int fd,
                         MSocketAddr addr)
accepts an incoming connection from a listening socket

Parameters:
fd - file-descriptor for a listening socket
addr - address in which the connecting client address is stored
Returns:
a file-descriptor for the connected client on success, or < 0 indicating error

connect

public static int connect(int fd,
                          MSocketAddr addr)
connects a socket to a remote host

Parameters:
fd - file-descriptor for a socket
addr - address of remote host (and port)
Returns:
0 on success, or < 0 indicating error

reschedule

public static void reschedule()
this performes a rescheduling operation. The current process is added to the back of the run-queue and a reschedule occurs


forkexecc

public static int forkexecc(java.lang.String cname,
                            java.lang.String[] args)
this performs a typical fork()/exec() used to start a new process

Parameters:
cname - Java class name to load (should implement MUserProcess)
args - arguments to pass to the started process
Returns:
process-ID on success, or < 0 indicating error

forkexec

public static int forkexec(java.lang.String cmd,
                           java.lang.String[] args)
this performs a typical fork()/exec() to start a new process, but tries to find it via the file-system and MExec

Parameters:
cmd - program name to run
args - arguments to pass to the started process
Returns:
process-ID on success, or < 0 indicating error

writeklog

public static int writeklog(java.lang.String str)
writes a string to the kernel log

Parameters:
str - string to write
Returns:
0 on success, or < 0 indicating error

loadmodule

public static int loadmodule(java.lang.String mod,
                             java.lang.String[] args)
this loads a new kernel module (something that implements MKernelProcess)

Parameters:
mod - path to module to run
args - arguments to pass to the module
Returns:
process-ID on success, or < 0 indicating error

exit

public static void exit(int exitcode)
this is called by a process to terminate. This never returns.

Parameters:
exitcode - return code for parent process

pause

public static int pause()
this is called to put a process to sleep (indefinitely)

Returns:
error-code indicating why the process woke up (EINTR)

getpid

public static int getpid()
used to retrieve the current process ID

Returns:
process ID

getppid

public static int getppid()
used to retrieve the parent process ID

Returns:
process ID or -1 if no parent (init-task or kernel-processes only)

signal

public static int signal(int sig,
                         int action)
sets a process's handling of a particular signal

Parameters:
sig - signal constant (MSignal.SIG...)
action - signal action constant (MSignal.SIG_...)
Returns:
previous signal action for this signal, or < 0 indicating error

kill

public static int kill(int pid,
                       int sig)
sends a signal to a process

Parameters:
pid - ID of process to signal
sig - signal number (MSignal.SIG...)
Returns:
zero on success, otherwise -ve indicating error

wait

public static int[] wait(boolean nohang)
this method waits for a child process to exit (nohang is false), or polls for an exited child (nohang is true). This is done by waiting for SIGCHLD signals to be delivered, rather than anything more elaborate. Any other signals will be delivered before returning.

Parameters:
nohang - if true, this method will not sleep if no child processes have exited
Returns:
an array of integers. These come in pairs, i.e. [pid0, exitcode0, pid1, exitcode1, ...]. If no child processes have exited, or the process interrupted, null is returned

process_list

public static MProcess[] process_list()
used to get the current process list. The application must format the resulting data.

Returns:
array of process information blocks

sleep

public static int sleep(long millis)
this is used to put a process to sleep for a given amount of time

Parameters:
millis - timeout in milli-seconds
Returns:
0 on success, or < 0 indicating error

sendmsg

public static int sendmsg(int pid,
                          int type,
                          java.lang.Object message)
this is used to send a message to another process

Parameters:
pid - target process PID
type - message type (application specific)
message - object message (application specific)
Returns:
0 on success, or < 0 indicating error

recvmsg

public static java.lang.Object recvmsg(int pid,
                                       int type)
this is used to receive a message from another (or any) process

Parameters:
pid - specific PID to receive from (or -1 for any)
type - specific message type to receive (application specific, or -1 for any)
Returns:
the object message, or null on error (bad pid/type or interrupted by a signal)

semop

public static int semop(int op,
                        int key,
                        int value)
this is used to perform a semaphore operation.

Parameters:
op - semaphore operation (SEMOP_CREATE, SEMOP_SET)
key - semaphore key (identifier)
value - initial value when creating, or adjustment when setting
Returns:
0 on success, or < 0 indicating error

mount

public static int mount(java.lang.String path,
                        java.lang.String fstype,
                        java.lang.String[] options)
this is used to mount a file-system.

Parameters:
path - absolute path to mount point
fstype - file-system type to mount
options - file-system specific options (may include device)
Returns:
0 on success, otherwise < 0 indicating error

umount

public static int umount(java.lang.String path)
this is used to unmount a file-system.

Parameters:
path - absolute path of the file-system to unmount (mount-point)
Returns:
0 on success, otherwise < 0 indicating error

mkdir

public static int mkdir(java.lang.String path,
                        int mode)
creates a directory

Parameters:
path - absolute path to the directory to be created
mode - permissions to be used when creating the directory
Returns:
0 on success, otherwise < 0 indicating error

stat

public static int stat(java.lang.String path,
                       MInode statbuf)
stats a file or directory

Parameters:
path - absolute path to the file/directory to be `stat'd
statbuf - MInode where the information will be placed
Returns:
0 on success, otherwise < 0 indicating error

umask

public static int umask(int mask)
sets a process's umask. This is the file-creation mask, e.g. if umask=022 and a mode is specified as 666, the actual mode will be: (666 | ~022) = 644 (rw-r--r--)

Parameters:
mask - the new umask
Returns:
the previous umask

access

public static int access(java.lang.String path,
                         int amode)
test to see if a file can be opened in the given mode. Modes are F_OK, R_OK, W_OK and X_OK.

Parameters:
path - path to file/directory to check
amode - access modes to check for
Returns:
0 on success (all modes granted), or < 0 indicating error

setpriority

public static int setpriority(int pri)
sets the priority of the calling process

Parameters:
pri - new process priority
Returns:
0 on success, or < 0 indicating error