system.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. Unix SMB/Netbios implementation.
  3. Version 1.9.
  4. Samba system utilities
  5. Copyright (C) Andrew Tridgell 1992-1998
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. */
  18. #include "includes.h"
  19. extern int DEBUGLEVEL;
  20. /*
  21. The idea is that this file will eventually have wrappers around all
  22. important system calls in samba. The aims are:
  23. - to enable easier porting by putting OS dependent stuff in here
  24. - to allow for hooks into other "pseudo-filesystems"
  25. - to allow easier integration of things like the japanese extensions
  26. - to support the philosophy of Samba to expose the features of
  27. the OS within the SMB model. In general whatever file/printer/variable
  28. expansions/etc make sense to the OS should be acceptable to Samba.
  29. */
  30. /*******************************************************************
  31. this replaces the normal select() system call
  32. return if some data has arrived on one of the file descriptors
  33. return -1 means error
  34. ********************************************************************/
  35. #ifndef HAVE_SELECT
  36. static int pollfd(int fd)
  37. {
  38. int r=0;
  39. #ifdef HAS_RDCHK
  40. r = rdchk(fd);
  41. #elif defined(TCRDCHK)
  42. (void)ioctl(fd, TCRDCHK, &r);
  43. #else
  44. (void)ioctl(fd, FIONREAD, &r);
  45. #endif
  46. return(r);
  47. }
  48. int sys_select(int maxfd, fd_set *fds,struct timeval *tval)
  49. {
  50. fd_set fds2;
  51. int counter=0;
  52. int found=0;
  53. FD_ZERO(&fds2);
  54. while (1)
  55. {
  56. int i;
  57. for (i=0;i<maxfd;i++) {
  58. if (FD_ISSET(i,fds) && pollfd(i)>0) {
  59. found++;
  60. FD_SET(i,&fds2);
  61. }
  62. }
  63. if (found) {
  64. memcpy((void *)fds,(void *)&fds2,sizeof(fds2));
  65. return(found);
  66. }
  67. if (tval && tval->tv_sec < counter) return(0);
  68. sleep(1);
  69. counter++;
  70. }
  71. }
  72. #else /* !NO_SELECT */
  73. int sys_select(int maxfd, fd_set *fds,struct timeval *tval)
  74. {
  75. #ifdef USE_POLL
  76. struct pollfd pfd[256];
  77. int i;
  78. int maxpoll;
  79. int timeout;
  80. int pollrtn;
  81. maxpoll = 0;
  82. for( i = 0; i < maxfd; i++) {
  83. if(FD_ISSET(i,fds)) {
  84. struct pollfd *pfdp = &pfd[maxpoll++];
  85. pfdp->fd = i;
  86. pfdp->events = POLLIN;
  87. pfdp->revents = 0;
  88. }
  89. }
  90. timeout = (tval != NULL) ? (tval->tv_sec * 1000) + (tval->tv_usec/1000) :
  91. -1;
  92. errno = 0;
  93. do {
  94. pollrtn = poll( &pfd[0], maxpoll, timeout);
  95. } while (pollrtn<0 && errno == EINTR);
  96. FD_ZERO(fds);
  97. for( i = 0; i < maxpoll; i++)
  98. if( pfd[i].revents & POLLIN )
  99. FD_SET(pfd[i].fd,fds);
  100. return pollrtn;
  101. #else /* USE_POLL */
  102. struct timeval t2;
  103. int selrtn;
  104. do {
  105. if (tval) memcpy((void *)&t2,(void *)tval,sizeof(t2));
  106. errno = 0;
  107. selrtn = select(maxfd,SELECT_CAST fds,NULL,NULL,tval?&t2:NULL);
  108. } while (selrtn<0 && errno == EINTR);
  109. return(selrtn);
  110. }
  111. #endif /* USE_POLL */
  112. #endif /* NO_SELECT */
  113. /*******************************************************************
  114. A stat() wrapper that will deal with 64 bit filesizes.
  115. ********************************************************************/
  116. int sys_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
  117. {
  118. return stat(fname, sbuf);
  119. }
  120. /*******************************************************************
  121. An lstat() wrapper that will deal with 64 bit filesizes.
  122. ********************************************************************/
  123. #if 0
  124. int sys_lstat(const char *fname,SMB_STRUCT_STAT *sbuf)
  125. {
  126. return lstat(fname, sbuf);
  127. }
  128. /*******************************************************************
  129. An fseek() wrapper that will deal with 64 bit filesizes.
  130. ********************************************************************/
  131. int sys_fseek(FILE *fp, SMB_OFF_T offset, int whence)
  132. {
  133. return fseek(fp, offset, whence);
  134. }
  135. /*******************************************************************
  136. An ftell() wrapper that will deal with 64 bit filesizes.
  137. ********************************************************************/
  138. SMB_OFF_T sys_ftell(FILE *fp)
  139. {
  140. return (SMB_OFF_T)ftell(fp);
  141. }
  142. #endif /* 0 */
  143. /*******************************************************************
  144. An open() wrapper that will deal with 64 bit filesizes.
  145. ********************************************************************/
  146. int sys_open(const char *path, int oflag, mode_t mode)
  147. {
  148. return open(path, oflag, mode);
  149. }
  150. /*******************************************************************
  151. An fopen() wrapper that will deal with 64 bit filesizes.
  152. ********************************************************************/
  153. FILE *sys_fopen(const char *path, const char *type)
  154. {
  155. return fopen(path, type);
  156. }
  157. #if 0
  158. /*******************************************************************
  159. A readdir wrapper that will deal with 64 bit filesizes.
  160. ********************************************************************/
  161. SMB_STRUCT_DIRENT *sys_readdir(DIR *dirp)
  162. {
  163. return readdir(dirp);
  164. }
  165. /*******************************************************************
  166. system wrapper for getwd
  167. ********************************************************************/
  168. char *sys_getwd(char *s)
  169. {
  170. char *wd;
  171. #ifdef HAVE_GETCWD
  172. wd = (char *)getcwd(s, sizeof (pstring));
  173. #else
  174. wd = (char *)getwd(s);
  175. #endif
  176. return wd;
  177. }
  178. /*******************************************************************
  179. chown isn't used much but OS/2 doesn't have it
  180. ********************************************************************/
  181. int sys_chown(const char *fname,uid_t uid,gid_t gid)
  182. {
  183. #ifndef HAVE_CHOWN
  184. static int done;
  185. if (!done) {
  186. DEBUG(1,("WARNING: no chown!\n"));
  187. done=1;
  188. }
  189. #else
  190. return(chown(fname,uid,gid));
  191. #endif
  192. }
  193. #endif /* 0 */
  194. /**************************************************************************
  195. A wrapper for gethostbyname() that tries avoids looking up hostnames
  196. in the root domain, which can cause dial-on-demand links to come up for no
  197. apparent reason.
  198. ****************************************************************************/
  199. struct hostent *sys_gethostbyname(const char *name)
  200. {
  201. #ifdef REDUCE_ROOT_DNS_LOOKUPS
  202. char query[256], hostname[256];
  203. char *domain;
  204. /* Does this name have any dots in it? If so, make no change */
  205. if (strchr(name, '.'))
  206. return(gethostbyname(name));
  207. /* Get my hostname, which should have domain name
  208. attached. If not, just do the gethostname on the
  209. original string.
  210. */
  211. gethostname(hostname, sizeof(hostname) - 1);
  212. hostname[sizeof(hostname) - 1] = 0;
  213. if ((domain = strchr(hostname, '.')) == NULL)
  214. return(gethostbyname(name));
  215. /* Attach domain name to query and do modified query.
  216. If names too large, just do gethostname on the
  217. original string.
  218. */
  219. if((strlen(name) + strlen(domain)) >= sizeof(query))
  220. return(gethostbyname(name));
  221. slprintf(query, sizeof(query)-1, "%s%s", name, domain);
  222. return(gethostbyname(query));
  223. #else /* REDUCE_ROOT_DNS_LOOKUPS */
  224. return(gethostbyname(name));
  225. #endif /* REDUCE_ROOT_DNS_LOOKUPS */
  226. }
  227. /**************************************************************************
  228. Wrapper for random().
  229. ****************************************************************************/
  230. #if 0
  231. long sys_random(void)
  232. {
  233. #if defined(HAVE_RANDOM)
  234. return (long)random();
  235. #elif defined(HAVE_RAND)
  236. return (long)rand();
  237. #else
  238. DEBUG(0,("Error - no random function available !\n"));
  239. exit(1);
  240. #endif
  241. }
  242. /**************************************************************************
  243. Wrapper for srandom().
  244. ****************************************************************************/
  245. void sys_srandom(unsigned int seed)
  246. {
  247. #if defined(HAVE_SRANDOM)
  248. srandom(seed);
  249. #elif defined(HAVE_SRAND)
  250. srand(seed);
  251. #else
  252. DEBUG(0,("Error - no srandom function available !\n"));
  253. exit(1);
  254. #endif
  255. }
  256. #endif /* 0 */