Style
[mono.git] / mcs / class / Mono.Posix / Mono.Unix.Native / Syscall.cs
index 4785de33aab220e2bcf0607f5f57cf11450adbc6..a95c5ee670991e0508aefb0f7fda765e694d9e2f 100644 (file)
@@ -646,13 +646,16 @@ namespace Mono.Unix.Native {
                ST_RDONLY      =    1,  // Mount read-only
                ST_NOSUID      =    2,  // Ignore suid and sgid bits
                ST_NODEV       =    4,  // Disallow access to device special files
+               ST_NOEXEC      =    8,  // Disallow program execution
                ST_SYNCHRONOUS =   16,  // Writes are synced at once
+               ST_REMOUNT     =   32,  // Alter flags of a mounted FS
                ST_MANDLOCK    =   64,  // Allow mandatory locks on an FS
                ST_WRITE       =  128,  // Write on file/directory/symlink
                ST_APPEND      =  256,  // Append-only file
                ST_IMMUTABLE   =  512,  // Immutable file
                ST_NOATIME     = 1024,  // Do not update access times
                ST_NODIRATIME  = 2048,  // Do not update directory access times
+               ST_BIND        = 4096,  // Bind directory at different place
        }
 
        [Map][Flags]
@@ -714,6 +717,7 @@ namespace Mono.Unix.Native {
 
        #region Structures
 
+       [Map ("struct flock")]
        public struct Flock
 #if NET_2_0
                : IEquatable <Flock>
@@ -736,7 +740,7 @@ namespace Mono.Unix.Native {
 
                public override bool Equals (object obj)
                {
-                       if (obj.GetType() != GetType())
+                       if ((obj == null) || (obj.GetType () != GetType ()))
                                return false;
                        Flock value = (Flock) obj;
                        return l_type == value.l_type && l_whence == value.l_whence && 
@@ -815,9 +819,10 @@ namespace Mono.Unix.Native {
                [ino_t]     public  ulong   st_ino;     // inode
                [CLSCompliant (false)]
                public  FilePermissions     st_mode;    // protection
-               [CLSCompliant (false)]
                [NonSerialized]
+#pragma warning disable 169            
                private uint                _padding_;  // padding for structure alignment
+#pragma warning restore 169            
                [CLSCompliant (false)]
                [nlink_t]   public  ulong   st_nlink;   // number of hard links
                [CLSCompliant (false)]
@@ -1022,7 +1027,9 @@ namespace Mono.Unix.Native {
 #endif
        {
                public  int tz_minuteswest; // minutes W of Greenwich
+#pragma warning disable 169            
                private int tz_dsttime;     // type of dst correction (OBSOLETE)
+#pragma warning restore 169            
 
                public override int GetHashCode ()
                {
@@ -1053,6 +1060,7 @@ namespace Mono.Unix.Native {
                }
        }
 
+       [Map ("struct utimbuf")]
        public struct Utimbuf
 #if NET_2_0
                : IEquatable <Utimbuf>
@@ -1090,6 +1098,88 @@ namespace Mono.Unix.Native {
                }
        }
 
+       [Map ("struct timespec")]
+       public struct Timespec
+#if NET_2_0
+               : IEquatable <Timespec>
+#endif
+       {
+               [time_t] public long    tv_sec;   // Seconds.
+               public          long    tv_nsec;  // Nanoseconds.
+
+               public override int GetHashCode ()
+               {
+                       return tv_sec.GetHashCode () ^ tv_nsec.GetHashCode ();
+               }
+
+               public override bool Equals (object obj)
+               {
+                       if (obj == null || obj.GetType () != GetType ())
+                               return false;
+                       Timespec value = (Timespec) obj;
+                       return value.tv_sec == tv_sec && value.tv_nsec == tv_nsec;
+               }
+
+               public bool Equals (Timespec value)
+               {
+                       return value.tv_sec == tv_sec && value.tv_nsec == tv_nsec;
+               }
+
+               public static bool operator== (Timespec lhs, Timespec rhs)
+               {
+                       return lhs.Equals (rhs);
+               }
+
+               public static bool operator!= (Timespec lhs, Timespec rhs)
+               {
+                       return !lhs.Equals (rhs);
+               }
+       }
+
+       [Flags][Map]
+       public enum EpollFlags {
+               EPOLL_CLOEXEC = 02000000,
+               EPOLL_NONBLOCK = 04000,
+       }
+
+       [Flags][Map]
+       [CLSCompliant (false)]
+       public enum EpollEvents : uint {
+               EPOLLIN = 0x001,
+               EPOLLPRI = 0x002,
+               EPOLLOUT = 0x004,
+               EPOLLRDNORM = 0x040,
+               EPOLLRDBAND = 0x080,
+               EPOLLWRNORM = 0x100,
+               EPOLLWRBAND = 0x200,
+               EPOLLMSG = 0x400,
+               EPOLLERR = 0x008,
+               EPOLLHUP = 0x010,
+               EPOLLRDHUP = 0x2000,
+               EPOLLONESHOT = 1 << 30,
+               EPOLLET = unchecked ((uint) (1 << 31))
+       }
+
+       public enum EpollOp {
+               EPOLL_CTL_ADD = 1,
+               EPOLL_CTL_DEL = 2,
+               EPOLL_CTL_MOD = 3,
+       }
+
+       [StructLayout (LayoutKind.Explicit, Size=12, Pack=1)]
+       [CLSCompliant (false)]
+       public struct EpollEvent {
+               [FieldOffset (0)]
+               public EpollEvents events;
+               [FieldOffset (4)]
+               public int fd;
+               [FieldOffset (4)]
+               public IntPtr ptr;
+               [FieldOffset (4)]
+               public uint u32;
+               [FieldOffset (4)]
+               public ulong u64;
+       }
        #endregion
 
        #region Classes
@@ -1342,6 +1432,58 @@ namespace Mono.Unix.Native {
                }
        }
 
+       public sealed class Utsname
+#if NET_2_0
+               : IEquatable <Utsname>
+#endif
+       {
+               public string sysname;
+               public string nodename;
+               public string release;
+               public string version;
+               public string machine;
+               public string domainname;
+
+               public override int GetHashCode ()
+               {
+                       return sysname.GetHashCode () ^ nodename.GetHashCode () ^ 
+                               release.GetHashCode () ^ version.GetHashCode () ^
+                               machine.GetHashCode () ^ domainname.GetHashCode ();
+               }
+
+               public override bool Equals (object obj)
+               {
+                       if (obj == null || GetType() != obj.GetType())
+                               return false;
+                       Utsname u = (Utsname) obj;
+                       return Equals (u);
+               }
+
+               public bool Equals (Utsname value)
+               {
+                       return value.sysname == sysname && value.nodename == nodename && 
+                               value.release == release && value.version == version && 
+                               value.machine == machine && value.domainname == domainname;
+               }
+
+               // Generate string in /etc/passwd format
+               public override string ToString ()
+               {
+                       return string.Format ("{0} {1} {2} {3} {4}",
+                               sysname, nodename, release, version, machine);
+               }
+
+               public static bool operator== (Utsname lhs, Utsname rhs)
+               {
+                       return Object.Equals (lhs, rhs);
+               }
+
+               public static bool operator!= (Utsname lhs, Utsname rhs)
+               {
+                       return !Object.Equals (lhs, rhs);
+               }
+       }
+
        //
        // Convention: Functions *not* part of the standard C library AND part of
        // a POSIX and/or Unix standard (X/Open, SUS, XPG, etc.) go here.
@@ -2525,6 +2667,48 @@ namespace Mono.Unix.Native {
 
                #endregion
 
+               #region <sys/epoll.h> Declarations
+
+               public static int epoll_create (int size)
+               {
+                       return sys_epoll_create (size);
+               }
+
+               public static int epoll_create (EpollFlags flags)
+               {
+                       return sys_epoll_create1 (flags);
+               }
+
+               public static int epoll_ctl (int epfd, EpollOp op, int fd, EpollEvents events)
+               {
+                       EpollEvent ee = new EpollEvent ();
+                       ee.events = events;
+                       ee.fd = fd;
+
+                       return sys_epoll_ctl (epfd, op, fd, ref ee);
+               }
+
+               public static int epoll_wait (int epfd, EpollEvent [] events, int max_events, int timeout)
+               {
+                       if (events.Length < max_events)
+                               throw new ArgumentOutOfRangeException ("events", "Must refer to at least 'max_events' elements.");
+
+                       return sys_epoll_wait (epfd, events, max_events, timeout);
+               }
+
+               [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_create")]
+               private static extern int sys_epoll_create (int size);
+
+               [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_create1")]
+               private static extern int sys_epoll_create1 (EpollFlags flags);
+
+               [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_ctl")]
+               private static extern int sys_epoll_ctl (int epfd, EpollOp op, int fd, ref EpollEvent ee);
+
+               [DllImport (LIBC, SetLastError=true, EntryPoint="epoll_wait")]
+               private static extern int sys_epoll_wait (int epfd, EpollEvent [] ee, int maxevents, int timeout);
+               #endregion
+               
                #region <sys/mman.h> Declarations
                //
                // <sys/mman.h>
@@ -2875,7 +3059,49 @@ namespace Mono.Unix.Native {
                // <sys/utsname.h>
                //
 
-               // TODO: uname(2)
+               [Map]
+               private struct _Utsname
+               {
+                       public IntPtr sysname;
+                       public IntPtr nodename;
+                       public IntPtr release;
+                       public IntPtr version;
+                       public IntPtr machine;
+                       public IntPtr domainname;
+                       public IntPtr _buf_;
+               }
+
+               private static void CopyUtsname (ref Utsname to, ref _Utsname from)
+               {
+                       try {
+                               to = new Utsname ();
+                               to.sysname     = UnixMarshal.PtrToString (from.sysname);
+                               to.nodename    = UnixMarshal.PtrToString (from.nodename);
+                               to.release     = UnixMarshal.PtrToString (from.release);
+                               to.version     = UnixMarshal.PtrToString (from.version);
+                               to.machine     = UnixMarshal.PtrToString (from.machine);
+                               to.domainname  = UnixMarshal.PtrToString (from.domainname);
+                       }
+                       finally {
+                               Stdlib.free (from._buf_);
+                               from._buf_ = IntPtr.Zero;
+                       }
+               }
+
+               [DllImport (MPH, SetLastError=true, 
+                               EntryPoint="Mono_Posix_Syscall_uname")]
+               private static extern int sys_uname (out _Utsname buf);
+
+               public static int uname (out Utsname buf)
+               {
+                       _Utsname _buf;
+                       int r = sys_uname (out _buf);
+                       buf = new Utsname ();
+                       if (r == 0) {
+                               CopyUtsname (ref buf, ref _buf);
+                       }
+                       return r;
+               }
 
                #region <sys/wait.h> Declarations
                //
@@ -3039,6 +3265,12 @@ namespace Mono.Unix.Native {
                // <time.h>
                //
 
+               // nanosleep(2)
+               //    int nanosleep(const struct timespec *req, struct timespec *rem);
+               [DllImport (MPH, SetLastError=true,
+                               EntryPoint="Mono_Posix_Syscall_nanosleep")]
+               public static extern int nanosleep (ref Timespec req, ref Timespec rem);
+
                // stime(2)
                //    int stime(time_t *t);
                [DllImport (MPH, SetLastError=true,