Skip to content

Did that close succeed.

July 20, 2005

Many years ago I got involved with a customer call where the customer’s application was loosing data when writing to an NFS file system. The solution was that the application needed to check the return status of close(2) as the NFS client could buffer data and only had to flush it to the server when the last close(2) happened and therefore that was when it would return the error. Typically this was due to the file system being full, close(2) would return -1 and set errno to be ENOSPC.

It was inevitable that I would one day be caught out by exactly the same issue. A test program I have has the following code:

 static ssize_t do_pwrite(int fildes, void *bufp, size_t bufs, off_t off) {         int fd;         ssize_t ret;         int err;          if ((fd = fd_open_blk(fildes, O_WRONLY|O_CREAT|fs_opts.wflags, off)) ==             -1) {                 return (-1);         }         ret = pwrite(fd, (char *)bufp, bufs, fd_off2realoff(fildes, off));         err = errno;         close(fd);         errno = err;         return (ret); } 

Oh deary deary me. That needs to be:

 static ssize_t do_pwrite(int fildes, void *bufp, size_t bufs, off_t off) {         int fd;         ssize_t ret;         int err;          if ((fd = fd_open_blk(fildes, O_WRONLY|O_CREAT|fs_opts.wflags, off)) ==             -1) {                 return (-1);         }         ret = pwrite(fd, (char *)bufp, bufs, fd_off2realoff(fildes, off));         err = errno;         if (close(fd)) < 0) {                 ret = -1;         } else {                 errno = err;         }         return (ret); } 

Advertisements

From → Solaris

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: