+int robust_open(const char * pathname, int flags, mode_t mode) {
+ int result = -1;
+ int e_busy_count = 0;
+
+ for (;;) {
+ if (flags & O_CREAT) {
+ result = open(pathname, flags, mode);
+ } else {
+ result = open(pathname, flags);
+ }
+
+ if (result < 0) {
+#ifdef EBUSY
+ /* EBUSY is a tricky one; sometimes it is synonymous with
+ EINTR, but sometimes it means the device is open
+ elsewhere (e.g., with a tape drive on Linux). We take
+ the middle path and retry, but with limited
+ patience. */
+ if (errno == EBUSY && e_busy_count < 10) {
+ e_busy_count ++;
+ continue;
+ } else
+#endif
+ if (0
+ /* Always retry on EINTR; if the caller did
+ not specify non-blocking mode, then also retry on
+ EAGAIN or EWOULDBLOCK. */
+#ifdef EINTR
+ || errno == EINTR
+#endif
+ || ( 1
+#ifdef O_NONBLOCK
+ && !(flags & O_NONBLOCK)
+#endif
+ && ( 0
+#ifdef EAGAIN
+ || errno == EAGAIN
+#endif
+#ifdef EWOULDBLOCK
+ || errno == EWOULDBLOCK
+#endif
+ ) ) ) {
+ /* Try again */
+ continue;
+ } else {
+ /* Failure. */
+ return result;
+ }
+ } else {
+ break;
+ }
+ }
+
+#ifdef F_SETFD
+ if (result >= 0) {
+ fcntl(result, F_SETFD, 1); /* Throw away result. */
+ }
+#endif
+
+ return result;
+}
+
+int robust_close(int fd) {
+ for (;;) {
+ int result;
+
+ result = close(fd);
+ if (result != 0 && (0
+#ifdef EINTR
+ || errno == EINTR
+#endif
+#ifdef EBUSY
+ || errno == EBUSY
+#endif
+#ifdef EAGAIN
+ || errno == EAGAIN
+#endif
+#ifdef EWOULDBLOCK
+ || errno == EWOULDBLOCK
+#endif
+ )) {
+ continue;
+ } else {
+ return result;
+ }
+ }
+}
+
+uid_t
+get_client_uid(void)
+{
+ static uid_t client_uid = (uid_t) -1;
+ struct passwd *pwent;
+
+ if(client_uid == (uid_t) -1 && (pwent = getpwnam(CLIENT_LOGIN)) != NULL) {
+ client_uid = pwent->pw_uid;
+ endpwent();
+ }
+
+ return client_uid;
+}
+
+gid_t
+get_client_gid(void)
+{
+ static gid_t client_gid = (gid_t) -1;
+ struct passwd *pwent;
+
+ if(client_gid == (gid_t) -1 && (pwent = getpwnam(CLIENT_LOGIN)) != NULL) {
+ client_gid = pwent->pw_gid;
+ endpwent();
+ }
+
+ return client_gid;
+}
+
+char *
+get_original_cwd(void)
+{
+ if (original_cwd == NULL) {
+ original_cwd = g_get_current_dir();
+ }
+
+ return original_cwd;
+}
+