} // class SocketStream // Create a new socket. public static int __syscall_socket(int domain, int type) { AddressFamily family; SocketType socketType; ProtocolType protocol; // Convert the parametrs into their C# equivalents. if (domain == 2 /*AF_INET*/) { family = AddressFamily.InterNetwork; } else if (domain == 10 /*AF_INET6*/) { family = AddressFamily.InterNetworkV6; } else { return(-22); /* EINVAL */ } if (type == 1 /*SOCK_STREAM*/) { socketType = SocketType.Stream; protocol = ProtocolType.Tcp; } else if (type == 2 /*SOCK_DGRAM*/) { socketType = SocketType.Dgram; protocol = ProtocolType.Udp; } else { return(-22); /* EINVAL */ } // Create the socket. Socket socket; try { socket = new Socket(family, socketType, protocol); } catch (SocketException) { return(-22); /* EINVAL */ } // Wrap the socket within a stream. Stream stream = new SocketStream(socket); // Associate the stream with a file descriptor. int fd = FileTable.AllocFD(); if (fd == -1) { stream.Close(); return(fd); } FileTable.SetFileDescriptor(fd, stream); return(fd); }
// Open a file descriptor, based on a file. public static int __syscall_open(IntPtr path, int mode, int access) { int fd; Stream stream = null; // Reserve a slot in the file descriptor table. fd = FileTable.AllocFD(); if (fd == -1) { return(-24); /* EMFILE */ } // Open the file stream. try { String name = Marshal.PtrToStringAnsi(path); stream = new FileStream (name, (FileMode)mode, (FileAccess)access); } catch (SecurityException) { return(-13); /* EACCES */ } catch (FileNotFoundException) { return(-2); /* ENOENT */ } catch (DirectoryNotFoundException) { return(-20); /* ENOTDIR */ } catch (PathTooLongException) { return(-36); /* ENAMETOOLONG */ } catch (UnauthorizedAccessException) { return(-13); /* EACCES */ } catch (IOException) { // Could be anything, but EEXIST is most likely. return(-17); /* EEXIST */ } finally { if (stream == null) { FileTable.ReleaseFD(fd); } } // Assign the stream to the "fd" slot in the file // descriptor table. FileTable.SetFileDescriptor(fd, stream); return(fd); }
// Wrap a socket that was just accepted with a file descriptor. public static int __syscall_wrap_accept(Socket socket) { // Wrap the socket within a stream. SocketStream stream = new SocketStream(socket); stream.Connected = true; // Associate the stream with a file descriptor. int fd = FileTable.AllocFD(); if (fd == -1) { stream.Close(); return(fd); } FileTable.SetFileDescriptor(fd, stream); return(fd); }
// Perform system library startup tasks. This is normally // called just before invoking the program's "main" function. public static void Startup() { Module mainModule; Assembly assembly; Type type; FieldInfo field; // Bail out if we've already done the startup code. lock (typeof(Crt0)) { if (startupDone) { return; } startupDone = true; } // Find the module that contains the "main" function. mainModule = null; try { assembly = Assembly.GetCallingAssembly(); type = assembly.GetType("<Module>"); if (type != null) { mainModule = type.Module; } } catch (NotImplementedException) { // The runtime engine probably does not have support // for reflection, so there's nothing we can do. } // Find standard C library's global module. libcModule = null; try { assembly = Assembly.Load("libc"); type = assembly.GetType("libc"); if (type != null) { libcModule = type.Module; } } catch (OutOfMemoryException) { // Send out of memory conditions back up the stack. throw; } catch (Exception) { // We weren't able to load "libc" for some reason. } // Set the global "__environ" variable within "libc". if (libcModule != null) { field = libcModule.GetField("__environ"); if (field != null) { field.SetValue(null, (Object)environ); } } // Initialize the stdin, stdout, and stderr file descriptors. #if CONFIG_SMALL_CONSOLE FileTable.SetFileDescriptor(0, Stream.Null); FileTable.SetFileDescriptor(1, new ConsoleStream()); FileTable.SetFileDescriptor(2, new ConsoleStream()); #else FileTable.SetFileDescriptor (0, new FDStream(0, Console.OpenStandardInput())); FileTable.SetFileDescriptor (1, new FDStream(1, Console.OpenStandardOutput())); FileTable.SetFileDescriptor (2, new FDStream(2, Console.OpenStandardError())); #endif // Invoke the application's ".init" function, if present. if (mainModule != null) { MethodInfo initMethod = mainModule.GetMethod(".init"); if (initMethod != null) { initMethod.Invoke(null, null); } } // Locate the application's ".fini" function. if (mainModule != null) { finiMethod = mainModule.GetMethod(".fini"); } else { finiMethod = null; } }