IO.C 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. *+SNOTICE
  25. *
  26. *
  27. * $TOG: IO.C /main/33 1999/01/29 14:45:00 mgreess $
  28. *
  29. * RESTRICTED CONFIDENTIAL INFORMATION:
  30. *
  31. * The information in this document is subject to special
  32. * restrictions in a confidential disclosure agreement bertween
  33. * HP, IBM, Sun, USL, SCO and Univel. Do not distribute this
  34. * document outside HP, IBM, Sun, USL, SCO, or Univel wihtout
  35. * Sun's specific written approval. This documment and all copies
  36. * and derivative works thereof must be returned or destroyed at
  37. * Sun's request.
  38. *
  39. * Copyright 1993 Sun Microsystems, Inc. All rights reserved.
  40. *
  41. *+ENOTICE
  42. */
  43. #define X_INCLUDE_TIME_H
  44. #define X_INCLUDE_GRP_H
  45. #define X_INCLUDE_PWD_H
  46. #define XOS_USE_NO_LOCKING
  47. #include <X11/Xos_r.h>
  48. #include <assert.h>
  49. #include <errno.h>
  50. #include <fcntl.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <stdarg.h>
  54. #include <stdlib.h>
  55. //
  56. // START Order dependent for AIX
  57. //
  58. #include <sys/socket.h>
  59. #if defined(_aix)
  60. #include <sys/socketvar.h>
  61. #if defined(BIG_ENDIAN)
  62. #undef BIG_ENDIAN
  63. #endif
  64. #endif
  65. #include <netinet/in.h>
  66. #include <netdb.h>
  67. #include <arpa/inet.h>
  68. //
  69. // END Order dependent for AIX
  70. //
  71. #include <sys/stat.h>
  72. #if !defined(__linux__)
  73. # include <sys/statvfs.h>
  74. #endif
  75. #include <sys/types.h>
  76. #include <sys/wait.h>
  77. #include <sys/uio.h>
  78. #include <unistd.h>
  79. #ifdef _AIX
  80. #include <sys/mntctl.h>
  81. #include <sys/statfs.h>
  82. #include <sys/vfs.h>
  83. #define IOVMAX MSG_MAXIOVLEN
  84. extern "C"
  85. {
  86. fchmod(int, mode_t);
  87. statfs(const char *, struct statfs *);
  88. mntctl(int, int, char *);
  89. ssize_t writev(int, const struct iovec *, int);
  90. }
  91. #if (OSMAJORVERSION==4) && (OSMINORVERSION==2)
  92. /* Temporary hack till the /usr/lpp/xlC/include/unistd.h file is fixed. */
  93. extern "C" { int lockf(int, int, off_t); }
  94. #endif
  95. #endif /* _AIX */
  96. #include <DtMail/DtMail.hh>
  97. #include <DtMail/DtMailError.hh>
  98. #include <DtMail/IO.hh>
  99. #include <DtMail/Threads.hh>
  100. #if !defined(IOV_MAX)
  101. #if !defined(__linux__)
  102. #include <sys/stream.h>
  103. #endif
  104. #if !defined(DEF_IOV_MAX)
  105. #define DEF_IOV_MAX 16
  106. #endif
  107. #define IOV_MAX DEF_IOV_MAX
  108. #endif
  109. #define IOVEC_IOVBASE_INCREMENTOR(iov, bytes) \
  110. ((caddr_t)((size_t)iov->iov_base+bytes))
  111. // The following I/O routines are wrappers for the normal routines,
  112. // but they deal with EINTR, and partial read/write situations.
  113. //
  114. //
  115. int
  116. SafeOpen(const char * path, int oflag, mode_t mode)
  117. {
  118. int status;
  119. do {
  120. status = open(path, oflag, mode);
  121. } while (status < 0 && errno == EINTR);
  122. return(status);
  123. }
  124. int
  125. SafeClose(int fd)
  126. {
  127. int status;
  128. do {
  129. status = close(fd);
  130. } while (status < 0 && errno == EINTR);
  131. return(status);
  132. }
  133. int
  134. SafeLink(const char * existingPath, const char * newPath)
  135. {
  136. int status;
  137. do {
  138. status = link(existingPath, newPath);
  139. } while (status < 0 && errno == EINTR);
  140. return(status);
  141. }
  142. int
  143. SafeRename(const char * oldPath, const char * newPath)
  144. {
  145. int status;
  146. do {
  147. status = rename(oldPath, newPath);
  148. } while (status < 0 && errno == EINTR);
  149. return(status);
  150. }
  151. int
  152. SafeRemove(const char *path)
  153. {
  154. int status;
  155. do {
  156. status = remove(path);
  157. } while (status < 0 && errno == EINTR);
  158. return(status);
  159. }
  160. int
  161. SafeUnlink(const char *path)
  162. {
  163. int status;
  164. do {
  165. status = unlink(path);
  166. } while (status < 0 && errno == EINTR);
  167. return(status);
  168. }
  169. ssize_t
  170. SafeRead(int fd, void * buf, size_t bytes)
  171. {
  172. ssize_t status = 0;
  173. do {
  174. status = read(fd, buf, bytes);
  175. } while(status < 0 && errno == EINTR);
  176. return(status);
  177. }
  178. ssize_t
  179. SafeWrite(int fd, const void * buf, size_t bytes)
  180. {
  181. ssize_t status = 0;
  182. do {
  183. status = write(fd, buf, bytes);
  184. } while(status < 0 && errno == EINTR);
  185. return(status);
  186. }
  187. // SafeWritev -- safe multiply vectored write
  188. // Description:
  189. // SafeWritev provides an interrupt safe way to call the writev() system
  190. // call. In addition, it removes the IOV_MAX limitation on the size of the
  191. // iovec structure, and it will not return until either all bytes specified
  192. // by the iovec structure are written or writev() returns an error.
  193. // Arguments:
  194. // int fd -- file descriptor to write to
  195. // struct iovec *iov -- iovec structure describing writes to be done
  196. // int iovcnt -- number of elements in passed in iov structure
  197. // Outputs:
  198. // Will effectively destroy the contents of the passed in iovec structure.
  199. // The caller must deallocate the storage associated with the structure
  200. // upon regaining control.
  201. // Returns:
  202. // == -1 : error returned from writev() - errno set to specific error number
  203. // != -1 : number of bytes actually written to the file
  204. unsigned long
  205. SafeWritev(int fd, struct iovec *iov, int iovcnt)
  206. {
  207. ssize_t status;
  208. unsigned long bytesWritten = 0;
  209. // outer loop: starting with the first write vector, as long as there is
  210. // at least one vector left to feed writev(), do so
  211. //
  212. for(int curIov = 0; curIov < iovcnt; ) {
  213. // inner loop: feed writev() allowing for interrupted system call
  214. do {
  215. status = writev(
  216. fd,
  217. &iov[curIov],
  218. (iovcnt-curIov) > IOV_MAX ? IOV_MAX : (iovcnt-curIov));
  219. } while (status < 0 && errno == EINTR);
  220. if (status == (ssize_t)-1) // true error from writev??
  221. return((unsigned long)-1); // yes: bail at this point to caller
  222. bytesWritten += status; // bump # bytes written count
  223. // must now "walk through" the io vector until we are up the to point
  224. // indicated by the number of bytes written by writev() - any leftover
  225. // in status indicates a partial write of a vector
  226. //
  227. while ((status > 0) && (curIov < iovcnt) && (iov[curIov].iov_len <= status)) {
  228. status -= iov[curIov++].iov_len;
  229. }
  230. // Check to see if we have reached the end of the io vector array; also
  231. // check to see if more bytes were written than called for; as crazy as
  232. // this sounds, in at least one instance when we finish spinning through
  233. // the io vectors we still had bytes left that had been written but not
  234. // accounted for in the io vectors (status > 0 && curIov >= iovcnt)
  235. //
  236. if (curIov >= iovcnt) { // out of IO vectors?
  237. if (status > 0) { // yes: all data accounted for?
  238. DtMailEnv error; // NO:: log error condition
  239. error.logError(
  240. DTM_TRUE,
  241. "SafeWritev: writev(): too many bytes written (%d/%d)\n",
  242. status, bytesWritten);
  243. }
  244. continue; // continue and let for loop exit
  245. }
  246. // Check for a partial write: if the current vector contains more data
  247. // than what has been written, writev() bailed in the middle of writing
  248. // a vector - adjust the vector and and feed it back to writev() again
  249. // OTHERWISE writev() ended with the current vector so move on to the next
  250. //
  251. if (iov[curIov].iov_len == status) // full write of this vector?
  252. curIov++; // yes: move on to the next vector
  253. else if (status != 0) { // no: adjust this vector and retry
  254. iov[curIov].iov_len -= status;
  255. iov[curIov].iov_base = IOVEC_IOVBASE_INCREMENTOR((&iov[curIov]), status);
  256. }
  257. }
  258. return(bytesWritten);
  259. }
  260. #define SWS_BUFFERSIZE 1024
  261. ssize_t
  262. SafeWriteStrip(int fd, const void * buf, size_t bytes)
  263. {
  264. ssize_t status = 0;
  265. int i, j;
  266. char *ptr = (char*)buf, *writebuf;
  267. // bug 5856: don't write out control M
  268. // make a finite size buffer for writing
  269. writebuf = (char*) malloc(bytes < SWS_BUFFERSIZE ? bytes : SWS_BUFFERSIZE);
  270. for (i = 0, j = 0; i < bytes; i++, ptr++) {
  271. if (*ptr == '\r' && *(ptr+1) == '\n')
  272. continue;
  273. writebuf[j++] = *ptr;
  274. if (j == SWS_BUFFERSIZE || i == (bytes-1)) {
  275. do {
  276. status = write(fd, writebuf, j);
  277. } while(status < 0 && errno == EINTR);
  278. j = 0;
  279. }
  280. }
  281. free(writebuf);
  282. return(status);
  283. }
  284. int
  285. SafeStat(const char * path, struct stat * buf)
  286. {
  287. int status;
  288. do {
  289. status = stat(path, buf);
  290. } while (status < 0 && errno == EINTR);
  291. return(status);
  292. }
  293. int
  294. SafeFchown(int fd, uid_t owner, gid_t group)
  295. {
  296. int status;
  297. do {
  298. status = fchown(fd, owner, group);
  299. } while (status < 0 && errno == EINTR);
  300. return(status);
  301. }
  302. int
  303. SafeLStat(const char * path, struct stat * buf)
  304. {
  305. int status;
  306. do {
  307. status = lstat(path, buf);
  308. } while (status < 0 && errno == EINTR);
  309. return(status);
  310. }
  311. int
  312. SafeFStat(int fd, struct stat * buf)
  313. {
  314. int status;
  315. do {
  316. status = fstat(fd, buf);
  317. } while (status < 0 && errno == EINTR);
  318. return(status);
  319. }
  320. // SafeGuaranteedStat - return stat info for object given path name
  321. // If NFS attribute caching is enabled (which is the default), a
  322. // stat/fstat of a NFS file may not return the correct true size of the
  323. // mailbox if it has been appended to since the last time it was read.
  324. // To get around this problem, this function will perform a open(),
  325. // read() of 1 byte, fstat(), close() which will force the attributes
  326. // for the named file to be retrieved directly from the server.
  327. //
  328. int
  329. SafeGuaranteedStat(const char * path, struct stat * buf)
  330. {
  331. int saveErrno;
  332. #ifndef O_RSYNC
  333. int tempFd = SafeOpen(path, O_RDONLY|O_SYNC);
  334. #else
  335. int tempFd = SafeOpen(path, O_RDONLY|O_RSYNC|O_SYNC);
  336. #endif /* O_RSYNC */
  337. if (tempFd == -1) {
  338. return(-1);
  339. }
  340. char tempBuf;
  341. if (SafeRead(tempFd, &tempBuf, 1) == -1) {
  342. saveErrno = errno;
  343. (void) SafeClose(tempFd);
  344. errno = saveErrno;
  345. return(-1);
  346. }
  347. if (SafeFStat(tempFd, buf) == -1) {
  348. saveErrno = errno;
  349. (void) SafeClose(tempFd);
  350. errno = saveErrno;
  351. return(-1);
  352. }
  353. (void) SafeClose(tempFd);
  354. return(0);
  355. }
  356. int
  357. SafeTruncate(const char * path, off_t len)
  358. {
  359. int status;
  360. do {
  361. status = truncate((char *)path, len); // The cast is for AIX
  362. } while (status < 0 && errno == EINTR);
  363. return(status);
  364. }
  365. int
  366. SafeFTruncate(int fd, off_t len)
  367. {
  368. int status;
  369. do {
  370. status = ftruncate(fd, len);
  371. } while (status < 0 && errno == EINTR);
  372. return(status);
  373. }
  374. int
  375. SafeAccess(const char * path, int amode)
  376. {
  377. int status;
  378. do {
  379. status = access(path, amode);
  380. } while (status < 0 && errno == EINTR);
  381. return(status);
  382. }
  383. #if defined(sun)
  384. #define LOCKF_SIZE_TYPE long
  385. #else
  386. #define LOCKF_SIZE_TYPE off_t
  387. #endif
  388. int
  389. SafeLockf(int fd, int func, long size)
  390. {
  391. int status;
  392. do {
  393. status = lockf(fd, func, (LOCKF_SIZE_TYPE) size);
  394. } while (status < 0 && errno == EINTR);
  395. return(status);
  396. }
  397. int
  398. SafeFChmod(int fd, mode_t mode)
  399. {
  400. int status;
  401. do {
  402. status = fchmod(fd, mode);
  403. } while (status < 0 && errno == EINTR);
  404. return(status);
  405. }
  406. int
  407. SafeDup2(int fd1, int fd2)
  408. {
  409. int status;
  410. do {
  411. status = dup2(fd1, fd2);
  412. } while (status < 0 && errno == EINTR);
  413. return(status);
  414. }
  415. int
  416. SafeExecvp(const char * file, char *const *argv)
  417. {
  418. int status;
  419. do {
  420. status = execvp(file, (char * const *)argv);
  421. } while (status < 0 && errno == EINTR);
  422. return(status);
  423. }
  424. int
  425. SafeWaitpid(pid_t proc, int * p_stat, int options)
  426. {
  427. pid_t status;
  428. do {
  429. status = waitpid(proc, p_stat, options);
  430. } while (status < 0 && errno == EINTR);
  431. return((int)status);
  432. }
  433. int
  434. SafeWait(int * p_stat)
  435. {
  436. pid_t status;
  437. do {
  438. status = wait(p_stat);
  439. } while (status < 0 && errno == EINTR);
  440. return((int)status);
  441. }
  442. int
  443. SafeUTime(const char * path, utimbuf * ntime)
  444. {
  445. int status;
  446. do {
  447. status = utime(path, ntime);
  448. } while (status < 0 && errno == EINTR);
  449. return(status);
  450. }
  451. void
  452. SafePathIsAccessible(DtMailEnv &error, const char *path)
  453. {
  454. int status;
  455. status = SafeAccess(path, F_OK);
  456. if (-1 == status)
  457. {
  458. if (EACCES == errno)
  459. error.vSetError(DTME_PathElementPermissions, DTM_FALSE, NULL, path);
  460. else if (ENOTDIR == errno)
  461. error.vSetError(DTME_PathElementNotDirectory, DTM_FALSE, NULL, path);
  462. else if (ENOENT == errno)
  463. {
  464. char *s, *t;
  465. t = strdup(path);
  466. s = strrchr(t, '/');
  467. if (s && s != t)
  468. {
  469. *s = '\0';
  470. status = SafeAccess(t, F_OK);
  471. if (-1 == status)
  472. error.vSetError(
  473. DTME_PathElementDoesNotExist,
  474. DTM_FALSE, NULL,
  475. path);
  476. }
  477. }
  478. }
  479. }
  480. #if defined(POSIX_THREADS)
  481. static void * time_mutex = NULL;
  482. #endif
  483. void
  484. SafeCtime(const time_t *clock, char * buf, int buflen)
  485. {
  486. _Xctimeparams ctime_buf;
  487. char *result;
  488. memset((void*) &ctime_buf, 0, sizeof(_Xctimeparams));
  489. result = _XCtime(clock, ctime_buf);
  490. if (result)
  491. strncpy(buf, result, buflen);
  492. else
  493. *buf = '\0';
  494. }
  495. void
  496. SafeLocaltime(const time_t *clock, tm & result)
  497. {
  498. struct tm *time_ptr;
  499. _Xltimeparams localtime_buf;
  500. memset((void*) &localtime_buf, 0, sizeof(_Xltimeparams));
  501. time_ptr = _XLocaltime(clock, localtime_buf);
  502. result = *time_ptr;
  503. }
  504. time_t
  505. SafeMktime(tm * timeptr)
  506. {
  507. #if defined(POSIX_THREADS)
  508. if (!time_mutex) {
  509. time_mutex = MutexInit();
  510. }
  511. MutexLock lock_scope(time_mutex);
  512. #endif
  513. return(mktime(timeptr));
  514. }
  515. size_t
  516. SafeStrftime(char * buf, size_t buf_size,
  517. const char * format, const tm * timeptr)
  518. {
  519. #if defined(POSIX_THREADS)
  520. if (!time_mutex) {
  521. time_mutex = MutexInit();
  522. }
  523. MutexLock lock_scope(time_mutex);
  524. #endif
  525. return(strftime(buf, buf_size, format, timeptr));
  526. }
  527. #define SockINTERNAL_BUFSIZE 2048
  528. void *SockOpen(char *host, int clientPort, char **errorstring)
  529. {
  530. int sockfd;
  531. unsigned long inaddr;
  532. struct sockaddr_in ad;
  533. struct hostent *hp;
  534. DtMailEnv error;
  535. char *errorfmt = NULL;
  536. memset(&ad, 0, sizeof(ad));
  537. ad.sin_family = AF_INET;
  538. inaddr = inet_addr(host);
  539. if (inaddr != -1)
  540. memcpy(&ad.sin_addr, &inaddr, sizeof(inaddr));
  541. else
  542. {
  543. hp = gethostbyname(host);
  544. if (hp == NULL)
  545. {
  546. if (NULL != errorstring)
  547. {
  548. errorfmt = DtMailEnv::getMessageText(
  549. SockErrorSet, 2,
  550. "Unknown host: %s");
  551. if (NULL == *errorstring) *errorstring = (char*) malloc(BUFSIZ);
  552. sprintf(*errorstring, errorfmt, host);
  553. }
  554. return (FILE*) NULL;
  555. }
  556. memcpy(&ad.sin_addr, hp->h_addr, hp->h_length);
  557. }
  558. ad.sin_port = htons(clientPort);
  559. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  560. if (sockfd < 0)
  561. {
  562. if (NULL != errorstring)
  563. {
  564. errorfmt = DtMailEnv::getMessageText(
  565. SockErrorSet, 3,
  566. "Error creating socket: %s");
  567. if (NULL == *errorstring) *errorstring = (char*) malloc(BUFSIZ);
  568. sprintf(*errorstring, errorfmt, error.errnoMessage());
  569. }
  570. return (FILE*) NULL;
  571. }
  572. if (connect(sockfd, (struct sockaddr *) &ad, sizeof(ad)) < 0)
  573. {
  574. if (NULL != errorstring)
  575. {
  576. errorfmt = DtMailEnv::getMessageText(
  577. SockErrorSet, 3,
  578. "Error connecting to socket: %s");
  579. if (NULL == *errorstring) *errorstring = (char*) malloc(BUFSIZ);
  580. sprintf(*errorstring, errorfmt, error.errnoMessage());
  581. }
  582. close(sockfd);
  583. return (FILE*) NULL;
  584. }
  585. #if defined(USE_SOCKSTREAM)
  586. FILE *sockfp = fdopen(sockfd, "r+");
  587. setvbuf(sockfp, NULL, _IONBF, SockINTERNAL_BUFSIZE);
  588. return (void*) sockfp;
  589. #else
  590. return (void*) sockfd;
  591. #endif
  592. }
  593. int SockPrintf(void *sockfp, char* format, ...)
  594. {
  595. va_list ap;
  596. char *buf = new char[8192];
  597. int i;
  598. va_start(ap, format);
  599. vsprintf(buf, format, ap);
  600. va_end(ap);
  601. i = SockWrite(buf, 1, strlen(buf), sockfp);
  602. delete [] buf;
  603. return i;
  604. }
  605. char *SockGets(char *buf, int len, void *sockfp)
  606. {
  607. #if defined(USE_SOCKSTREAM)
  608. char *in = fgets(buf, len, (FILE*) sockfp);
  609. fseek((FILE*) sockfp, 0L, SEEK_CUR); /* required by POSIX */
  610. return in;
  611. #else
  612. size_t n;
  613. char *bufp;
  614. n = 0;
  615. bufp = buf-1;
  616. do
  617. {
  618. bufp++;
  619. read((int) sockfp, (void*) bufp, 1);
  620. n++;
  621. } while (*bufp != '\n');
  622. *(bufp+1) = '\0';
  623. return buf;
  624. #endif
  625. }
  626. int SockRead(char *buf, int size, int len, void *sockfp)
  627. {
  628. #if defined(USE_SOCKSTREAM)
  629. int n = fread(buf, size, len, (FILE*) sockfp);
  630. fseek((FILE*) sockfp, 0L, SEEK_CUR); /* required by POSIX */
  631. #else
  632. int n = (int) read((int) sockfp, (void*) buf, (size_t) size * len);
  633. #endif
  634. return n;
  635. }
  636. int SockWrite(char *buf, int size, int len, void *sockfp)
  637. {
  638. int n;
  639. #if defined(USE_SOCKSTREAM)
  640. n = fwrite(buf, size, len, (FILE*) sockfp);
  641. fseek((FILE*) sockfp, 0L, SEEK_CUR); /* required by POSIX */
  642. #else
  643. n = write((int) sockfp, buf, size * len);
  644. #endif
  645. return n;
  646. }
  647. void SockClose(void *sockfp)
  648. {
  649. #if defined(USE_SOCKSTREAM)
  650. fclose((FILE*) sockfp);
  651. #else
  652. close((int) sockfp);
  653. #endif
  654. }
  655. void
  656. GetGroupName(char * grp_name)
  657. {
  658. struct group *grp;
  659. _Xgetgrparams grp_buf;
  660. memset((void*) &grp_buf, 0, sizeof(_Xgetgrparams));
  661. grp = _XGetgrgid(getegid(), grp_buf);
  662. if (grp) {
  663. strcpy(grp_name, grp->gr_name);
  664. }
  665. else {
  666. strcpy(grp_name, "UNKNOWN_GROUP");
  667. }
  668. }
  669. gid_t
  670. GetIdForGroupName(char * grp_name)
  671. {
  672. assert(grp_name != NULL);
  673. struct group *grp;
  674. _Xgetgrparams grp_buf;
  675. memset((void*) &grp_buf, 0, sizeof(_Xgetgrparams));
  676. grp = _XGetgrnam(grp_name, grp_buf);
  677. return(grp ? grp->gr_gid : (gid_t)-1);
  678. }
  679. void
  680. GetPasswordEntry(passwd & result)
  681. {
  682. static struct passwd passwordEntry;
  683. static int oneTimeFlag = 0;
  684. if (!oneTimeFlag) {
  685. _Xgetpwparams pw_buf;
  686. struct passwd *tresult;
  687. memset((void*) &pw_buf, 0, sizeof(_Xgetpwparams));
  688. #if defined(_AIX)
  689. _Xos_processLock;
  690. tresult = getpwuid(getuid());
  691. _Xos_processUnlock;
  692. #else
  693. tresult = _XGetpwuid(getuid(), pw_buf);
  694. #endif
  695. assert(tresult != NULL);
  696. memcpy(&passwordEntry, tresult, sizeof(struct passwd));
  697. passwordEntry.pw_name = strdup(passwordEntry.pw_name);
  698. passwordEntry.pw_passwd = strdup(passwordEntry.pw_passwd);
  699. #if !defined(_AIX) && !defined(__linux__) && !defined(CSRG_BASED)
  700. passwordEntry.pw_age = strdup(passwordEntry.pw_age);
  701. passwordEntry.pw_comment = strdup(passwordEntry.pw_comment);
  702. #endif
  703. passwordEntry.pw_gecos = strdup(passwordEntry.pw_gecos);
  704. passwordEntry.pw_dir = strdup(passwordEntry.pw_dir);
  705. passwordEntry.pw_shell = strdup(passwordEntry.pw_shell);
  706. oneTimeFlag++;
  707. }
  708. memcpy(&result, &passwordEntry, sizeof(struct passwd));
  709. return;
  710. }
  711. #if defined(MAILGROUP_REQUIRED)
  712. int isSetMailGidNeeded(const char * mailboxPath)
  713. {
  714. assert(mailboxPath);
  715. char inbox_path[1024];
  716. char mbox_path[1024];
  717. struct passwd *pw;
  718. struct stat buf;
  719. struct stat buf1;
  720. _Xgetpwparams pw_buf;
  721. memset((void*) &pw_buf, 0, sizeof(_Xgetpwparams));
  722. pw = _XGetpwuid(getuid(), pw_buf);
  723. // construct the lockfile name for the user.
  724. char *lock_file = (char *)calloc(1,strlen(pw->pw_name) + 6);
  725. strcpy(lock_file,pw->pw_name);
  726. strcat(lock_file,".lock");
  727. // parse the mailfolder name from the path
  728. char *p = strrchr(mailboxPath,'/');
  729. int len = (NULL!=0) ? strlen(mailboxPath)-strlen(p) : strlen(mailboxPath);
  730. char *str = (char *) calloc(1, len+1);
  731. strncpy(str, mailboxPath, len);
  732. str[len] = '\0';
  733. stat(str,&buf);
  734. // Default or system mailbox dir name.
  735. strcpy(inbox_path, "/var/spool/mail");
  736. stat(inbox_path,&buf1);
  737. free(str);
  738. if( ( (buf.st_dev == buf1.st_dev) && (buf.st_ino == buf1.st_ino) ) )
  739. {
  740. if( !strcmp((p+1),pw->pw_name) || !strcmp((p+1),lock_file) )
  741. {
  742. if( access(mailboxPath,R_OK) == -1 )
  743. {
  744. free(lock_file);
  745. return (1);
  746. }
  747. }
  748. free(lock_file);
  749. return ( 0 );
  750. }
  751. else
  752. {
  753. free(lock_file);
  754. return ( 1 );
  755. }
  756. }
  757. #endif
  758. #ifdef _AIX
  759. /*
  760. * NAME: get_stat
  761. *
  762. * FUNCTION:
  763. * gather mount status for all virtual mounts for this host.
  764. *
  765. * EXECUTION ENVIRONMENT: Part of user command.
  766. *
  767. * ON ENTRY:
  768. * vmountpp pointer to a buffer in which to put mount info
  769. *
  770. * NOTES:
  771. * get_stat() was lifted and slightly modified from
  772. * AIX Version 3 df.c.
  773. *
  774. * RETURNS:
  775. * < 0 error in mntctl()
  776. * > 0 for number of struct vmounts (mount points) in
  777. * buffer which is pointed to by pointer left at
  778. * *vmountpp.
  779. */
  780. int get_stat(
  781. struct vmount **vmountpp) /* place to tell where buffer is */
  782. {
  783. size_t size;
  784. struct vmount *vm;
  785. int nmounts;
  786. int count;
  787. size = BUFSIZ; /* initial default size */
  788. count = 10; /* don't try forever */
  789. while (count--) { /* don't try it forever */
  790. if ((vm = (struct vmount*) malloc(size)) == NULL) {
  791. return(-1);
  792. }
  793. /*
  794. * perform the QUERY mntctl - if it returns > 0, that is the
  795. * number of vmount structures in the buffer. If it returns
  796. * -1, an error occurred. If it returned 0, then look in
  797. * first word of buffer for needed size.
  798. */
  799. if ((nmounts = mntctl(MCTL_QUERY, size, (caddr_t)vm)) > 0) {
  800. *vmountpp = vm; /* OK, got it, now return */
  801. return(nmounts);
  802. } else {
  803. if (nmounts == 0) {
  804. size = *(int *)vm; /* the buffer wasn't big enough */
  805. free((void *)vm); /* get required buffer size */
  806. } else {
  807. free((void *)vm);/* some other kind of error occurred*/
  808. return(-1);
  809. }
  810. }
  811. }
  812. return(-1);
  813. }
  814. /*
  815. * NAME: get_vmount
  816. *
  817. * FUNCTION:
  818. * Determines, via the filesystems vmount structures,
  819. * the vmount id of the the filesystem id provided as
  820. * an argument (enables ultimate access to the actual
  821. * name of the filesystem).
  822. *
  823. * EXECUTION ENVIRONMENT: Part of user command.
  824. *
  825. * RETURNS:
  826. * ptr to structure with vmount id of filesystem or NULL
  827. */
  828. struct vmount *get_vmount(fsid_t *fsid)
  829. {
  830. struct vmount *inu_vmount_p=NULL;
  831. int inu_vmount_num;
  832. struct vmount *vm;
  833. int nmount;
  834. /* make sure we have all the virtual mount status of this host */
  835. if(inu_vmount_p == NULL)
  836. inu_vmount_num = get_stat(&inu_vmount_p);
  837. /* get the number of struct vmount in the vmount buffer */
  838. nmount = inu_vmount_num;
  839. /* go thru all the structures in the vmount buffer */
  840. for (vm = inu_vmount_p; nmount; nmount--,
  841. vm = (struct vmount *)((char *)vm + vm->vmt_length)) {
  842. if(( vm->vmt_fsid.fsid_dev == fsid->fsid_dev ) &&
  843. ( vm->vmt_fsid.fsid_type == fsid->fsid_type ))
  844. return(vm);
  845. }
  846. return((struct vmount *)NULL);
  847. }
  848. #endif /* _AIX */
  849. int FileSystemSpace(const char *file_path, size_t bytes, char **fsname)
  850. {
  851. int fserror=FALSE;
  852. struct stat stat_buf;
  853. size_t req_space = 0;
  854. #if !defined(__linux__)
  855. struct statvfs statvfs_buf;
  856. #endif
  857. if (stat(file_path,&stat_buf) < 0) return 0;
  858. #if !defined(__linux__)
  859. if (statvfs(file_path,&statvfs_buf) < 0) return 0;
  860. #endif
  861. #ifdef _AIX
  862. struct statfs statfs_buf;
  863. if (statfs(file_path,&statfs_buf) < 0) return 0;
  864. if (statfs_buf.f_vfstype == MNT_NFS)
  865. {
  866. struct vmount *vm;
  867. vm = get_vmount(&(statfs_buf.f_fsid));
  868. strcpy (statfs_buf.f_fname, vmt2dataptr (vm, VMT_STUB));
  869. }
  870. #endif /* _AIX */
  871. if (bytes != 0)
  872. {
  873. // The following code became redundant as the writeMailBox method now
  874. // checks for No space from the SafeWritev call.
  875. #if 0
  876. // CDExc20314
  877. // This check does not work (HP, IBM, Sun return -1; DEC returns 0)
  878. // Since the calling code has already created the file and is only
  879. // check that there is enough space, we don't need to check that
  880. // there are enough inodes.
  881. //
  882. // special case where the filesystem is out of inodes.
  883. if(statvfs_buf.f_ffree < 10) fserror = TRUE;
  884. #endif
  885. if (! fserror)
  886. {
  887. req_space = (size_t) ((bytes > stat_buf.st_size) ?
  888. (bytes-stat_buf.st_size) :
  889. 0);
  890. #if !defined(__linux__)
  891. if ( (statvfs_buf.f_bfree*statvfs_buf.f_bsize) >
  892. (req_space + statvfs_buf.f_bsize) )
  893. return 1;
  894. else
  895. fserror = TRUE;
  896. #endif
  897. }
  898. }
  899. else fserror = TRUE;
  900. if (fserror)
  901. {
  902. #ifdef _AIX
  903. *fsname = (char*) calloc(1, strlen(statfs_buf.f_fname)+1);
  904. strcpy (*fsname, statfs_buf.f_fname);
  905. #else
  906. *fsname = (char*) calloc(1, strlen(file_path)+1);
  907. strcpy (*fsname, file_path);
  908. #endif
  909. return 0;
  910. }
  911. return 1;
  912. }