1 /*
   2  * This file and its contents are supplied under the terms of the
   3  * Common Development and Distribution License ("CDDL"), version 1.0.
   4  * You may only use this file in accordance with the terms of version
   5  * 1.0 of the CDDL.
   6  *
   7  * A full copy of the text of the CDDL should have accompanied this
   8  * source.  A copy of the CDDL is also available via the Internet at
   9  * http://www.illumos.org/license/CDDL.
  10  */
  11 
  12 /*
  13  * Copyright 2015 Joyent, Inc.
  14  */
  15 
  16 /*
  17  * This file contains a mapping table and lookup function for converting
  18  * illumos native error numbers into error numbers appropriate for Linux
  19  * emulation.
  20  *
  21  * The translation table is generated by the "gen_errno", built from and
  22  * documented in "usr/src/common/brand/lx/tools".
  23  */
  24 
  25 #include <sys/debug.h>
  26 
  27 const int
  28 lx_stol_errno[] = {
  29         0,      /*   0: No Error                                        */
  30         1,      /*   1: EPERM           -->   1: EPERM                       */
  31         2,      /*   2: ENOENT          -->   2: ENOENT                      */
  32         3,      /*   3: ESRCH           -->   3: ESRCH                       */
  33         4,      /*   4: EINTR           -->   4: EINTR                       */
  34         5,      /*   5: EIO             -->   5: EIO                 */
  35         6,      /*   6: ENXIO           -->   6: ENXIO                       */
  36         7,      /*   7: E2BIG           -->   7: E2BIG                       */
  37         8,      /*   8: ENOEXEC         -->   8: ENOEXEC             */
  38         9,      /*   9: EBADF           -->   9: EBADF                       */
  39         10,     /*  10: ECHILD          -->  10: ECHILD                      */
  40         11,     /*  11: EAGAIN          -->  11: EAGAIN                      */
  41         12,     /*  12: ENOMEM          -->  12: ENOMEM                      */
  42         13,     /*  13: EACCES          -->  13: EACCES                      */
  43         14,     /*  14: EFAULT          -->  14: EFAULT                      */
  44         15,     /*  15: ENOTBLK         -->  15: ENOTBLK             */
  45         16,     /*  16: EBUSY           -->  16: EBUSY                       */
  46         17,     /*  17: EEXIST          -->  17: EEXIST                      */
  47         18,     /*  18: EXDEV           -->  18: EXDEV                       */
  48         19,     /*  19: ENODEV          -->  19: ENODEV                      */
  49         20,     /*  20: ENOTDIR         -->  20: ENOTDIR             */
  50         21,     /*  21: EISDIR          -->  21: EISDIR                      */
  51         22,     /*  22: EINVAL          -->  22: EINVAL                      */
  52         23,     /*  23: ENFILE          -->  23: ENFILE                      */
  53         24,     /*  24: EMFILE          -->  24: EMFILE                      */
  54         25,     /*  25: ENOTTY          -->  25: ENOTTY                      */
  55         26,     /*  26: ETXTBSY         -->  26: ETXTBSY             */
  56         27,     /*  27: EFBIG           -->  27: EFBIG                       */
  57         28,     /*  28: ENOSPC          -->  28: ENOSPC                      */
  58         29,     /*  29: ESPIPE          -->  29: ESPIPE                      */
  59         30,     /*  30: EROFS           -->  30: EROFS                       */
  60         31,     /*  31: EMLINK          -->  31: EMLINK                      */
  61         32,     /*  32: EPIPE           -->  32: EPIPE                       */
  62         33,     /*  33: EDOM            -->  33: EDOM                        */
  63         34,     /*  34: ERANGE          -->  34: ERANGE                      */
  64         42,     /*  35: ENOMSG          -->  42: ENOMSG                      */
  65         43,     /*  36: EIDRM           -->  43: EIDRM                       */
  66         44,     /*  37: ECHRNG          -->  44: ECHRNG                      */
  67         45,     /*  38: EL2NSYNC        -->  45: EL2NSYNC            */
  68         46,     /*  39: EL3HLT          -->  46: EL3HLT                      */
  69         47,     /*  40: EL3RST          -->  47: EL3RST                      */
  70         48,     /*  41: ELNRNG          -->  48: ELNRNG                      */
  71         49,     /*  42: EUNATCH         -->  49: EUNATCH             */
  72         50,     /*  43: ENOCSI          -->  50: ENOCSI                      */
  73         51,     /*  44: EL2HLT          -->  51: EL2HLT                      */
  74         35,     /*  45: EDEADLK         -->  35: EDEADLK             */
  75         37,     /*  46: ENOLCK          -->  37: ENOLCK                      */
  76         125,    /*  47: ECANCELED       --> 125: ECANCELED           */
  77         38,     /*  48: ENOTSUP         -->  38: ENOSYS                      */
  78         122,    /*  49: EDQUOT          --> 122: EDQUOT                      */
  79         52,     /*  50: EBADE           -->  52: EBADE                       */
  80         53,     /*  51: EBADR           -->  53: EBADR                       */
  81         54,     /*  52: EXFULL          -->  54: EXFULL                      */
  82         55,     /*  53: ENOANO          -->  55: ENOANO                      */
  83         56,     /*  54: EBADRQC         -->  56: EBADRQC             */
  84         57,     /*  55: EBADSLT         -->  57: EBADSLT             */
  85         35,     /*  56: EDEADLOCK       -->  35: EDEADLK             */
  86         59,     /*  57: EBFONT          -->  59: EBFONT                      */
  87         130,    /*  58: EOWNERDEAD      --> 130: EOWNERDEAD          */
  88         131,    /*  59: ENOTRECOVERABLE --> 131: ENOTRECOVERABLE     */
  89         60,     /*  60: ENOSTR          -->  60: ENOSTR                      */
  90         61,     /*  61: ENODATA         -->  61: ENODATA             */
  91         62,     /*  62: ETIME           -->  62: ETIME                       */
  92         63,     /*  63: ENOSR           -->  63: ENOSR                       */
  93         64,     /*  64: ENONET          -->  64: ENONET                      */
  94         65,     /*  65: ENOPKG          -->  65: ENOPKG                      */
  95         66,     /*  66: EREMOTE         -->  66: EREMOTE             */
  96         67,     /*  67: ENOLINK         -->  67: ENOLINK             */
  97         68,     /*  68: EADV            -->  68: EADV                        */
  98         69,     /*  69: ESRMNT          -->  69: ESRMNT                      */
  99         70,     /*  70: ECOMM           -->  70: ECOMM                       */
 100         71,     /*  71: EPROTO          -->  71: EPROTO                      */
 101         -2,     /*  72: ELOCKUNMAPPED   -->  -2: No Analogue         */
 102         -2,     /*  73: ENOTACTIVE      -->  -2: No Analogue         */
 103         72,     /*  74: EMULTIHOP       -->  72: EMULTIHOP           */
 104         -1,     /*  75: Unused Number                                   */
 105         -1,     /*  76: Unused Number                                   */
 106         74,     /*  77: EBADMSG         -->  74: EBADMSG             */
 107         36,     /*  78: ENAMETOOLONG    -->  36: ENAMETOOLONG                */
 108         75,     /*  79: EOVERFLOW       -->  75: EOVERFLOW           */
 109         76,     /*  80: ENOTUNIQ        -->  76: ENOTUNIQ            */
 110         77,     /*  81: EBADFD          -->  77: EBADFD                      */
 111         78,     /*  82: EREMCHG         -->  78: EREMCHG             */
 112         79,     /*  83: ELIBACC         -->  79: ELIBACC             */
 113         80,     /*  84: ELIBBAD         -->  80: ELIBBAD             */
 114         81,     /*  85: ELIBSCN         -->  81: ELIBSCN             */
 115         82,     /*  86: ELIBMAX         -->  82: ELIBMAX             */
 116         83,     /*  87: ELIBEXEC        -->  83: ELIBEXEC            */
 117         84,     /*  88: EILSEQ          -->  84: EILSEQ                      */
 118         38,     /*  89: ENOSYS          -->  38: ENOSYS                      */
 119         40,     /*  90: ELOOP           -->  40: ELOOP                       */
 120         85,     /*  91: ERESTART        -->  85: ERESTART            */
 121         86,     /*  92: ESTRPIPE        -->  86: ESTRPIPE            */
 122         39,     /*  93: ENOTEMPTY       -->  39: ENOTEMPTY           */
 123         87,     /*  94: EUSERS          -->  87: EUSERS                      */
 124         88,     /*  95: ENOTSOCK        -->  88: ENOTSOCK            */
 125         89,     /*  96: EDESTADDRREQ    -->  89: EDESTADDRREQ                */
 126         90,     /*  97: EMSGSIZE        -->  90: EMSGSIZE            */
 127         91,     /*  98: EPROTOTYPE      -->  91: EPROTOTYPE          */
 128         92,     /*  99: ENOPROTOOPT     -->  92: ENOPROTOOPT         */
 129         -1,     /* 100: Unused Number                                   */
 130         -1,     /* 101: Unused Number                                   */
 131         -1,     /* 102: Unused Number                                   */
 132         -1,     /* 103: Unused Number                                   */
 133         -1,     /* 104: Unused Number                                   */
 134         -1,     /* 105: Unused Number                                   */
 135         -1,     /* 106: Unused Number                                   */
 136         -1,     /* 107: Unused Number                                   */
 137         -1,     /* 108: Unused Number                                   */
 138         -1,     /* 109: Unused Number                                   */
 139         -1,     /* 110: Unused Number                                   */
 140         -1,     /* 111: Unused Number                                   */
 141         -1,     /* 112: Unused Number                                   */
 142         -1,     /* 113: Unused Number                                   */
 143         -1,     /* 114: Unused Number                                   */
 144         -1,     /* 115: Unused Number                                   */
 145         -1,     /* 116: Unused Number                                   */
 146         -1,     /* 117: Unused Number                                   */
 147         -1,     /* 118: Unused Number                                   */
 148         -1,     /* 119: Unused Number                                   */
 149         93,     /* 120: EPROTONOSUPPORT -->  93: EPROTONOSUPPORT     */
 150         94,     /* 121: ESOCKTNOSUPPORT -->  94: ESOCKTNOSUPPORT     */
 151         95,     /* 122: EOPNOTSUPP      -->  95: EOPNOTSUPP          */
 152         96,     /* 123: EPFNOSUPPORT    -->  96: EPFNOSUPPORT                */
 153         97,     /* 124: EAFNOSUPPORT    -->  97: EAFNOSUPPORT                */
 154         98,     /* 125: EADDRINUSE      -->  98: EADDRINUSE          */
 155         99,     /* 126: EADDRNOTAVAIL   -->  99: EADDRNOTAVAIL               */
 156         100,    /* 127: ENETDOWN        --> 100: ENETDOWN            */
 157         101,    /* 128: ENETUNREACH     --> 101: ENETUNREACH         */
 158         102,    /* 129: ENETRESET       --> 102: ENETRESET           */
 159         103,    /* 130: ECONNABORTED    --> 103: ECONNABORTED                */
 160         104,    /* 131: ECONNRESET      --> 104: ECONNRESET          */
 161         105,    /* 132: ENOBUFS         --> 105: ENOBUFS             */
 162         106,    /* 133: EISCONN         --> 106: EISCONN             */
 163         107,    /* 134: ENOTCONN        --> 107: ENOTCONN            */
 164         -1,     /* 135: Unused Number                                   */
 165         -1,     /* 136: Unused Number                                   */
 166         -1,     /* 137: Unused Number                                   */
 167         -1,     /* 138: Unused Number                                   */
 168         -1,     /* 139: Unused Number                                   */
 169         -1,     /* 140: Unused Number                                   */
 170         -1,     /* 141: Unused Number                                   */
 171         -1,     /* 142: Unused Number                                   */
 172         108,    /* 143: ESHUTDOWN       --> 108: ESHUTDOWN           */
 173         109,    /* 144: ETOOMANYREFS    --> 109: ETOOMANYREFS                */
 174         110,    /* 145: ETIMEDOUT       --> 110: ETIMEDOUT           */
 175         111,    /* 146: ECONNREFUSED    --> 111: ECONNREFUSED                */
 176         112,    /* 147: EHOSTDOWN       --> 112: EHOSTDOWN           */
 177         113,    /* 148: EHOSTUNREACH    --> 113: EHOSTUNREACH                */
 178         114,    /* 149: EALREADY        --> 114: EALREADY            */
 179         115,    /* 150: EINPROGRESS     --> 115: EINPROGRESS         */
 180         116     /* 151: ESTALE          --> 116: ESTALE                      */
 181 };
 182 
 183 /*
 184  * Convert an illumos native error number to a Linux error number and return
 185  * it.  If no valid conversion is possible, the function fails back to the
 186  * value of "defval".  In userland, passing a default error number of "-1"
 187  * will abort the program if the error number could not be converted.
 188  */
 189 int
 190 lx_errno(int native_errno, int defval)
 191 {
 192 #ifdef  _KERNEL
 193         VERIFY3S(defval, >=, 0);
 194 #endif
 195 
 196         if (native_errno < 0 || native_errno >= (sizeof (lx_stol_errno) /
 197             sizeof (lx_stol_errno[0]))) {
 198 #ifndef _KERNEL
 199                 VERIFY3S(defval, >=, 0);
 200 #endif
 201 
 202                 return (defval);
 203         }
 204 
 205         return (lx_stol_errno[native_errno]);
 206 }