Technical article 100833

The library routine ftp_putfile() does not handle file descriptors properly.

OpenServer 5.0.0

Written · Last revised

Problem

The ftp_putfile(SLIB) library routine destroys file descriptor 0 upon the first call, and abandons file descriptors upon subsequent calls.

Cause

There is a flaw in the ftp_putfile(SLIB) routine. The problem is illustrated with the following two code stubs:

--------------------------- cut here --------------------------------------
/* Compile with "cc testfile -lftp"  */
# include <stdio.h>
# include <errno.h>
# include <net/libftp.h>
# define USAGE "usage: %s remote-host username password rmtdir\n"
FTPINFO ftpinfo;
FTPINFO ftp_check(int argc, char *argv[]);
main(int argc, char *argv[]) {
char buf[256];
int retval, i;
  ftpinfo=ftp_check(argc, argv);
/* Error handling */
/**************************************************************/

/* This illustrates how ftp_putfile(SLIB) destroys file * descriptor 0 upon the first call. */

printf("before ftp_putfile - press <ret> to continue\n"); retval = read(0, buf, 24); /* This read works ok */ printf("read retval = %d, errno = %d\n", retval, errno); if(ftp_putfile(&ftpinfo, "2kfile", "2kfile") < 0) { printf("ftp_putfile failed: (%d) %s\n", ftpinfo.reply, ftpinfo.ftp_msg); if(ftpinfo.sockfd >= 0) close(ftpinfo.sockfd); exit(1); } printf("after ftp_putfile: "); retval = read(0, buf, 24); /* This read returns an error 111 (Socket not connected) */ printf("retval = %d, errno = %d (%s)\n", retval, errno, (errno));

/**************************************************************/

/* Here we loop through four more times, illustrating how * ftp_putfile(SLIB) abandons an open socket file descriptor * upon each call. Use crash(ADM) or netstat(TC) to see * open sockets. */

for(i = 1; i < 5; i++) {
           if(ftp_putfile(&ftpinfo, "2kfile", "2kfile") < 0) {
                   printf("ftp_putfile failed: (%d) %s\n",
                           ftpinfo.reply, ftpinfo.ftp_msg);
           if(ftpinfo.sockfd >= 0)
                   close(ftpinfo.sockfd);
           exit(1);
           }
   }
/**************************************************************/
if(ftp_bye(&ftpinfo) < 0) {
        printf("ftp_bye failed: (%d) %s\n", ftpinfo.reply,
                ftpinfo.ftp_msg);
        if(ftpinfo.sockfd >= 0)
                close(ftpinfo.sockfd);
        exit(1);
}
pause();
}
FTPINFO ftp_check(int argc, char *argv[]){
        FTPINFO ftpinfo_local;
      /* Error handling */
      if(argc != 5) {
              printf(USAGE, argv[0]);
              exit(1);
      }
      if(ftp_login(&ftpinfo_local, argv[1], argv[2],
                      argv[3], (char *)0) < 0) {
              printf("ftp_login failed: (%d) %s\n",
                      ftpinfo_local.reply, ftpinfo_local.ftp_msg);
              if(ftpinfo_local.sockfd >= 0)
                      close(ftpinfo_local.sockfd);
              exit(1);
      }
      if(ftp_chdir(&ftpinfo_local, argv[4]) < 0) {
              printf("ftp_chdir failed: (%d) %s\n",
                      ftpinfo_local.reply, ftpinfo_local.ftp_msg);
              if(ftpinfo_local.sockfd >= 0)
                      close(ftpinfo_local.sockfd);
              exit(1);
      }
      if(ftp_binary(&ftpinfo_local) < 0) {
              printf("ftp_binary failed: (%d) %s\n",
                      ftpinfo_local.reply, ftpinfo_local.ftp_msg);
              if(ftpinfo_local.sockfd >= 0)
                      close(ftpinfo_local.sockfd);
              exit(1);
      }
      return (ftpinfo_local);
}
--------------------------- cut here --------------------------------------

Solution

This has been reported to SCO Engineering.

From the Xinuos technical article database, record 100833. Reproduced with permission. The original is at www.sco.com/ta/100833, which is reachable but blocked from search engines. Xinuos marks this article against a retired product line. That refers to the product’s lifecycle, not to whether the advice still works — plenty of it still applies to running systems.

Next step

Need this fixed rather than explained?

Tell us the release, what the system runs and what it is doing. We will reply by email with what we would do next.

  • You deal with the people who do the work
  • No obligation, and no upgrade required before we will help
  • We tell you plainly if something is not worth doing

Send an enquiry

We reply by email. If a server is down, say so in the first line. You can also write to help@scoexpert.com.