Changeset 9016

Show
Ignore:
Timestamp:
08/19/11 13:24:28 (21 months ago)
Author:
walt
Message:

Completed initial debugging of all interfaces.

Location:
branches/Orange-Branch/src/client/usrint
Files:
13 modified

Legend:

Unmodified
Added
Removed
  • branches/Orange-Branch/src/client/usrint/Makefile

    r8957 r9016  
    44LIBDIR= 
    55 
    6 CFLAGS=-g -O0 $(INCDIR) $(LIBDIR) 
     6#CFLAGS=-g -O0 $(INCDIR) $(LIBDIR) -Wall -Wstrict-prototypes -D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE 
     7CFLAGS=-g -O0 $(INCDIR) $(LIBDIR) -Wall -Wstrict-prototypes -D_LARGEFILE64_SOURCE 
    78 
    8 OBJS=iocommon.o openfile-util.o posix-pvfs.o posix.o stdio.o request.o socket.o 
     9OBJS=iocommon.o openfile-util.o posix-pvfs.o posix.o stdio.o request.o socket.o shim.o 
    910 
    10 INCS=usrint.h iocommon.h posix-pvfs.h openfile-util.h posix-ops.h 
     11INCS=usrint.h iocommon.h posix-pvfs.h openfile-util.h posix-ops.h stdio-ops.h 
    1112 
    1213all: $(OBJS) 
  • branches/Orange-Branch/src/client/usrint/iocommon.c

    r8975 r9016  
    1010 *  PVFS2 user interface routines - low level calls to system interface 
    1111 */ 
    12 #include <usrint.h> 
     12#include "usrint.h" 
    1313#include <linux/dirent.h> 
    14 #include <posix-ops.h> 
    15 #include <openfile-util.h> 
    16 #include <iocommon.h> 
     14#include "posix-ops.h" 
     15#include "openfile-util.h" 
     16#include "iocommon.h" 
    1717 
    1818/* Functions in this file generally define a label errorout 
     
    8989 
    9090    pvfs_sys_init(); 
     91    if (!pd || pd->is_in_use != PVFS_FS) 
     92    { 
     93        errno = EBADF; 
     94        return -1; 
     95    } 
    9196    iocommon_cred(&credentials); 
    9297    rc = PVFS_sys_flush(pd->pvfs_ref, credentials, PVFS_HINT_NULL); 
     
    314319    if (dist) 
    315320    { 
    316         PINT_dist_free(dist); 
     321        PVFS_sys_dist_free(dist); 
    317322    } 
    318323    return rc; 
     
    338343    PVFS_object_ref file_ref; 
    339344    PVFS_object_ref parent_ref; 
    340     int fs_id = 0; 
    341345    pvfs_descriptor *pd = NULL; /* invalid pd until file is opened */ 
    342346    PVFS_sysresp_getattr attributes_resp; 
     
    416420            rc = (*glibc_ops.open)(error_path, flags & 01777777, mode); 
    417421            IOCOMMON_RETURN_ERR(rc); 
    418             pd = pvfs_alloc_descriptor(&pvfs_ops); 
     422            pd = pvfs_alloc_descriptor(&pvfs_ops, -1); 
    419423            pd->is_in_use = PVFS_FS;    /* indicate fd is valid! */ 
    420424            pd->true_fd = rc; 
     
    463467    /* Set the file information */ 
    464468    /* create fd object */ 
    465     pd = pvfs_alloc_descriptor(&pvfs_ops); 
     469    pd = pvfs_alloc_descriptor(&pvfs_ops, -1); 
     470    if (!pd) 
     471    { 
     472        rc = -1; 
     473        goto errorout; 
     474    } 
    466475    pd->pvfs_ref = file_ref; 
    467476    pd->flags = flags;           /* open flags */ 
     
    565574    int orig_errno = errno; 
    566575 
     576    if (!pd || pd->is_in_use != PVFS_FS) 
     577    { 
     578        errno = EBADF; 
     579        return -1; 
     580    } 
    567581    switch(whence) 
    568582    { 
     
    838852    PVFS_sysresp_io io_resp; 
    839853 
     854    if (!pd || pd->is_in_use != PVFS_FS) 
     855    { 
     856        errno = EBADF; 
     857        return -1; 
     858    } 
    840859    /* Initialize */ 
    841860    memset(&io_resp, 0, sizeof(io_resp)); 
    842861 
    843     if (!pd) 
    844     { 
    845         errno = EBADF; 
    846         return -1; 
    847     } 
    848  
    849862    //Ensure descriptor is used for the correct type of access 
    850     if (which==PVFS_IO_READ && (O_WRONLY == (pd->flags & O_ACCMODE)) || 
    851         which==PVFS_IO_WRITE && (O_RDONLY == (pd->flags & O_ACCMODE))) 
     863    if ((which==PVFS_IO_READ && (O_WRONLY == (pd->flags & O_ACCMODE))) || 
     864        (which==PVFS_IO_WRITE && (O_RDONLY == (pd->flags & O_ACCMODE)))) 
    852865    { 
    853866        errno = EBADF; 
     
    898911    PVFS_size req_size; 
    899912 
     913    if (!pd || pd->is_in_use != PVFS_FS) 
     914    { 
     915        errno = EBADF; 
     916        return -1; 
     917    } 
    900918    //Ensure descriptor is used for the correct type of access 
    901     if (which==PVFS_IO_READ && (O_WRONLY == (pd->flags & O_ACCMODE)) || 
    902         which==PVFS_IO_WRITE && (O_RDONLY == (pd->flags & O_ACCMODE))) 
     919    if ((which==PVFS_IO_READ && (O_WRONLY == (pd->flags & O_ACCMODE))) || 
     920        (which==PVFS_IO_WRITE && (O_RDONLY == (pd->flags & O_ACCMODE)))) 
    903921    { 
    904922        errno = EBADF; 
     
    9881006    PVFS_sys_attr attr; 
    9891007 
     1008    if (!pd || pd->is_in_use != PVFS_FS) 
     1009    { 
     1010        errno = EBADF; 
     1011        return -1; 
     1012    } 
    9901013    /* Initialize */ 
    9911014    memset(&attr, 0, sizeof(attr)); 
     
    10321055{ 
    10331056    int rc = 0; 
    1034     int orig_errno = errno; 
    10351057    PVFS_sys_attr attr; 
    10361058 
     1059    if (!pd || pd->is_in_use != PVFS_FS) 
     1060    { 
     1061        errno = EBADF; 
     1062        return -1; 
     1063    } 
    10371064    /* Initialize */ 
    10381065    memset(&attr, 0, sizeof(attr)); 
     
    10751102{ 
    10761103    int rc = 0; 
    1077     int orig_errno = errno; 
    10781104    PVFS_sys_attr attr; 
    10791105 
     1106    if (!pd || pd->is_in_use != PVFS_FS) 
     1107    { 
     1108        errno = EBADF; 
     1109        return -1; 
     1110    } 
    10801111    /* Initialize */ 
    10811112    memset(&attr, 0, sizeof(attr)); 
     
    10991130{ 
    11001131    int rc = 0; 
    1101     int orig_errno = errno; 
    11021132    PVFS_sys_attr attr; 
    11031133 
     1134    if (!pd || pd->is_in_use != PVFS_FS) 
     1135    { 
     1136        errno = EBADF; 
     1137        return -1; 
     1138    } 
    11041139    /* Initialize */ 
    11051140    memset(&attr, 0, sizeof(attr)); 
     
    11121147} 
    11131148 
    1114 iocommon_make_directory(const char *pvfs_path, 
     1149int iocommon_make_directory(const char *pvfs_path, 
    11151150                        const int mode, 
    11161151                        PVFS_object_ref *pdir) 
     
    11871222int iocommon_readlink(pvfs_descriptor *pd, char *buf, int size) 
    11881223{ 
    1189     int                  rc = 0; 
    1190     int orig_errno = errno; 
    1191     PVFS_sys_attr        attr; 
    1192  
     1224    int rc = 0; 
     1225    PVFS_sys_attr attr; 
     1226 
     1227    if (!pd || pd->is_in_use != PVFS_FS) 
     1228    { 
     1229        errno = EBADF; 
     1230        return -1; 
     1231    } 
    11931232    /* Initialize any variables */ 
    11941233    memset(&attr, 0, sizeof(attr)); 
     
    12991338    int bytes = 0, i = 0; 
    13001339 
     1340    if (!pd || pd->is_in_use != PVFS_FS) 
     1341    { 
     1342        errno = EBADF; 
     1343        return -1; 
     1344    } 
    13011345    if (pd->token == PVFS_READDIR_END) 
    13021346    { 
     
    13571401    int bytes = 0, i = 0; 
    13581402 
     1403    if (!pd || pd->is_in_use != PVFS_FS) 
     1404    { 
     1405        errno = EBADF; 
     1406        return -1; 
     1407    } 
    13591408    if (pd->token == PVFS_READDIR_END) 
    13601409    { 
     
    15631612    PVFS_sysresp_statfs statfs_resp; 
    15641613     
     1614    if (!pd || pd->is_in_use != PVFS_FS) 
     1615    { 
     1616        errno = EBADF; 
     1617        return -1; 
     1618    } 
    15651619    /* Initialize the system interface for this process */ 
    15661620    pvfs_sys_init(); 
     
    15981652    PVFS_sysresp_statfs statfs_resp; 
    15991653     
     1654    if (!pd || pd->is_in_use != PVFS_FS) 
     1655    { 
     1656        errno = EBADF; 
     1657        return -1; 
     1658    } 
    16001659    /* Initialize the system interface for this process */ 
    16011660    pvfs_sys_init(); 
     
    16261685 
    16271686int iocommon_sendfile(int sockfd, pvfs_descriptor *pd, 
    1628                       off64_t offset, size_t count) 
    1629 { 
    1630     int rc = 0, bytes_read; 
    1631     int orig_errno = errno; 
     1687                      off64_t *offset, size_t count) 
     1688{ 
     1689    int rc = 0, bytes_read = 0; 
    16321690    PVFS_Request mem_req, file_req; 
    1633     PVFS_credentials *credentials; 
    16341691    char *buffer; 
    1635     int buffer_size; 
    1636  
     1692    int buffer_size = (8*1024*1024); 
     1693 
     1694    if (!pd || pd->is_in_use != PVFS_FS) 
     1695    { 
     1696        errno = EBADF; 
     1697        return -1; 
     1698    } 
    16371699    buffer = (char *)malloc(buffer_size); 
    16381700 
     
    16401702    file_req = PVFS_BYTE; 
    16411703 
    1642     rc = iocommon_readorwrite(PVFS_IO_READ, pd, offset + bytes_read, 
     1704    rc = iocommon_readorwrite(PVFS_IO_READ, pd, *offset + bytes_read, 
    16431705                              buffer, mem_req, file_req); 
    16441706    while(rc > 0) 
     
    16551717            break; 
    16561718        } 
    1657         rc = iocommon_readorwrite(PVFS_IO_READ, pd, offset + bytes_read, 
     1719        rc = iocommon_readorwrite(PVFS_IO_READ, pd, *offset + bytes_read, 
    16581720                                  buffer, mem_req, file_req); 
    16591721    }   
     
    16661728    else 
    16671729    { 
     1730        *offset += bytes_read; 
    16681731        return bytes_read; 
    16691732    } 
     
    16771740 */ 
    16781741int iocommon_geteattr(pvfs_descriptor *pd, 
    1679                       char *key_p, 
     1742                      const char *key_p, 
    16801743                      void *val_p, 
    16811744                      int size) 
     
    16861749    PVFS_ds_keyval key, val; 
    16871750 
     1751    if (!pd || pd->is_in_use != PVFS_FS) 
     1752    { 
     1753        errno = EBADF; 
     1754        return -1; 
     1755    } 
    16881756    /* Initialize */ 
    16891757    memset(&key, 0, sizeof(key)); 
     
    16931761    iocommon_cred(&credentials); 
    16941762 
    1695     key.buffer = key_p; 
     1763    key.buffer = (char *)key_p; 
    16961764    key.buffer_sz = strlen(key_p) + 1; 
    16971765    val.buffer = val_p; 
     
    17211789 */ 
    17221790int iocommon_seteattr(pvfs_descriptor *pd, 
    1723                       char *key_p, 
    1724                       void *val_p, 
     1791                      const char *key_p, 
     1792                      const void *val_p, 
    17251793                      int size, 
    17261794                      int flag) 
     
    17321800    PVFS_ds_keyval key, val; 
    17331801 
     1802    if (!pd || pd->is_in_use != PVFS_FS) 
     1803    { 
     1804        errno = EBADF; 
     1805        return -1; 
     1806    } 
    17341807    /* Initialize */ 
    17351808    memset(&key, 0, sizeof(key)); 
     
    17391812    iocommon_cred(&credentials); 
    17401813 
    1741     key.buffer = key_p; 
     1814    key.buffer = (char *)key_p; 
    17421815    key.buffer_sz = strlen(key_p) + 1; 
    1743     val.buffer = val_p; 
     1816    val.buffer = (void *)val_p; 
    17441817    val.buffer_sz = size; 
    17451818 
     
    17731846 */ 
    17741847int iocommon_deleattr(pvfs_descriptor *pd, 
    1775                       char *key_p) 
    1776 { 
    1777     int rc = 0; 
    1778     int pvfs_flag = 0; 
     1848                      const char *key_p) 
     1849{ 
     1850    int rc = 0; 
    17791851    int orig_errno = errno; 
    17801852    PVFS_credentials *credentials; 
    17811853    PVFS_ds_keyval key; 
    17821854 
     1855    if (!pd || pd->is_in_use != PVFS_FS) 
     1856    { 
     1857        errno = EBADF; 
     1858        return -1; 
     1859    } 
    17831860    /* Initialize */ 
    17841861    memset(&key, 0, sizeof(key)); 
     
    17871864    iocommon_cred(&credentials); 
    17881865 
    1789     key.buffer = key_p; 
     1866    key.buffer = (char *)key_p; 
    17901867    key.buffer_sz = strlen(key_p) + 1; 
    17911868 
     
    18241901    PVFS_sysresp_listeattr listeattr_resp; 
    18251902 
     1903    if (!pd || pd->is_in_use != PVFS_FS) 
     1904    { 
     1905        errno = EBADF; 
     1906        return -1; 
     1907    } 
    18261908    /* Initialize */ 
    18271909    memset(&listeattr_resp, 0, sizeof(listeattr_resp)); 
  • branches/Orange-Branch/src/client/usrint/iocommon.h

    r8975 r9016  
    3838 
    3939/* Perform PVFS initialization if not already finished */ 
    40 void iocommon_ensure_init(); 
     40void iocommon_ensure_init(void); 
    4141 
    4242void iocommon_cred(PVFS_credentials **credentials); 
     
    145145int iocommon_stat64(pvfs_descriptor *pd, struct stat64 *buf, uint32_t mask); 
    146146 
     147int iocommon_statfs(pvfs_descriptor *pd, struct statfs *buf); 
     148 
     149int iocommon_statfs64(pvfs_descriptor *pd, struct statfs64 *buf); 
     150 
     151int iocommon_seteattr(pvfs_descriptor *pd, const char *key, const void *val, int size, int flag); 
     152 
     153int iocommon_geteattr(pvfs_descriptor *pd, const char *key, void *val, int size); 
     154 
     155int iocommon_listeattr(pvfs_descriptor *pd, char *list, int size, int *numkeys); 
     156 
     157int iocommon_deleattr(pvfs_descriptor *pd, const char * key); 
     158 
    147159int iocommon_chown(pvfs_descriptor *pd, uid_t owner, gid_t group); 
    148160 
     
    163175                      unsigned int count); 
    164176 
     177int iocommon_getdents64(pvfs_descriptor *pd, 
     178                      struct dirent64 *dirp, 
     179                      unsigned int count); 
     180 
    165181int iocommon_access(const char *pvfs_path, 
    166182                    int mode, 
     
    168184                    PVFS_object_ref *pdir); 
    169185 
     186int iocommon_sendfile(int sockfd, 
     187                      pvfs_descriptor *pd, 
     188                      off64_t *offset, 
     189                      size_t count); 
    170190/* 
    171191 * Local variables: 
  • branches/Orange-Branch/src/client/usrint/openfile-util.c

    r8975 r9016  
    1212#include <usrint.h> 
    1313#include <linux/dirent.h> 
     14#include <sys/syscall.h> 
    1415#include <posix-ops.h> 
    1516#include <openfile-util.h> 
     17#include <posix-pvfs.h> 
     18 
     19static struct glibc_redirect_s 
     20{ 
     21    int (*stat)(int ver, const char *path, struct stat *buf); 
     22    int (*stat64)(int ver, const char *path, struct stat64 *buf); 
     23    int (*fstat)(int ver, int fd, struct stat *buf); 
     24    int (*fstat64)(int ver, int fd, struct stat64 *buf); 
     25    int (*fstatat)(int ver, int fd, const char *path, struct stat *buf, int flag); 
     26    int (*fstatat64)(int ver, int fd, const char *path, struct stat64 *buf, int flag); 
     27    int (*lstat)(int ver, const char *path, struct stat *buf); 
     28    int (*lstat64)(int ver, const char *path, struct stat64 *buf); 
     29    int (*mknod)(int ver, const char *path, mode_t mode, dev_t dev); 
     30    int (*mknodat)(int ver, int dirfd, const char *path, mode_t mode, dev_t dev); 
     31} glibc_redirect; 
    1632 
    1733#define PREALLOC 3 
     34static char logfilepath[25]; 
     35static int logfile; 
     36static int pvfs_initializing_flag = 0; 
    1837static int descriptor_table_count = 0;  
    1938static int descriptor_table_size = 0;  
    20 static int next_descriptor = 0;  
    2139static pvfs_descriptor **descriptor_table;  
    2240static char rstate[256];  /* used for random number generation */ 
     
    6886    .is_in_use = PVFS_FS 
    6987}; 
     88 
     89static int my_glibc_stat(const char *path, struct stat *buf) 
     90{ 
     91    return glibc_redirect.stat(3, path, buf); 
     92} 
     93 
     94static int my_glibc_stat64(const char *path, struct stat64 *buf) 
     95{ 
     96    return glibc_redirect.stat64(3, path, buf); 
     97} 
     98 
     99static int my_glibc_fstat(int fd, struct stat *buf) 
     100{ 
     101    return glibc_redirect.fstat(3, fd, buf); 
     102} 
     103 
     104static int my_glibc_fstat64(int fd, struct stat64 *buf) 
     105{ 
     106    return glibc_redirect.fstat64(3, fd, buf); 
     107} 
     108 
     109static int my_glibc_fstatat(int fd, const char *path, struct stat *buf, int flag) 
     110{ 
     111    return glibc_redirect.fstatat(3, fd, path, buf, flag); 
     112} 
     113 
     114static int my_glibc_fstatat64(int fd, const char *path, struct stat64 *buf, int flag) 
     115{ 
     116    return glibc_redirect.fstatat64(3, fd, path, buf, flag); 
     117} 
     118 
     119static int my_glibc_lstat(const char *path, struct stat *buf) 
     120{ 
     121    return glibc_redirect.lstat(3, path, buf); 
     122} 
     123 
     124static int my_glibc_lstat64(const char *path, struct stat64 *buf) 
     125{ 
     126    return glibc_redirect.lstat64(3, path, buf); 
     127} 
     128 
     129static int my_glibc_mknod(const char *path, mode_t mode, dev_t dev) 
     130{ 
     131    return glibc_redirect.mknod(1, path, mode, dev); 
     132} 
     133 
     134static int my_glibc_mknodat(int dirfd, const char *path, mode_t mode, dev_t dev) 
     135{ 
     136    return glibc_redirect.mknodat(1, dirfd, path, mode, dev); 
     137} 
     138 
     139static int my_glibc_getdents(u_int fd, struct dirent *dirp, u_int count) 
     140{ 
     141    return syscall(SYS_getdents, fd, dirp, count); 
     142} 
     143 
     144static int my_glibc_getdents64(u_int fd, struct dirent64 *dirp, u_int count) 
     145{ 
     146    return syscall(SYS_getdents64, fd, dirp, count); 
     147} 
     148 
     149static int my_glibc_fadvise64(int fd, off64_t offset, off64_t len, int advice) 
     150{ 
     151    return syscall(SYS_fadvise64, fd, offset, len, advice); 
     152} 
     153 
     154static int my_glibc_fadvise(int fd, off_t offset, off_t len, int advice) 
     155{ 
     156    return my_glibc_fadvise64(fd, (off64_t)offset, (off64_t)len, advice); 
     157} 
     158 
     159static int my_glibc_flush(int fd) 
     160{ 
     161    errno = ENOSYS; 
     162    return -1; 
     163} 
     164  
     165static int my_glibc_readdir(u_int fd, struct dirent *dirp, u_int count) 
     166{ 
     167    return syscall(SYS_readdir, fd, dirp, count); 
     168} 
    70169 
    71170void load_glibc(void) 
     
    97196    glibc_ops.fallocate = dlsym(RTLD_NEXT, "posix_fallocate"); 
    98197    glibc_ops.close = dlsym(RTLD_NEXT, "close"); 
    99     glibc_ops.flush = dlsym(RTLD_NEXT, "flush"); 
    100     glibc_ops.stat = dlsym(RTLD_NEXT, "stat"); 
    101     glibc_ops.stat64 = dlsym(RTLD_NEXT, "stat64"); 
    102     glibc_ops.fstat = dlsym(RTLD_NEXT, "fstat"); 
    103     glibc_ops.fstat64 = dlsym(RTLD_NEXT, "fstat64"); 
    104     glibc_ops.fstatat = dlsym(RTLD_NEXT, "fstatat"); 
    105     glibc_ops.fstatat64 = dlsym(RTLD_NEXT, "fstatat64"); 
    106     glibc_ops.lstat = dlsym(RTLD_NEXT, "lstat"); 
    107     glibc_ops.lstat64 = dlsym(RTLD_NEXT, "lstat64"); 
     198    glibc_ops.flush = my_glibc_flush; 
     199    glibc_ops.stat = my_glibc_stat; 
     200    glibc_redirect.stat = dlsym(RTLD_NEXT, "__xstat"); 
     201    glibc_ops.stat64 = my_glibc_stat64; 
     202    glibc_redirect.stat64 = dlsym(RTLD_NEXT, "__xstat64"); 
     203    glibc_ops.fstat = my_glibc_fstat; 
     204    glibc_redirect.fstat = dlsym(RTLD_NEXT, "__fxstat"); 
     205    glibc_ops.fstat64 = my_glibc_fstat64; 
     206    glibc_redirect.fstat64 = dlsym(RTLD_NEXT, "__fxstat64"); 
     207    glibc_ops.fstatat = my_glibc_fstatat; 
     208    glibc_redirect.fstatat = dlsym(RTLD_NEXT, "__fxstatat"); 
     209    glibc_ops.fstatat64 = my_glibc_fstatat64; 
     210    glibc_redirect.fstatat64 = dlsym(RTLD_NEXT, "__fxstatat64"); 
     211    glibc_ops.lstat = my_glibc_lstat; 
     212    glibc_redirect.lstat = dlsym(RTLD_NEXT, "__lxstat"); 
     213    glibc_ops.lstat64 = my_glibc_lstat64; 
     214    glibc_redirect.lstat64 = dlsym(RTLD_NEXT, "__lxstat64"); 
    108215    glibc_ops.dup = dlsym(RTLD_NEXT, "dup"); 
    109216    glibc_ops.dup2 = dlsym(RTLD_NEXT, "dup2"); 
     
    124231    glibc_ops.link = dlsym(RTLD_NEXT, "link"); 
    125232    glibc_ops.linkat = dlsym(RTLD_NEXT, "linkat"); 
    126     glibc_ops.readdir = dlsym(RTLD_NEXT, "readdir"); 
    127     glibc_ops.getdents = dlsym(RTLD_NEXT, "getdents"); 
    128     glibc_ops.getdents64 = dlsym(RTLD_NEXT, "getdents64"); 
     233    glibc_ops.readdir = my_glibc_readdir; 
     234    glibc_ops.getdents = my_glibc_getdents; 
     235    glibc_ops.getdents64 = my_glibc_getdents64; 
    129236    glibc_ops.access = dlsym(RTLD_NEXT, "access"); 
    130237    glibc_ops.faccessat = dlsym(RTLD_NEXT, "faccessat"); 
     
    134241    glibc_ops.fsync = dlsym(RTLD_NEXT, "fsync"); 
    135242    glibc_ops.fdatasync = dlsym(RTLD_NEXT, "fdatasync"); 
    136     glibc_ops.fadvise = dlsym(RTLD_NEXT, "fadvise"); 
    137     glibc_ops.fadvise64 = dlsym(RTLD_NEXT, "fadvise64"); 
     243    glibc_ops.fadvise = my_glibc_fadvise; 
     244    glibc_ops.fadvise64 = my_glibc_fadvise64; 
    138245    glibc_ops.statfs = dlsym(RTLD_NEXT, "statfs"); 
    139246    glibc_ops.statfs64 = dlsym(RTLD_NEXT, "statfs64"); 
    140247    glibc_ops.fstatfs = dlsym(RTLD_NEXT, "fstatfs"); 
    141248    glibc_ops.fstatfs64 = dlsym(RTLD_NEXT, "fstatfs64"); 
    142     glibc_ops.mknod = dlsym(RTLD_NEXT, "mknod"); 
    143     glibc_ops.mknodat = dlsym(RTLD_NEXT, "mknodat"); 
     249    glibc_ops.mknod = my_glibc_mknod; 
     250    glibc_redirect.mknod = dlsym(RTLD_NEXT, "__xmknod"); 
     251    glibc_ops.mknodat = my_glibc_mknodat; 
     252    glibc_redirect.mknodat = dlsym(RTLD_NEXT, "__xmknodat"); 
    144253    glibc_ops.sendfile = dlsym(RTLD_NEXT, "sendfile"); 
    145254    glibc_ops.sendfile64 = dlsym(RTLD_NEXT, "sendfile64"); 
     
    181290    glibc_ops.socketpair = dlsym(RTLD_NEXT, "socketpair"); 
    182291    glibc_ops.pipe = dlsym(RTLD_NEXT, "pipe"); 
     292    glibc_ops.umask = dlsym(RTLD_NEXT, "umask"); 
     293    glibc_ops.getumask = dlsym(RTLD_NEXT, "getumask"); 
     294    glibc_ops.getdtablesize = dlsym(RTLD_NEXT, "getdtablesize"); 
    183295 
    184296/* PVFS does not implement socket ops */ 
     
    208320    pvfs_ops.socketpair = dlsym(RTLD_NEXT, "socketpair"); 
    209321    pvfs_ops.pipe = dlsym(RTLD_NEXT, "pipe"); 
    210     glibc_ops.umask = dlsym(RTLD_NEXT, "umask"); 
    211     glibc_ops.getumask = dlsym(RTLD_NEXT, "getumask"); 
    212     glibc_ops.getdtablesize = dlsym(RTLD_NEXT, "getdtablesize"); 
     322} 
     323 
     324static void usrint_cleanup(void) 
     325{ 
     326    /* later check for an error that might want us */ 
     327    /* to keep this - for now it is empty */ 
     328    glibc_ops.unlink(logfilepath); 
    213329} 
    214330 
     
    227343    } 
    228344    pvfs_lib_init_flag = 1; /* should only run this once */ 
     345    pvfs_initializing_flag = 1; 
    229346 
    230347    /* this allows system calls to run */ 
    231348    load_glibc(); 
     349    PINT_initrand(); 
     350 
     351    /* if this fails no much we can do about it */ 
     352    atexit(usrint_cleanup); 
    232353 
    233354        rc = getrlimit(RLIMIT_NOFILE, &rl);  
     
    245366    descriptor_table[2] = &pvfs_stderr; 
    246367    descriptor_table_count = PREALLOC; 
    247         next_descriptor = PREALLOC; 
     368 
     369    sprintf(logfilepath, "/tmp/pvfsuid-%05d.log", (int)(getuid())); 
     370    logfile = glibc_ops.open(logfilepath, O_RDWR|O_CREAT, 0600); 
     371    if (logfile < 0) 
     372    { 
     373        perror("failed in pvfs_sys_init"); 
     374        exit(-1); 
     375    } 
    248376 
    249377        /* initalize PVFS */  
     
    255383 
    256384    /* call other initialization routines */ 
    257     PINT_initrand(); 
    258385    //PVFS_perror_gossip_silent();  
     386    pvfs_initializing_flag = 0; 
    259387} 
    260388 
     
    268396 * initialize fsops to the given set 
    269397 */ 
    270  pvfs_descriptor *pvfs_alloc_descriptor(posix_ops *fsops) 
     398 pvfs_descriptor *pvfs_alloc_descriptor(posix_ops *fsops, int fd) 
    271399 { 
    272         int i;  
     400        int newfd, rc, flags;  
    273401    if (fsops == NULL) 
    274402    { 
     
    278406    pvfs_sys_init(); 
    279407 
    280     /* table should be initialized now - check for available slot */ 
    281         if (descriptor_table_count == (descriptor_table_size - PREALLOC)) 
    282         { 
    283         errno = ENOMEM; 
    284                 return NULL; 
    285         } 
    286  
    287    /* find next empty slot in table */ 
    288         for (i = next_descriptor; descriptor_table[i]; 
    289                         i = (i == descriptor_table_size - 1) ? PREALLOC : i + 1); 
    290  
    291    /* found a slot */ 
    292         descriptor_table[i] = malloc(sizeof(pvfs_descriptor)); 
    293         if (descriptor_table[i] == NULL) 
    294         { 
    295                 return NULL; 
    296         } 
    297         next_descriptor = ((i == descriptor_table_size - 1) ? PREALLOC : i + 1); 
     408    if (fd == -1) 
     409    { 
     410        /* PVFS file allocate a real descriptor for it */ 
     411        newfd = glibc_ops.dup(logfile); 
     412    } 
     413    else 
     414    { 
     415        /* opened by glibc, make sure this is a valid fd */ 
     416        newfd = fd; 
     417        rc = fcntl(newfd, F_GETFL, flags); 
     418        if (rc < 0) 
     419        { 
     420            return NULL; 
     421        } 
     422    } 
    298423        descriptor_table_count++; 
     424    descriptor_table[newfd] = 
     425                (pvfs_descriptor *)malloc(sizeof(pvfs_descriptor)); 
     426    if (!descriptor_table[newfd]) 
     427    { 
     428        return NULL; 
     429    } 
    299430 
    300431        /* fill in descriptor */ 
    301         descriptor_table[i]->fd = i; 
    302         descriptor_table[i]->dup_cnt = 1; 
    303         descriptor_table[i]->fsops = fsops; 
    304         descriptor_table[i]->true_fd = i; 
    305         descriptor_table[i]->pvfs_ref.fs_id = 0; 
    306         descriptor_table[i]->pvfs_ref.handle = 0; 
    307         descriptor_table[i]->flags = 0; 
    308         descriptor_table[i]->mode = 0; 
    309         descriptor_table[i]->file_pointer = 0; 
    310         descriptor_table[i]->token = 0; 
    311         descriptor_table[i]->dpath = NULL; 
    312         descriptor_table[i]->is_in_use = PVFS_FS; 
    313  
    314     return descriptor_table[i]; 
     432    /* add reference to chache objects here */ 
     433        descriptor_table[newfd]->fd = newfd; 
     434        descriptor_table[newfd]->dup_cnt = 1; 
     435        descriptor_table[newfd]->fsops = fsops; 
     436        descriptor_table[newfd]->true_fd = newfd; 
     437        descriptor_table[newfd]->pvfs_ref.fs_id = 0; 
     438        descriptor_table[newfd]->pvfs_ref.handle = 0; 
     439        descriptor_table[newfd]->flags = flags; 
     440        descriptor_table[newfd]->mode = 0; 
     441        descriptor_table[newfd]->file_pointer = 0; 
     442        descriptor_table[newfd]->token = 0; 
     443        descriptor_table[newfd]->dpath = NULL; 
     444        descriptor_table[newfd]->is_in_use = PVFS_FS; 
     445 
     446    return descriptor_table[newfd]; 
    315447} 
    316448 
    317449/* 
    318  * Function for dupliating a descriptor - used in dup and dup2 calls 
     450 * Function for duplicating a descriptor - used in dup and dup2 calls 
    319451 */ 
    320452int pvfs_dup_descriptor(int oldfd, int newfd) 
    321453{ 
     454    int rc = 0; 
    322455    if (oldfd < 0 || oldfd >= descriptor_table_size) 
    323456    { 
     
    327460    if (newfd == -1) 
    328461    { 
    329         /* find next empty slot in table */ 
    330         for (newfd = next_descriptor; descriptor_table[newfd]; 
    331             newfd = (newfd == descriptor_table_size-1) ? PREALLOC : newfd++); 
     462        newfd = glibc_ops.dup(logfile); 
     463        if (newfd < 0) 
     464        { 
     465            return newfd; 
     466        } 
    332467    } 
    333468    else 
     
    336471        { 
    337472            /* close old file in new slot */ 
    338             pvfs_close(newfd); 
    339         } 
     473            rc = pvfs_free_descriptor(newfd); 
     474            if (rc < 0) 
     475            { 
     476                return rc; 
     477            } 
     478        } 
     479    } 
     480    rc = glibc_ops.dup2(oldfd, newfd); 
     481    if (rc < 0) 
     482    { 
     483        return rc; 
    340484    } 
    341485    descriptor_table[newfd] = descriptor_table[oldfd]; 
    342486    descriptor_table[newfd]->dup_cnt++; 
    343487        descriptor_table_count++; 
     488    return 0; 
    344489} 
    345490 
     
    351496pvfs_descriptor *pvfs_find_descriptor(int fd) 
    352497{ 
     498    pvfs_descriptor *pd; 
    353499    if (fd < 0 || fd >= descriptor_table_size) 
    354500    { 
     
    356502        return NULL; 
    357503    } 
    358         return descriptor_table[fd]; 
     504    pd = descriptor_table[fd]; 
     505    if (!pd) 
     506    { 
     507        int rc, flags; 
     508        /* see if glibc opened this file without our knowing */ 
     509        rc = glibc_ops.fcntl(fd, F_GETFL, flags); 
     510        if (rc < 0) 
     511        { 
     512            errno = EBADF; 
     513            return NULL; 
     514        } 
     515        pd = (pvfs_descriptor *)malloc(sizeof(pvfs_descriptor)); 
     516            /* fill in descriptor */ 
     517            pd->fd = fd; 
     518            pd->dup_cnt = 1; 
     519            pd->fsops = &glibc_ops; 
     520            pd->true_fd = fd; 
     521            pd->pvfs_ref.fs_id = 0; 
     522            pd->pvfs_ref.handle = 0; 
     523            pd->flags = flags; 
     524            pd->mode = 0; 
     525            pd->file_pointer = 0; 
     526            pd->token = 0; 
     527            pd->dpath = NULL; 
     528            pd->is_in_use = PVFS_FS; 
     529        descriptor_table[fd] = pd; 
     530    } 
     531    else if (pd->is_in_use != PVFS_FS) 
     532    { 
     533        errno = EBADF; 
     534        return NULL; 
     535    } 
     536        return pd; 
    359537} 
    360538 
     
    362540{ 
    363541    pvfs_descriptor *pd; 
    364  
    365     if (fd < 0 || fd >= descriptor_table_size) 
    366     { 
    367         errno = EBADF; 
     542    debug("pvfs_free_descriptor called with %d\n", fd); 
     543 
     544    pd = pvfs_find_descriptor(fd); 
     545    if (pd == NULL) 
     546    { 
    368547        return -1; 
    369548    } 
    370     pd = descriptor_table[fd]; 
    371549 
    372550        /* clear out table entry */ 
    373551        descriptor_table[fd] = NULL; 
     552    glibc_ops.close(fd); 
    374553 
    375554        /* keep up with used descriptors */ 
     
    383562            free(pd->dpath); 
    384563        } 
     564        /* release cache opbjects here */ 
    385565            /* free descriptor - wipe memory first */ 
    386566            memset(pd, 0, sizeof(pvfs_descriptor)); 
     
    388568    } 
    389569 
     570    debug("pvfs_free_descriptor returns %d\n", 0); 
    390571        return 0; 
    391572} 
     
    401582    int cdsz; 
    402583    int psz; 
    403     char *curdir; 
    404     char *newpath; 
     584    char *curdir = NULL; 
     585    char *newpath = NULL; 
    405586 
    406587    if(path[0] != '/') 
     
    410591        do 
    411592        { 
    412             if (i > 1) 
     593            if (i > 1 && curdir) 
    413594            { 
    414595                free(curdir); 
     
    425606        { 
    426607            /* some other error, bail out */ 
    427             free(curdir); 
     608            if (curdir) 
     609            { 
     610                free(curdir); 
     611            } 
    428612            return NULL; 
    429613        } 
     
    450634 */ 
    451635 
     636int is_pvfs_path(const char *path) 
     637{ 
    452638#ifdef PVFS_ASSUME_MOUNT 
    453 int is_pvfs_path(const char *path) 
    454 { 
    455639    struct statfs file_system; 
     640#endif 
     641    int rc = 0; 
     642    PVFS_fs_id fs_id; 
     643    char pvfs_path[256]; 
    456644    char *newpath = NULL ; 
    457645 
     646    if (pvfs_initializing_flag) 
     647    { 
     648        /* we cannot open a PVFS file while 
     649         * initializing PVFS 
     650         */ 
     651        return 0; 
     652    } 
     653 
    458654    pvfs_sys_init(); 
     655#ifdef PVFS_ASSUME_MOUNT 
    459656    memset(&file_system, 0, sizeof(file_system)); 
    460657     
     
    484681        return 0; /* not PVFS assume the kernel can handle it */ 
    485682    } 
    486 } 
     683/***************************************************************/ 
    487684#else 
    488 int is_pvfs_path(const char *path) 
    489 { 
    490     int rc = 0; 
    491     PVFS_fs_id fs_id; 
    492     char pvfs_path[256]; 
    493     char *newpath = NULL; 
    494  
    495     pvfs_sys_init(); 
     685/***************************************************************/ 
    496686    newpath = pvfs_qualify_path(path); 
    497687    rc = PVFS_util_resolve(newpath, &fs_id, pvfs_path, 256); 
     
    510700    } 
    511701    return 1; /* a PVFS path */ 
    512 } 
    513702#endif 
    514  
    515 void pvfs_debug(char *fmt, ...) 
    516 { 
    517     va_list ap; 
    518  
    519     va_start(ap, fmt); 
    520     vfprintf(stderr, fmt, ap); 
    521     va_end(ap); 
    522703} 
    523704 
  • branches/Orange-Branch/src/client/usrint/openfile-util.h

    r8907 r9016  
    44 * See COPYING in top-level directory. 
    55 */ 
     6 
     7#include "posix-ops.h" 
    68 
    79/** \file 
     
    4244                            PVFS_object_ref *ref); 
    4345 
    44 pvfs_descriptor *pvfs_alloc_descriptor(posix_ops *fsops); 
     46pvfs_descriptor *pvfs_alloc_descriptor(posix_ops *fsops, int fd); 
    4547 
    4648pvfs_descriptor *pvfs_find_descriptor(int fd); 
    4749 
     50int pvfs_dup_descriptor(int oldfd, int newfd); 
     51 
     52int pvfs_free_descriptor(int fd); 
     53 
     54int pvfs_descriptor_table_size(void); 
    4855 
    4956int pvfs_create_file(const char *filename, 
  • branches/Orange-Branch/src/client/usrint/posix-ops.h

    r8975 r9016  
    5151    int (*fstat)(int fd, struct stat *buf); 
    5252    int (*fstat64)(int fd, struct stat64 *buf); 
    53     int (*fstatat)(int fd, char *path, struct stat *buf, int flag); 
    54     int (*fstatat64)(int fd, char *path, struct stat64 *buf, int flag); 
     53    int (*fstatat)(int fd, const char *path, struct stat *buf, int flag); 
     54    int (*fstatat64)(int fd, const char *path, struct stat64 *buf, int flag); 
    5555    int (*lstat)(const char *path, struct stat *buf); 
    5656    int (*lstat64)(const char *path, struct stat64 *buf); 
     
    7474    int (*linkat)(int olddirfd, const char *oldpath, 
    7575                  int newdirfd, const char *newpath, int flags); 
    76     int (*readdir)(unsigned int fd, struct dirent *dirp, unsigned int count); 
    77     int (*getdents)(unsigned int fd, struct dirent *dirp, unsigned int count); 
    78     int (*getdents64)(unsigned int fd, struct dirent64 *dirp, unsigned int count); 
     76    int (*readdir)(u_int fd, struct dirent *dirp, u_int count); 
     77    int (*getdents)(u_int fd, struct dirent *dirp, u_int count); 
     78    int (*getdents64)(u_int fd, struct dirent64 *dirp, u_int count); 
    7979    int (*access)(const char *path, int mode); 
    8080    int (*faccessat)(int dirfd, const char *path, int mode, int flags); 
     
    9292    int (*mknod)(const char *path, mode_t mode, dev_t dev); 
    9393    int (*mknodat)(int dirfd, const char *path, mode_t mode, dev_t dev); 
    94     ssize_t (*sendfile)(int outfd, int infd, off_t offset, size_t count); 
    95     ssize_t (*sendfile64)(int outfd, int infd, off64_t offset, size_t count); 
     94    ssize_t (*sendfile)(int outfd, int infd, off_t *offset, size_t count); 
     95    ssize_t (*sendfile64)(int outfd, int infd, off64_t *offset, size_t count); 
    9696    int (*setxattr)(const char *path, const char *name, 
    9797                    const void *value, size_t size, int flags); 
  • branches/Orange-Branch/src/client/usrint/posix-pvfs.c

    r8975 r9016  
    4343    char *newpath; 
    4444    pvfs_descriptor *pd; 
     45    debug("pvfs_open: called with %s\n", path); 
    4546 
    4647    if (!path) 
     
    7172        return -1; 
    7273    } 
     74    debug("pvfs_open: returns %d\n", pd->fd); 
    7375    return pd->fd; 
    7476} 
     
    122124    if (!path) 
    123125    { 
    124         errno - EINVAL; 
     126        errno = EINVAL; 
    125127        return -1; 
    126128    } 
     
    176178    int mode; 
    177179    PVFS_hint hints; 
    178     pvfs_descriptor *pd; 
    179180 
    180181    if (dirfd < 0) 
     
    228229    int rc = 0; 
    229230    char *newpath; 
    230     pvfs_descriptor *pd; 
    231231 
    232232    newpath = pvfs_qualify_path(path); 
     
    264264            return -1; 
    265265        } 
    266         rc = iocommon_unlink(path, &pd->pvfs_ref); 
     266        if (flags & AT_REMOVEDIR) 
     267        { 
     268            rc = iocommon_rmdir(path, &pd->pvfs_ref); 
     269        } 
     270        else 
     271        { 
     272            rc = iocommon_unlink(path, &pd->pvfs_ref); 
     273        } 
    267274    } 
    268275    return rc; 
     
    620627    } 
    621628    /* if (file_size < offset + length) 
    622     /* { 
     629     * { 
    623630     */ 
    624631    return pvfs_ftruncate64(fd, (off64_t)(offset) + (off64_t)(length)); 
     
    662669int pvfs_close(int fd) 
    663670{ 
     671    int rc = 0; 
    664672    pvfs_descriptor* pd; 
     673    debug("pvfs_close: called with %d\n", fd); 
    665674 
    666675    if (fd < 0) 
     
    677686 
    678687    /* flush buffers */ 
    679     pvfs_flush(fd); 
     688    if (S_ISREG(pd->mode)) 
     689    { 
     690        rc = iocommon_fsync(pd); 
     691        if (rc < 0) 
     692        { 
     693            return -1; 
     694        } 
     695    } 
    680696 
    681697    /* free descriptor */ 
    682     pvfs_free_descriptor(fd); 
    683  
    684     return PVFS_FD_SUCCESS; 
     698    rc = pvfs_free_descriptor(pd->fd); 
     699    if (rc < 0) 
     700    { 
     701        return -1; 
     702    } 
     703 
     704    debug("pvfs_close: returns %d\n", rc); 
     705    return rc; 
    685706} 
    686707 
     
    690711int pvfs_flush(int fd) 
    691712{ 
     713    int rc = 0; 
    692714    pvfs_descriptor* pd; 
    693715 
    694 #ifdef DEBUG 
    695     pvfs_debug("in pvfs_flush(%ld)\n", fd); 
    696 #endif 
     716    debug("pvfs_flush: called with %d\n", fd); 
    697717 
    698718    if (fd < 0) 
     
    706726    { 
    707727        errno = EBADF; 
    708         return PVFS_FD_FAILURE; 
     728        return -1; 
    709729    } 
    710730 
    711731    /* tell the server to flush data to disk */ 
    712     return iocommon_fsync(pd); 
     732    rc = iocommon_fsync(pd); 
     733    debug("pvfs_flush: returns %d\n", rc); 
     734    return rc; 
    713735} 
    714736 
     
    809831 * pvfs_fstatat 
    810832 */ 
    811 int pvfs_fstatat(int fd, char *path, struct stat *buf, int flag) 
     833int pvfs_fstatat(int fd, const char *path, struct stat *buf, int flag) 
    812834{ 
    813835    int rc; 
     
    852874 * pvfs_fstatat64 
    853875 */ 
    854 int pvfs_fstatat64(int fd, char *path, struct stat64 *buf, int flag) 
     876int pvfs_fstatat64(int fd, const char *path, struct stat64 *buf, int flag) 
    855877{ 
    856878    int rc; 
     
    11541176{ 
    11551177    int rc; 
    1156     pvfs_descriptor *pd, *pd2; 
     1178    pvfs_descriptor *pd; 
    11571179 
    11581180    if (path[0] == '/' || dirfd == AT_FDCWD) 
     
    12131235       free(newpath); 
    12141236    } 
     1237    debug("pvfs_readlink mode is %o\n", pd->mode); 
    12151238    /* this checks that it is a valid symlink and sets errno if not */ 
    12161239    rc = iocommon_readlink(pd, buf, bufsiz); 
     
    12571280    char *abspath; 
    12581281    abspath = pvfs_qualify_path(newpath); 
    1259     rc =  iocommon_symlink(abspath, oldpath, NULL); 
     1282    rc = iocommon_symlink(abspath, oldpath, NULL); 
    12601283    if (abspath != newpath) 
    12611284    { 
     
    14071430    int rc = 0; 
    14081431    va_list ap; 
    1409     long arg; 
     1432    /* long arg; */ 
    14101433    struct flock *lock; 
    14111434    pvfs_descriptor *pd; 
     
    14151438    { 
    14161439        errno = EBADF; 
    1417         return -1; 
     1440        rc = -1; 
     1441        goto errorout; 
    14181442    } 
    14191443    va_start(ap, cmd); 
     
    14311455        break; 
    14321456    case F_SETFL : 
    1433         pd-> flags = va_arg(ap, int); 
     1457        pd->flags = va_arg(ap, int); 
    14341458        break; 
    14351459    case F_GETLK : 
    14361460    case F_SETLK : 
    14371461    case F_SETLKW : 
     1462        lock = va_arg(ap, struct flock *); 
    14381463    case F_GETOWN : 
    14391464    case F_SETOWN : 
     
    15351560        free(newpath); 
    15361561    } 
    1537     rc = iocommon_statfs(pd, buf); 
     1562    rc = iocommon_statfs64(pd, buf); 
    15381563    pvfs_close(pd->fd); 
    15391564    return rc; 
     
    15841609{ 
    15851610    int fd; 
    1586     int s_type = mode & S_IFMT; 
     1611    /* int s_type = mode & S_IFMT; */ 
    15871612     
    15881613    switch (dev) 
     
    16071632} 
    16081633 
    1609 ssize_t pvfs_sendfile(int outfd, int infd, off_t offset, size_t count) 
    1610 { 
    1611     return pvfs_sendfile64(outfd, infd, (off64_t) offset, count); 
     1634ssize_t pvfs_sendfile(int outfd, int infd, off_t *offset, size_t count) 
     1635{ 
     1636    return pvfs_sendfile64(outfd, infd, (off64_t *)offset, count); 
    16121637} 
    16131638                  
    1614 ssize_t pvfs_sendfile64(int outfd, int infd, off64_t offset, size_t count) 
     1639ssize_t pvfs_sendfile64(int outfd, int infd, off64_t *offset, size_t count) 
    16151640{ 
    16161641    pvfs_descriptor *inpd, *outpd; 
  • branches/Orange-Branch/src/client/usrint/posix-pvfs.h

    r8975 r9016  
    8888int pvfs_stat64(const char *path, struct stat64 *buf); 
    8989 
     90int pvfs_stat_mask(const char *path, struct stat *buf, uint32_t mask); 
     91 
    9092int pvfs_fstat(int fd, struct stat *buf); 
    9193 
    9294int pvfs_fstat64(int fd, struct stat64 *buf); 
    9395 
    94 int pvfs_fstatat(int fd, char *path, struct stat *buf, int flag); 
    95  
    96 int pvfs_fstatat64(int fd, char *path, struct stat64 *buf, int flag); 
     96int pvfs_fstatat(int fd, const char *path, struct stat *buf, int flag); 
     97 
     98int pvfs_fstatat64(int fd, const char *path, struct stat64 *buf, int flag); 
     99 
     100int pvfs_fstat_mask(int fd, struct stat *buf, uint32_t mask); 
    97101 
    98102int pvfs_lstat(const char *path, struct stat *buf); 
    99103 
    100104int pvfs_lstat64(const char *path, struct stat64 *buf); 
     105 
     106int pvfs_lstat_mask(const char *path, struct stat *buf, uint32_t mask); 
    101107 
    102108int pvfs_dup(int oldfd); 
     
    180186int pvfs_mknodat(int dirfd, const char *path, mode_t mode, dev_t dev); 
    181187 
    182 ssize_t pvfs_sendfile(int outfd, int infd, off_t offset, size_t count); 
    183  
    184 ssize_t pvfs_sendfile64(int outfd, int infd, off64_t offset, size_t count); 
     188ssize_t pvfs_sendfile(int outfd, int infd, off_t *offset, size_t count); 
     189 
     190ssize_t pvfs_sendfile64(int outfd, int infd, off64_t *offset, size_t count); 
    185191 
    186192int pvfs_setxattr(const char *path, const char *name, 
  • branches/Orange-Branch/src/client/usrint/posix.c

    r8975 r9016  
    1010 *  PVFS2 user interface routines - wrappers for posix system calls 
    1111 */ 
     12 
     13/* this prevents headers from using inlines for 64 bit calls */ 
     14#ifdef _FILE_OFFSET_BITS 
     15#undef _FILE_OFFSET_BITS 
     16#endif 
     17 
    1218#include <usrint.h> 
    1319#include <linux/dirent.h> 
     
    5662        int rc; 
    5763        struct stat sbuf; 
    58         /* descrptor unknown to FS here so we must set it up */ 
    59         rc = pd->fsops->open(path, flags & 01777777, mode); 
     64        /* path unknown to FS so open with glibc */ 
     65        rc = glibc_ops.open(path, flags & 01777777, mode); 
    6066        if (rc < 0) 
    6167        { 
    6268            return rc; 
    6369        } 
    64         pd = pvfs_alloc_descriptor(&glibc_ops); 
     70        /* set up the descriptor manually */ 
     71        pd = pvfs_alloc_descriptor(&glibc_ops, rc); 
     72        if (!pd) 
     73        { 
     74            return -1; 
     75        } 
    6576        pd->is_in_use = PVFS_FS; 
    66         pd->true_fd = rc; 
    6777        pd->flags = flags; 
    68         fstat(rc, &sbuf); 
     78        glibc_ops.fstat(rc, &sbuf); 
    6979        pd->mode = sbuf.st_mode; 
    7080        return pd->fd;  
     
    7888{  
    7989    int fd; 
     90    int mode = 0; 
    8091    va_list ap; 
    8192    va_start(ap, flags); 
    82     fd = open(path, flags|O_LARGEFILE, ap);  
     93    if (flags & O_CREAT) 
     94    { 
     95        mode = va_arg(ap, int); 
     96    } 
     97    fd = open(path, flags|O_LARGEFILE, mode);  
    8398    va_end(ap); 
    8499    return fd; 
     
    88103{ 
    89104    int fd;  
     105    int mode = 0; 
    90106    pvfs_descriptor *pd;  
    91107    va_list ap; 
     
    97113    } 
    98114    va_start(ap, flags); 
     115    if (flags & O_CREAT) 
     116    { 
     117        mode = va_arg(ap, int); 
     118    } 
    99119    if (dirfd == AT_FDCWD || path[0] == '/') 
    100120    { 
    101         fd = open(path, flags, ap); 
     121        fd = open(path, flags, mode); 
    102122    } 
    103123    else 
     
    106126        if (pd) 
    107127        { 
    108             fd = pd->fsops->openat(pd->true_fd, path, flags, ap); 
     128            fd = pd->fsops->openat(pd->true_fd, path, flags, mode); 
    109129        } 
    110130        else 
     
    121141{ 
    122142    int fd; 
     143    int mode; 
    123144    va_list ap; 
    124145    va_start(ap, flags); 
    125     fd = openat(dirfd, path, flags|O_LARGEFILE, ap);  
     146    if (flags & O_CREAT) 
     147    { 
     148        mode = va_arg(ap, int); 
     149    } 
     150    fd = openat(dirfd, path, flags|O_LARGEFILE, mode);  
    126151    va_end(ap); 
    127152    return fd; 
     
    154179        return -1; 
    155180    } 
    156     if (is_pvfs_path(path)) { 
     181    if (is_pvfs_path(path)) 
     182    { 
    157183        return pvfs_ops.unlink(path); 
    158184    } 
    159     else { 
     185    else 
     186    { 
    160187        return glibc_ops.unlink(path); 
    161188    } 
     
    164191int unlinkat(int dirfd, const char *path, int flag) 
    165192{ 
    166     int rc;  
     193    int rc = 0;  
    167194    pvfs_descriptor *pd;  
    168195     
     
    189216        } 
    190217    } 
     218    return rc; 
    191219} 
    192220 
     
    252280ssize_t read(int fd, void *buf, size_t count) 
    253281{ 
    254     ssize_t rc;  
     282    ssize_t rc = 0;  
    255283    pvfs_descriptor *pd;  
    256284     
     
    273301ssize_t pread(int fd, void *buf, size_t nbytes, off_t offset) 
    274302{ 
    275     ssize_t rc;  
     303    ssize_t rc = 0;  
    276304    pvfs_descriptor *pd;  
    277305     
     
    288316    return rc; 
    289317}  
    290  
    291 /* 
    292  * pread64 wrapper  
    293  */ 
    294 ssize_t pread64(int fd, void *buf, size_t nbytes, off64_t offset) 
    295 { 
    296     ssize_t rc;  
    297     pvfs_descriptor *pd;  
    298      
    299     pd = pvfs_find_descriptor(fd);  
    300     if (pd) 
    301     { 
    302         rc = pd->fsops->pread64(pd->true_fd, (void *)buf, nbytes, offset);  
    303     } 
    304     else 
    305     { 
    306         errno = EBADF; 
    307         rc = -1; 
    308     } 
    309     return rc; 
    310 } 
    311318 
    312319/* 
     
    315322ssize_t readv(int fd, const struct iovec *iov, int iovcnt) 
    316323{  
    317     ssize_t rc; 
     324    ssize_t rc = 0; 
    318325    pvfs_descriptor *pd;  
    319326     
     
    322329    { 
    323330        rc = pd->fsops->readv(pd->true_fd, iov, iovcnt);  
     331    } 
     332    else 
     333    { 
     334        errno = EBADF; 
     335        rc = -1; 
     336    } 
     337    return rc; 
     338} 
     339 
     340/* 
     341 * pread64 wrapper  
     342 */ 
     343ssize_t pread64(int fd, void *buf, size_t nbytes, off64_t offset) 
     344{ 
     345    ssize_t rc = 0;  
     346    pvfs_descriptor *pd;  
     347     
     348    pd = pvfs_find_descriptor(fd);  
     349    if (pd) 
     350    { 
     351        rc = pd->fsops->pread64(pd->true_fd, (void *)buf, nbytes, offset);  
    324352    } 
    325353    else 
     
    336364ssize_t write(int fd, const void *buf, size_t count) 
    337365{ 
    338     ssize_t rc;  
     366    ssize_t rc = 0;  
    339367    pvfs_descriptor *pd;  
    340368     
     
    357385ssize_t pwrite(int fd, const void *buf, size_t nbytes, off_t offset) 
    358386{ 
    359     ssize_t rc; 
     387    ssize_t rc = 0; 
    360388    pvfs_descriptor *pd; 
    361389     
     
    364392    { 
    365393        rc = pd->fsops->pwrite(pd->true_fd, buf, nbytes, offset); 
    366     } 
    367     else 
    368     { 
    369         errno = EBADF; 
    370         rc = -1; 
    371     } 
    372     return rc; 
    373 } 
    374  
    375 /* 
    376  * pwrite64 wrapper  
    377  */ 
    378 ssize_t pwrite64(int fd, const void *buf, size_t nbytes, off64_t offset) 
    379 { 
    380     ssize_t rc; 
    381     pvfs_descriptor *pd; 
    382      
    383     pd = pvfs_find_descriptor(fd); 
    384     if (pd) 
    385     { 
    386         rc = pd->fsops->pwrite64(pd->true_fd, buf, nbytes, offset); 
    387394    } 
    388395    else 
     
    399406ssize_t writev(int fd, const struct iovec *iov, int iovcnt) 
    400407{  
    401     ssize_t rc; 
     408    ssize_t rc = 0; 
    402409    pvfs_descriptor *pd; 
    403410     
     
    408415        if (rc > 0) 
    409416            pd->file_pointer += rc; 
     417    } 
     418    else 
     419    { 
     420        errno = EBADF; 
     421        rc = -1; 
     422    } 
     423    return rc; 
     424} 
     425 
     426/* 
     427 * pwrite64 wrapper  
     428 */ 
     429ssize_t pwrite64(int fd, const void *buf, size_t nbytes, off64_t offset) 
     430{ 
     431    ssize_t rc = 0; 
     432    pvfs_descriptor *pd; 
     433     
     434    pd = pvfs_find_descriptor(fd); 
     435    if (pd) 
     436    { 
     437        rc = pd->fsops->pwrite64(pd->true_fd, buf, nbytes, offset); 
    410438    } 
    411439    else 
     
    438466off64_t lseek64(int fd, off64_t offset, int whence) 
    439467{ 
    440     off64_t rc; 
     468    off64_t rc = 0; 
    441469    pvfs_descriptor *pd; 
    442470     
     
    490518int ftruncate(int fd, off_t length) 
    491519{ 
    492     int rc; 
     520    int rc = 0; 
    493521    pvfs_descriptor *pd; 
    494522     
     
    508536int ftruncate64(int fd, off64_t length) 
    509537{ 
    510     int rc; 
     538    int rc = 0; 
    511539    pvfs_descriptor *pd; 
    512540     
     
    527555int posix_fallocate(int fd, off_t offset, off_t length) 
    528556{ 
    529     int rc; 
     557    int rc = 0; 
    530558    pvfs_descriptor *pd; 
    531559     
     
    549577int close(int fd) 
    550578{ 
    551     pvfs_descriptor *pd; 
    552      
    553     pd = pvfs_find_descriptor(fd); 
    554     if (pd) 
    555     { 
    556         pd->fsops->flush(pd->true_fd); 
    557         pd->fsops->close(pd->true_fd); 
    558         pvfs_free_descriptor(pd); 
    559         return 0; 
    560     } 
    561     else 
    562     { 
    563         errno = EBADF; 
    564         return -1; 
    565     } 
     579    int rc = 0; 
     580     
     581    rc = pvfs_free_descriptor(fd); 
     582    return rc; 
    566583} 
    567584 
    568585int flush(int fd) 
    569586{ 
    570     int rc; 
     587    int rc = 0; 
    571588    pvfs_descriptor *pd; 
    572589     
     
    631648int fstat(int fd, struct stat *buf) 
    632649{ 
    633     int rc; 
     650    int rc = 0; 
    634651    pvfs_descriptor *pd; 
    635652     
     
    654671int fstat64(int fd, struct stat64 *buf) 
    655672{ 
    656     int rc; 
     673    int rc = 0; 
    657674    pvfs_descriptor *pd; 
    658675     
     
    675692} 
    676693 
     694int fstatat(int fd, const char *path, struct stat *buf, int flag) 
     695{ 
     696    int rc = 0;  
     697    pvfs_descriptor *pd;  
     698     
     699    if (fd == AT_FDCWD || path[0] == '/') 
     700    { 
     701        if (flag & AT_SYMLINK_NOFOLLOW) 
     702        { 
     703            lstat(path, buf); 
     704        } 
     705        else 
     706        { 
     707            stat(path, buf); 
     708        } 
     709    } 
     710    else 
     711    { 
     712        pd = pvfs_find_descriptor(fd); 
     713        if (pd) 
     714        { 
     715            rc = pd->fsops->fstatat(pd->true_fd, path, buf, flag); 
     716        } 
     717        else 
     718        { 
     719            errno = EBADF; 
     720            rc = -1; 
     721        } 
     722    } 
     723    return rc; 
     724} 
     725 
     726int __fxstatat(int ver, int fd, const char *path, struct stat *buf, int flag) 
     727{ 
     728    return fstatat(fd, path, buf, flag); 
     729} 
     730 
     731int fstatat64(int fd, const char *path, struct stat64 *buf, int flag) 
     732{ 
     733    int rc = 0;  
     734    pvfs_descriptor *pd;  
     735     
     736    if (fd == AT_FDCWD || path[0] == '/') 
     737    { 
     738        if (flag & AT_SYMLINK_NOFOLLOW) 
     739        { 
     740            lstat64(path, buf); 
     741        } 
     742        else 
     743        { 
     744            stat64(path, buf); 
     745        } 
     746    } 
     747    else 
     748    { 
     749        pd = pvfs_find_descriptor(fd); 
     750        if (pd) 
     751        { 
     752            rc = pd->fsops->fstatat64(pd->true_fd, path, buf, flag); 
     753        } 
     754        else 
     755        { 
     756            errno = EBADF; 
     757            rc = -1; 
     758        } 
     759    } 
     760    return rc; 
     761} 
     762 
     763int __fxstatat64(int ver, int fd, const char *path, struct stat64 *buf, int flag) 
     764{ 
     765    return fstatat64(fd, path, buf, flag); 
     766} 
     767 
    677768int lstat(const char *path, struct stat *buf) 
    678769{ 
     
    721812int dup(int oldfd) 
    722813{ 
    723     int rc; 
     814    int rc = 0; 
    724815    pvfs_descriptor *pd; 
    725816     
     
    739830int dup2(int oldfd, int newfd) 
    740831{ 
    741     int rc; 
     832    int rc = 0; 
    742833    pvfs_descriptor *pd; 
    743834     
     
    774865int fchown(int fd, uid_t owner, gid_t group) 
    775866{ 
    776     int rc; 
     867    int rc = 0; 
    777868    pvfs_descriptor *pd; 
    778869     
     
    792883int fchownat(int dirfd, const char *path, uid_t owner, gid_t group, int flag) 
    793884{ 
    794     ssize_t rc;  
     885    int rc = 0;  
    795886    pvfs_descriptor *pd;  
    796887     
     
    812903        } 
    813904    } 
     905    return rc; 
    814906} 
    815907 
     
    850942int fchmod(int fd, mode_t mode) 
    851943{ 
    852     int rc; 
     944    int rc = 0; 
    853945    pvfs_descriptor *pd; 
    854946     
     
    868960int fchmodat(int dirfd, const char *path, mode_t mode, int flag) 
    869961{ 
    870     ssize_t rc;  
     962    int rc = 0;  
    871963    pvfs_descriptor *pd;  
    872964     
     
    893985        } 
    894986    } 
     987    return rc; 
    895988} 
    896989 
     
    9141007int mkdirat(int dirfd, const char *path, mode_t mode) 
    9151008{ 
    916     ssize_t rc;  
     1009    int rc = 0;  
    9171010    pvfs_descriptor *pd;  
    9181011     
     
    9391032        } 
    9401033    } 
     1034    return rc; 
    9411035} 
    9421036 
     
    9771071int readlinkat(int dirfd, const char *path, char *buf, size_t bufsiz) 
    9781072{ 
    979     ssize_t rc;  
     1073    int rc = 0;  
    9801074    pvfs_descriptor *pd;  
    9811075     
     
    10021096        } 
    10031097    } 
     1098    return rc; 
    10041099} 
    10051100 
     
    10231118int symlinkat(const char *oldpath, int newdirfd, const char *newpath) 
    10241119{ 
    1025     ssize_t rc;  
     1120    int rc = 0;  
    10261121    pvfs_descriptor *pd;  
    10271122     
     
    10481143        } 
    10491144    } 
     1145    return rc; 
    10501146} 
    10511147 
     
    10651161        return glibc_ops.link(oldpath, newpath); 
    10661162    } 
     1163    return -1; 
    10671164} 
    10681165 
     
    10951192} 
    10961193 
    1097 int readdir(unsigned int fd, struct dirent *dirp, unsigned int count) 
     1194int posix_readdir(unsigned int fd, struct dirent *dirp, unsigned int count) 
    10981195{ 
    10991196    int rc; 
     
    11201217int getdents(unsigned int fd, struct dirent *dirp, unsigned int count) 
    11211218{ 
    1122     int rc; 
     1219    int rc = 0; 
    11231220    pvfs_descriptor *pd; 
    11241221     
     
    11431240int getdents64(unsigned int fd, struct dirent64 *dirp, unsigned int count) 
    11441241{ 
    1145     int rc; 
     1242    int rc = 0; 
    11461243    pvfs_descriptor *pd; 
    11471244     
     
    11881285int faccessat(int dirfd, const char *path, int mode, int flags) 
    11891286{ 
    1190     ssize_t rc;  
     1287    int rc = 0;  
    11911288    pvfs_descriptor *pd;  
    11921289     
     
    12131310        } 
    12141311    } 
     1312    return rc; 
    12151313} 
    12161314 
    12171315int flock(int fd, int op) 
    12181316{ 
    1219     int rc; 
     1317    int rc = 0; 
    12201318    pvfs_descriptor *pd; 
    12211319     
     
    12351333int fcntl(int fd, int cmd, ...) 
    12361334{ 
    1237     int rc; 
     1335    int rc = 0; 
     1336    long arg; 
     1337    struct flock *lock; 
    12381338    pvfs_descriptor *pd; 
    12391339    va_list ap; 
     
    12431343    if (pd) 
    12441344    { 
    1245         rc = pd->fsops->fcntl(pd->true_fd, cmd, ap); 
     1345        switch (cmd) 
     1346        { 
     1347        case F_GETLK: 
     1348        case F_SETLK: 
     1349        case F_SETLKW: 
     1350            lock = va_arg(ap, struct flock *); 
     1351            rc = pd->fsops->fcntl(pd->true_fd, cmd, lock); 
     1352            break; 
     1353        default: 
     1354            arg = va_arg(ap, long); 
     1355            rc = pd->fsops->fcntl(pd->true_fd, cmd, arg); 
     1356            break; 
     1357        } 
    12461358    } 
    12471359    else 
     
    12621374int fsync(int fd) 
    12631375{ 
    1264     int rc; 
     1376    int rc = 0; 
    12651377    pvfs_descriptor *pd; 
    12661378     
     
    12801392int fdatasync(int fd) 
    12811393{ 
    1282     int rc; 
     1394    int rc = 0; 
    12831395    pvfs_descriptor *pd; 
    12841396     
     
    12981410int posix_fadvise(int fd, off_t offset, off_t length, int advice) 
    12991411{ 
    1300     int rc; 
     1412    int rc = 0; 
    13011413    pvfs_descriptor *pd; 
    13021414     
     
    13161428int fadvise(int fd, off_t offset, off_t len, int advice) 
    13171429{ 
    1318     int rc; 
     1430    int rc = 0; 
    13191431    pvfs_descriptor *pd; 
    13201432     
     
    13341446int fadvise64(int fd, off64_t offset, off64_t len, int advice) 
    13351447{ 
    1336     int rc; 
     1448    int rc = 0; 
    13371449    pvfs_descriptor *pd; 
    13381450     
     
    13861498int fstatfs(int fd, struct statfs *buf) 
    13871499{ 
    1388     int rc; 
     1500    int rc = 0; 
    13891501    pvfs_descriptor *pd; 
    13901502     
     
    14091521int fstatfs64(int fd, struct statfs64 *buf) 
    14101522{ 
    1411     int rc; 
     1523    int rc = 0; 
    14121524    pvfs_descriptor *pd; 
    14131525     
     
    14491561int mknodat(int dirfd, const char *path, mode_t mode, dev_t dev) 
    14501562{             
    1451     ssize_t rc;  
     1563    int rc = 0;  
    14521564    pvfs_descriptor *pd;  
    14531565     
     
    14741586        } 
    14751587    } 
    1476 } 
    1477  
    1478 ssize_t sendfile(int outfd, int infd, off_t offset, size_t count) 
    1479 { 
    1480     return sendfile64(outfd, infd, (off64_t)offset, count); 
    1481 } 
    1482  
    1483 ssize_t sendfile64(int outfd, int infd, off64_t offset, size_t count) 
    1484 { 
    1485     int rc; 
     1588    return rc; 
     1589} 
     1590 
     1591ssize_t sendfile(int outfd, int infd, off_t *offset, size_t count) 
     1592{ 
     1593    return sendfile64(outfd, infd, (off64_t *)offset, count); 
     1594} 
     1595 
     1596ssize_t sendfile64(int outfd, int infd, off64_t *offset, size_t count) 
     1597{ 
     1598    int rc = 0; 
    14861599    pvfs_descriptor *inpd, *outpd; 
    14871600     
     
    15401653              const void *value, size_t size, int flags) 
    15411654{ 
    1542     int rc; 
     1655    int rc = 0; 
    15431656    pvfs_descriptor *pd; 
    15441657     
     
    15951708              size_t size) 
    15961709{ 
    1597     int rc; 
     1710    int rc = 0; 
    15981711    pvfs_descriptor *pd; 
    15991712     
     
    16471760int flistxattr(int fd, char *list, size_t size) 
    16481761{ 
    1649     int rc; 
     1762    int rc = 0; 
    16501763    pvfs_descriptor *pd; 
    16511764     
     
    16991812int fremovexattr(int fd, const char *name) 
    17001813{ 
    1701     int rc; 
     1814    int rc = 0; 
    17021815    pvfs_descriptor *pd; 
    17031816     
  • branches/Orange-Branch/src/client/usrint/request.c

    r8890 r9016  
    1111 */ 
    1212#include <usrint.h> 
     13 
     14int pvfs_check_vector(const struct iovec *iov, 
     15                      int count, 
     16                      PVFS_Request *req, 
     17                      void **buf); 
    1318 
    1419/** 
     
    3944    PVFS_Request *req_array; 
    4045    int rblk; 
    41     PVFS_Request tmpreq; 
    4246 
    4347    /* set up request arrays */ 
  • branches/Orange-Branch/src/client/usrint/socket.c

    r8975 r9016  
    3030        return sockfd; 
    3131    } 
    32     pd = pvfs_alloc_descriptor(&glibc_ops); 
    33     pd->is_in_use = PVFS_FS; 
    34     pd->true_fd = sockfd; 
     32    pd = pvfs_alloc_descriptor(&glibc_ops, sockfd); 
     33    pd->mode |= S_IFSOCK; 
    3534    return pd->fd; 
    3635} 
     
    3837int accept (int sockfd, struct sockaddr *addr, socklen_t *alen) 
    3938{ 
    40     int rc = 0; 
    41     pvfs_descriptor *pd; 
    42  
    43     pd = pvfs_find_descriptor(sockfd); 
    44     if (pd) 
    45     { 
    46         rc = pd->fsops->accept(pd->true_fd, addr, alen); 
    47     } 
    48     else 
    49     { 
    50         rc = -1; 
    51         errno = EBADF; 
    52     } 
     39    int rc = 0, fd; 
     40    pvfs_descriptor *pd; 
     41 
     42    pd = pvfs_find_descriptor(sockfd); 
     43    if (!pd) 
     44    { 
     45        errno = EBADF; 
     46        rc = -1; 
     47        goto errorout; 
     48    } 
     49    if (!S_ISSOCK(pd->mode)) 
     50    { 
     51        errno = ENOTSOCK; 
     52        rc = -1; 
     53        goto errorout; 
     54    } 
     55    fd = pd->fsops->accept(pd->true_fd, addr, alen); 
     56    if (fd < 0) 
     57    { 
     58        rc = -1; 
     59        goto errorout; 
     60    } 
     61    pd = pvfs_alloc_descriptor(&glibc_ops, fd); 
     62    pd->mode |= S_IFSOCK; 
     63    rc = fd;    
     64errorout: 
    5365    return rc; 
    5466} 
     
    6072 
    6173    pd = pvfs_find_descriptor(sockfd); 
    62     if (pd) 
    63     { 
    64         rc = pd->fsops->bind(pd->true_fd, addr, alen); 
    65     } 
    66     else 
    67     { 
    68         rc = -1; 
    69         errno = EBADF; 
    70     } 
     74    if (!pd) 
     75    { 
     76        errno = EBADF; 
     77        rc = -1; 
     78        goto errorout; 
     79    } 
     80    if (!S_ISSOCK(pd->mode)) 
     81    { 
     82        errno = ENOTSOCK; 
     83        rc = -1; 
     84        goto errorout; 
     85    } 
     86    rc = pd->fsops->bind(pd->true_fd, addr, alen); 
     87errorout: 
    7188    return rc; 
    7289} 
     
    7895 
    7996    pd = pvfs_find_descriptor(sockfd); 
    80     if (pd) 
    81     { 
    82         rc = pd->fsops->connect(pd->true_fd, addr, alen); 
    83     } 
    84     else 
    85     { 
    86         rc = -1; 
    87         errno = EBADF; 
    88     } 
     97    if (!pd) 
     98    { 
     99        errno = EBADF; 
     100        rc = -1; 
     101        goto errorout; 
     102    } 
     103    if (!S_ISSOCK(pd->mode)) 
     104    { 
     105        errno = ENOTSOCK; 
     106        rc = -1; 
     107        goto errorout; 
     108    } 
     109    rc = pd->fsops->connect(pd->true_fd, addr, alen); 
     110errorout: 
    89111    return rc; 
    90112} 
     
    96118 
    97119    pd = pvfs_find_descriptor(sockfd); 
    98     if (pd) 
    99     { 
    100         rc = pd->fsops->getpeername(pd->true_fd, addr, alen); 
    101     } 
    102     else 
    103     { 
    104         rc = -1; 
    105         errno = EBADF; 
    106     } 
     120    if (!pd) 
     121    { 
     122        errno = EBADF; 
     123        rc = -1; 
     124        goto errorout; 
     125    } 
     126    if (!S_ISSOCK(pd->mode)) 
     127    { 
     128        errno = ENOTSOCK; 
     129        rc = -1; 
     130        goto errorout; 
     131    } 
     132    rc = pd->fsops->getpeername(pd->true_fd, addr, alen); 
     133errorout: 
    107134    return rc; 
    108135} 
     
    114141 
    115142    pd = pvfs_find_descriptor(sockfd); 
    116     if (pd) 
    117     { 
    118         rc = pd->fsops->getsockname(pd->true_fd, addr, alen); 
    119     } 
    120     else 
    121     { 
    122         rc = -1; 
    123         errno = EBADF; 
    124     } 
     143    if (!pd) 
     144    { 
     145        errno = EBADF; 
     146        rc = -1; 
     147        goto errorout; 
     148    } 
     149    if (!S_ISSOCK(pd->mode)) 
     150    { 
     151        errno = ENOTSOCK; 
     152        rc = -1; 
     153        goto errorout; 
     154    } 
     155    rc = pd->fsops->getsockname(pd->true_fd, addr, alen); 
     156errorout: 
    125157    return rc; 
    126158} 
     
    133165 
    134166    pd = pvfs_find_descriptor(sockfd); 
    135     if (pd) 
    136     { 
    137         rc = pd->fsops->getsockopt(pd->true_fd, lvl, oname, oval, olen); 
    138     } 
    139     else 
    140     { 
    141         rc = -1; 
    142         errno = EBADF; 
    143     } 
     167    if (!pd) 
     168    { 
     169        errno = EBADF; 
     170        rc = -1; 
     171        goto errorout; 
     172    } 
     173    if (!S_ISSOCK(pd->mode)) 
     174    { 
     175        errno = ENOTSOCK; 
     176        rc = -1; 
     177        goto errorout; 
     178    } 
     179    rc = pd->fsops->getsockopt(pd->true_fd, lvl, oname, oval, olen); 
     180errorout: 
    144181    return rc; 
    145182} 
     
    152189 
    153190    pd = pvfs_find_descriptor(sockfd); 
    154     if (pd) 
    155     { 
    156         rc = pd->fsops->setsockopt(pd->true_fd, lvl, oname, oval, olen); 
    157     } 
    158     else 
    159     { 
    160         rc = -1; 
    161         errno = EBADF; 
    162     } 
     191    if (!pd) 
     192    { 
     193        errno = EBADF; 
     194        rc = -1; 
     195        goto errorout; 
     196    } 
     197    if (!S_ISSOCK(pd->mode)) 
     198    { 
     199        errno = ENOTSOCK; 
     200        rc = -1; 
     201        goto errorout; 
     202    } 
     203    rc = pd->fsops->setsockopt(pd->true_fd, lvl, oname, oval, olen); 
     204errorout: 
    163205    return rc; 
    164206} 
     
    172214    va_start(ap, request); 
    173215    pd = pvfs_find_descriptor(fd); 
    174     if (pd) 
    175     { 
    176         rc = pd->fsops->ioctl(pd->true_fd, request, ap); 
    177     } 
    178     else 
    179     { 
    180         errno = EBADF; 
    181         rc = -1; 
    182     } 
     216    if (!pd) 
     217    { 
     218        errno = EBADF; 
     219        rc = -1; 
     220        goto errorout; 
     221    } 
     222    if (!S_ISSOCK(pd->mode)) 
     223    { 
     224        errno = ENOTSOCK; 
     225        rc = -1; 
     226        goto errorout; 
     227    } 
     228    rc = pd->fsops->ioctl(pd->true_fd, request, ap); 
    183229    va_end(ap); 
     230errorout: 
    184231    return rc; 
    185232} 
     
    191238 
    192239    pd = pvfs_find_descriptor(sockfd); 
    193     if (pd) 
    194     { 
    195         rc = pd->fsops->listen(pd->true_fd, backlog); 
    196     } 
    197     else 
    198     { 
    199         rc = -1; 
    200         errno = EBADF; 
    201     } 
     240    if (!pd) 
     241    { 
     242        errno = EBADF; 
     243        rc = -1; 
     244        goto errorout; 
     245    } 
     246    if (!S_ISSOCK(pd->mode)) 
     247    { 
     248        errno = ENOTSOCK; 
     249        rc = -1; 
     250        goto errorout; 
     251    } 
     252    rc = pd->fsops->listen(pd->true_fd, backlog); 
     253errorout: 
    202254    return rc; 
    203255} 
     
    209261 
    210262    pd = pvfs_find_descriptor(sockfd); 
    211     if (pd) 
    212     { 
    213         rc = pd->fsops->recv(pd->true_fd, buf, len, flags); 
    214     } 
    215     else 
    216     { 
    217         rc = -1; 
    218         errno = EBADF; 
    219     } 
     263    if (!pd) 
     264    { 
     265        errno = EBADF; 
     266        rc = -1; 
     267        goto errorout; 
     268    } 
     269    if (!S_ISSOCK(pd->mode)) 
     270    { 
     271        errno = ENOTSOCK; 
     272        rc = -1; 
     273        goto errorout; 
     274    } 
     275    rc = pd->fsops->recv(pd->true_fd, buf, len, flags); 
     276errorout: 
    220277    return rc; 
    221278} 
     
    228285 
    229286    pd = pvfs_find_descriptor(sockfd); 
    230     if (pd) 
    231     { 
    232         rc = pd->fsops->recvfrom(pd->true_fd, buf, len, flags, addr, alen); 
    233     } 
    234     else 
    235     { 
    236         rc = -1; 
    237         errno = EBADF; 
    238     } 
     287    if (!pd) 
     288    { 
     289        errno = EBADF; 
     290        rc = -1; 
     291        goto errorout; 
     292    } 
     293    if (!S_ISSOCK(pd->mode)) 
     294    { 
     295        errno = ENOTSOCK; 
     296        rc = -1; 
     297        goto errorout; 
     298    } 
     299    rc = pd->fsops->recvfrom(pd->true_fd, buf, len, flags, addr, alen); 
     300errorout: 
    239301    return rc; 
    240302} 
     
    246308 
    247309    pd = pvfs_find_descriptor(sockfd); 
    248     if (pd) 
    249     { 
    250         rc = pd->fsops->recvmsg(pd->true_fd, msg, flags); 
    251     } 
    252     else 
    253     { 
    254         rc = -1; 
    255         errno = EBADF; 
    256     } 
     310    if (!pd) 
     311    { 
     312        errno = EBADF; 
     313        rc = -1; 
     314        goto errorout; 
     315    } 
     316    if (!S_ISSOCK(pd->mode)) 
     317    { 
     318        errno = ENOTSOCK; 
     319        rc = -1; 
     320        goto errorout; 
     321    } 
     322    rc = pd->fsops->recvmsg(pd->true_fd, msg, flags); 
     323errorout: 
    257324    return rc; 
    258325} 
     
    273340 
    274341    pd = pvfs_find_descriptor(sockfd); 
    275     if (pd) 
    276     { 
    277         rc = pd->fsops->send(pd->true_fd, buf, len, flags); 
    278     } 
    279     else 
    280     { 
    281         rc = -1; 
    282         errno = EBADF; 
    283     } 
     342    if (!pd) 
     343    { 
     344        errno = EBADF; 
     345        rc = -1; 
     346        goto errorout; 
     347    } 
     348    if (!S_ISSOCK(pd->mode)) 
     349    { 
     350        errno = ENOTSOCK; 
     351        rc = -1; 
     352        goto errorout; 
     353    } 
     354    rc = pd->fsops->send(pd->true_fd, buf, len, flags); 
     355errorout: 
    284356    return rc; 
    285357} 
     
    292364 
    293365    pd = pvfs_find_descriptor(sockfd); 
    294     if (pd) 
    295     { 
    296         rc = pd->fsops->sendto(pd->true_fd, buf, len, flags, addr, alen); 
    297     } 
    298     else 
    299     { 
    300         rc = -1; 
    301         errno = EBADF; 
    302     } 
     366    if (!pd) 
     367    { 
     368        errno = EBADF; 
     369        rc = -1; 
     370        goto errorout; 
     371    } 
     372    if (!S_ISSOCK(pd->mode)) 
     373    { 
     374        errno = ENOTSOCK; 
     375        rc = -1; 
     376        goto errorout; 
     377    } 
     378    rc = pd->fsops->sendto(pd->true_fd, buf, len, flags, addr, alen); 
     379errorout: 
    303380    return rc; 
    304381} 
     
    310387 
    311388    pd = pvfs_find_descriptor(sockfd); 
    312     if (pd) 
    313     { 
    314         rc = pd->fsops->sendmsg(pd->true_fd, msg, flags); 
    315     } 
    316     else 
    317     { 
    318         rc = -1; 
    319         errno = EBADF; 
    320     } 
     389    if (!pd) 
     390    { 
     391        errno = EBADF; 
     392        rc = -1; 
     393        goto errorout; 
     394    } 
     395    if (!S_ISSOCK(pd->mode)) 
     396    { 
     397        errno = ENOTSOCK; 
     398        rc = -1; 
     399        goto errorout; 
     400    } 
     401    rc = pd->fsops->sendmsg(pd->true_fd, msg, flags); 
     402errorout: 
    321403    return rc; 
    322404} 
     
    328410 
    329411    pd = pvfs_find_descriptor(sockfd); 
    330     if (pd) 
    331     { 
    332         rc = pd->fsops->shutdown(pd->true_fd, how); 
    333     } 
    334     else 
    335     { 
    336         rc = -1; 
    337         errno = EBADF; 
    338     } 
     412    if (!pd) 
     413    { 
     414        errno = EBADF; 
     415        rc = -1; 
     416        goto errorout; 
     417    } 
     418    if (!S_ISSOCK(pd->mode)) 
     419    { 
     420        errno = ENOTSOCK; 
     421        rc = -1; 
     422        goto errorout; 
     423    } 
     424    rc = pd->fsops->shutdown(pd->true_fd, how); 
     425errorout: 
    339426    return rc; 
    340427} 
     
    342429int socketpair (int d, int type, int protocol, int sv[2]) 
    343430{ 
    344     int rc; 
    345     pvfs_descriptor *pd; 
     431    int rc = 0; 
     432    pvfs_descriptor *pd0, *pd1; 
    346433    rc = glibc_ops.socketpair(d, type, protocol, sv); 
    347434    if (rc < 0) 
    348435    { 
    349         return rc; 
    350     } 
    351     pd = pvfs_alloc_descriptor(&glibc_ops); 
    352     pd->is_in_use = PVFS_FS; 
    353     pd->true_fd = sv[0]; 
    354     sv[0] = pd->fd; 
    355     pd = pvfs_alloc_descriptor(&glibc_ops); 
    356     pd->is_in_use = PVFS_FS; 
    357     pd->true_fd = sv[1]; 
    358     sv[1] = pd->fd; 
     436        goto errorout; 
     437    } 
     438    pd0 = pvfs_alloc_descriptor(&glibc_ops, sv[0]); 
     439    if (!pd0) 
     440    { 
     441        goto errorout; 
     442    } 
     443    pd1 = pvfs_alloc_descriptor(&glibc_ops, sv[1]); 
     444    if (!pd1) 
     445    { 
     446        pvfs_free_descriptor(pd0->fd); 
     447        errno = EMFILE; 
     448        rc = -1; 
     449        goto errorout; 
     450    } 
     451    pd0->mode |= S_IFSOCK; 
     452    pd1->mode |= S_IFSOCK; 
     453    sv[0] = pd0->true_fd; 
     454    sv[1] = pd1->true_fd; 
     455errorout: 
    359456    return rc; 
    360457} 
     
    376473        goto errorout; 
    377474    } 
    378     f0 = pvfs_alloc_descriptor(&glibc_ops); 
     475    f0 = pvfs_alloc_descriptor(&glibc_ops, fa[0]); 
    379476    if (!f0) 
    380477    { 
    381478        goto errorout; 
    382479    } 
    383     f1 = pvfs_alloc_descriptor(&glibc_ops); 
     480    f1 = pvfs_alloc_descriptor(&glibc_ops, fa[1]); 
    384481    if (!f1) 
    385482    { 
    386         pvfs_free_descriptor(f0); 
     483        pvfs_free_descriptor(f0->fd); 
    387484        errno = EMFILE; 
    388485        rc = -1; 
    389486        goto errorout; 
    390487    } 
    391     f0->true_fd = fa[0]; 
    392     filedes[0] = f0->fd; 
    393     f1->true_fd = fa[1]; 
    394     filedes[1] = f1->fd; 
    395     /* need to set mode and stuff appropriately */ 
     488    f0->mode |= S_IFSOCK; 
     489    f1->mode |= S_IFSOCK; 
     490    filedes[0] = f0->true_fd; 
     491    filedes[1] = f1->true_fd; 
    396492errorout: 
    397493    return rc; 
  • branches/Orange-Branch/src/client/usrint/stdio.c

    r8899 r9016  
    1010 *  PVFS2 user interface routines - implementation of stdio for pvfs 
    1111 */ 
     12/* this prevents headers from using inlines for 64 bit calls */ 
     13#ifdef _FILE_OFFSET_BITS 
     14#undef _FILE_OFFSET_BITS 
     15#endif 
     16 
    1217#include <usrint.h> 
    1318#include <dirent.h> 
    14  
    15 #define ISFLAGSET(s,f) ((stream->_flags & (f)) == (f)) 
     19#include <openfile-util.h> 
     20#include <stdio-ops.h> 
     21 
     22#define STDIO_DEBUG 0 
     23 
     24static void init_stdio(void); 
     25static struct stdio_ops_s stdio_ops; 
     26 
     27#define _P_IO_MAGIC     0xF0BD0000 
     28#define SETMAGIC(s,m)   do{(s)->_flags = (m) & _IO_MAGIC_MASK;}while(0) 
     29#define ISMAGICSET(s,m) (((s)->_flags & _IO_MAGIC_MASK) == (m)) 
     30#define SETFLAG(s,f)    do{(s)->_flags |= ((f) & ~_IO_MAGIC_MASK);}while(0) 
     31#define CLEARFLAG(s,f)  do{(s)->_flags &= ~((f) & ~_IO_MAGIC_MASK);}while(0) 
     32#define ISFLAGSET(s,f)  (((s)->_flags & (f)) == (f)) 
    1633 
    1734/* STDIO implementation - this gives users something to link to 
     
    2744 */ 
    2845struct __dirstream { 
    29     int flags;      /**< general flags field */ 
    30     int fileno;     /**< file dscriptor of open dir */ 
    31     char *buf_base; /**< pointer to beginning of buffer */ 
    32     char *buf_end;  /**< pointer to end of buffer */ 
    33     char *buf_act;  /**< pointer to end of active portion of buffer */ 
    34     char *buf_ptr;  /**< pointer to current position in buffer */ 
     46    int _flags;       /**< general flags field */ 
     47    int fileno;       /**< file dscriptor of open dir */ 
     48    struct dirent de; /**< pointer to dirent read by readdir */ 
     49    char *buf_base;   /**< pointer to beginning of buffer */ 
     50    char *buf_end;    /**< pointer to end of buffer */ 
     51    char *buf_act;    /**< pointer to end of active portion of buffer */ 
     52    char *buf_ptr;    /**< pointer to current position in buffer */ 
    3553}; 
    3654 
    37 #define DIRSTREAM_MAGIC 0xfd100000 
     55#define DIRSTREAM_MAGIC 0xFD100000 
    3856#define DIRBUFSIZE (512*1024) 
    3957#define ASIZE 256 
    4058#define MAXTRIES 16 /* arbitrary - how many tries to get a unique file name */ 
    4159 
     60/** These functions lock and unlock the stream structure 
     61 * 
     62 *  These are only called within our library, so we assume that the 
     63 *  stream is good, that it is our stream (and not glibc's) and we 
     64 *  check for the flag to see if the lock is being used. 
     65 */ 
     66 
     67static inline void lock_init_stream(FILE *stream) 
     68{ 
     69#ifdef _IO_MTSAFE_IO 
     70    if (ISFLAGSET(stream, _IO_USER_LOCK)) 
     71    { 
     72        _IO_lock_init(stream->_lock); 
     73    } 
     74#endif 
     75} 
     76 
     77static inline void lock_stream(FILE *stream) 
     78{ 
     79#ifdef _IO_MTSAFE_IO 
     80    if (ISFLAGSET(stream, _IO_USER_LOCK)) 
     81    { 
     82        _IO_lock_lock(stream->_lock); 
     83    } 
     84#endif 
     85} 
     86 
     87static inline int trylock_stream(FILE *stream) 
     88{ 
     89#ifdef _IO_MTSAFE_IO 
     90    if (ISFLAGSET(stream, _IO_USER_LOCK)) 
     91    { 
     92        return _IO_lock_try(stream->_lock); 
     93    } 
     94#else 
     95    return 0; 
     96#endif 
     97} 
     98 
     99static inline void unlock_stream(FILE *stream) 
     100{ 
     101#ifdef _IO_MTSAFE_IO 
     102    if (ISFLAGSET(stream, _IO_USER_LOCK)) 
     103    { 
     104        _IO_lock_unlock(stream->_lock); 
     105    } 
     106#endif 
     107} 
     108 
     109static inline void lock_fini_stream(FILE *stream) 
     110{ 
     111#ifdef _IO_MTSAFE_IO 
     112    if (ISFLAGSET(stream, _IO_USER_LOCK)) 
     113    { 
     114        _IO_lock_fini(stream->_lock); 
     115    } 
     116#endif 
     117} 
     118 
     119/** POSIX interface for user level locking of streams *. 
     120 * 
     121 */ 
     122void flockfile(FILE *stream) 
     123{ 
     124    lock_stream(stream); 
     125} 
     126 
     127int ftrylockfile(FILE *stream) 
     128{ 
     129    return trylock_stream(stream); 
     130} 
     131 
     132void funlockfile(FILE *stream) 
     133{ 
     134    unlock_stream(stream); 
     135} 
     136 
    42137/** This function coverts from stream style mode to ssycall style flags 
    43138 * 
     
    45140static int mode2flags(const char *mode) 
    46141{ 
    47     int i; 
    48     int flags; 
     142    int i = 0; 
     143    int flags = 0; 
    49144    int append = false, read = false, write = false, update = false; 
    50145    int exclusive = false; 
     
    110205        return -1; 
    111206    } 
    112     if (read) 
     207    if (read && update) 
     208    {  
     209        flags = O_RDWR;  
     210    } 
     211    else if(read) 
    113212    {  
    114213        flags = O_RDONLY;  
    115214    } 
    116     else if(read && update) 
     215    else if(write && update) 
    117216    {  
    118         flags = O_RDWR;  
    119     } 
     217        flags = O_RDWR | O_CREAT | O_TRUNC;  
     218    }  
    120219    else if(write) 
    121220    {  
    122221        flags = O_WRONLY | O_CREAT | O_TRUNC;  
     222    } 
     223    else if(append && update) 
     224    {  
     225        flags = O_RDWR | O_APPEND | O_CREAT;  
    123226    }  
    124     else if(write && update) 
    125     {  
    126         flags = O_RDWR | O_CREAT | O_TRUNC;  
    127     } 
    128     else if(append) 
     227    else if (append) 
    129228    {  
    130229        flags = O_WRONLY | O_APPEND | O_CREAT;  
    131     }  
    132     else if (append && update) 
    133     {  
    134         flags = O_RDWR | O_APPEND | O_CREAT;  
    135230    } 
    136231    if (exclusive) /* check this regardless of the above */ 
     
    173268{ 
    174269    /* set up stream here */ 
    175     stream->_flags = _IO_MAGIC; 
     270    SETMAGIC(stream, _P_IO_MAGIC); 
    176271    if (!(flags & O_WRONLY)) 
    177         stream->_flags |= _IO_NO_READS; 
     272        SETFLAG(stream, _IO_NO_READS); 
    178273    if (!(flags & O_RDONLY)) 
    179         stream->_flags |= _IO_NO_WRITES; 
     274        SETFLAG(stream, _IO_NO_WRITES); 
    180275    /* set up default buffering here */ 
    181276    stream->_IO_buf_base   = (char *)malloc(bufsize); 
     
    215310    memset(newfile, 0, sizeof(FILE)); 
    216311 
     312    /* initize lock for this stream */ 
     313    SETFLAG(newfile, _IO_USER_LOCK); 
     314    lock_init_stream(newfile); 
     315 
    217316    newfile->_fileno = fd; 
    218317    rc = init_stream(newfile, flags, PVFS_BUFSIZE); 
     
    232331    int fd = 0; 
    233332    int flags = 0; 
     333    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     334    { 
     335        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     336        { 
     337            init_stdio(); 
     338            return stdio_ops.freopen(path, mode, stream); 
     339        } 
     340        errno = EINVAL; 
     341        return NULL; 
     342    } 
     343    lock_stream(stream); 
    234344    /* see if stream is in use - if so close the file */ 
    235345    if (stream->_fileno > -1) 
     
    239349        if (rc == -1) 
    240350        { 
     351            unlock_stream(stream); 
    241352            return NULL; 
    242353        } 
     
    250361    if (fd == -1) 
    251362    { 
     363        unlock_stream(stream); 
    252364        return NULL; 
    253365    } 
     
    259371    init_stream(stream, flags, PVFS_BUFSIZE); 
    260372 
     373    unlock_stream(stream); 
    261374    return stream; 
    262375} 
     
    276389size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) 
    277390{ 
     391    int rc = 0; 
     392 
     393    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     394    { 
     395        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     396        { 
     397            init_stdio(); 
     398            return stdio_ops.fwrite(ptr, size, nmemb, stream); 
     399        } 
     400        errno = EINVAL; 
     401        return -1; 
     402    } 
     403    lock_stream(stream); 
     404    rc = fwrite_unlocked(ptr, size, nmemb, stream); 
     405    unlock_stream(stream); 
     406    return rc; 
     407} 
     408 
     409size_t fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, FILE *stream) 
     410{ 
    278411    off64_t rsz, rsz_buf, rsz_extra; 
    279412    int rc; 
    280413 
    281     if (!stream || !ISFLAGSET(stream, _IO_MAGIC) || 
    282             ISFLAGSET(stream, _IO_NO_WRITES) || 
    283             !ptr || size <= 0 || nmemb <= 0) 
     414    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     415    { 
     416        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     417        { 
     418            init_stdio(); 
     419            return stdio_ops.fwrite(ptr, size, nmemb, stream); 
     420        } 
     421        errno = EINVAL; 
     422        return -1; 
     423    } 
     424    if (!ptr || size <= 0 || nmemb <= 0) 
    284425    { 
    285426        errno = EINVAL; 
     
    290431    if (!ISFLAGSET(stream, _IO_CURRENTLY_PUTTING)) 
    291432    { 
    292         /* write buffer back */ 
    293         rc = write(stream->_fileno, stream->_IO_write_base, 
    294                 stream->_IO_write_ptr - stream->_IO_write_base);  
    295         if (rc == -1) 
    296         { 
    297             stream->_flags |= _IO_ERR_SEEN; 
    298             return -1; 
    299         } 
    300433        /* reset read pointer */ 
    301434        stream->_IO_read_ptr = stream->_IO_read_end; 
    302435        /* set flag */ 
    303         stream->_flags |= _IO_CURRENTLY_PUTTING; 
     436        SETFLAG(stream, _IO_CURRENTLY_PUTTING); 
    304437        /* indicate read buffer empty */ 
    305438        stream->_IO_read_end = stream->_IO_read_base; 
     
    324457    { 
    325458        /* buffer is full - write the current buffer */ 
     459#if STDIO_DEBUG 
     460        fprintf(stderr,"fwrite writing %d bytes to offset %d\n", 
     461                    (int)(stream->_IO_write_ptr - stream->_IO_write_base), 
     462                    (int)lseek(stream->_fileno, 0, SEEK_CUR)); 
     463#endif 
    326464        rc = write(stream->_fileno, stream->_IO_write_base, 
    327465                        stream->_IO_write_ptr - stream->_IO_write_base); 
    328466        if (rc == -1) 
    329467        { 
    330             stream->_flags |= _IO_ERR_SEEN; 
     468            SETFLAG(stream, _IO_ERR_SEEN); 
    331469            return -1; 
    332470        } 
     
    336474        if(rsz_extra > stream->_IO_buf_end - stream->_IO_buf_base) 
    337475        { 
     476#if STDIO_DEBUG 
     477            fprintf(stderr,"fwrite writing %d bytes to offset %d\n", 
     478                    (int)rsz_extra, 
     479                    (int)lseek(stream->_fileno, 0, SEEK_CUR)); 
     480#endif 
    338481            /* write data directly */ 
    339482            rc = write(stream->_fileno, ptr + rsz_buf, rsz_extra); 
    340483            if (rc == -1) 
    341484            { 
    342                 stream->_flags |= _IO_ERR_SEEN; 
     485                SETFLAG(stream, _IO_ERR_SEEN); 
    343486                return -1; 
    344487            } 
     
    359502size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) 
    360503{ 
    361     int fd; 
     504    int rc = 0; 
     505 
     506    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     507    { 
     508        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     509        { 
     510            init_stdio(); 
     511            return stdio_ops.fwrite(ptr, size, nmemb, stream); 
     512        } 
     513        errno = EINVAL; 
     514        return -1; 
     515    } 
     516    lock_stream(stream); 
     517    rc = fread_unlocked(ptr, size, nmemb, stream); 
     518    unlock_stream(stream); 
     519    return rc; 
     520} 
     521 
     522size_t fread_unlocked(void *ptr, size_t size, size_t nmemb, FILE *stream) 
     523{ 
    362524    int rsz, rsz_buf, rsz_extra; 
    363525    int bytes_read; 
    364526    int rc; 
    365527 
    366     if (!stream || !ISFLAGSET(stream, _IO_MAGIC) || 
    367             ISFLAGSET(stream, _IO_NO_READS) || 
    368             !ptr || size < 0 || nmemb < 0) 
     528    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     529    { 
     530        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     531        { 
     532            init_stdio(); 
     533            return stdio_ops.fread(ptr, size, nmemb, stream); 
     534        } 
     535        errno = EINVAL; 
     536        return -1; 
     537    } 
     538    if (!ptr || size < 0 || nmemb < 0) 
    369539    { 
    370540        errno = EINVAL; 
     
    376546    { 
    377547        /* write buffer back */ 
     548#if STDIO_DEBUG 
     549        fprintf(stderr,"fread writing %d bytes to offset %d\n", 
     550                    (int)(stream->_IO_write_ptr - stream->_IO_write_base), 
     551                    (int)lseek(stream->_fileno, 0, SEEK_CUR)); 
     552#endif 
    378553        rc = write(stream->_fileno, stream->_IO_write_base, 
    379554                stream->_IO_write_ptr - stream->_IO_write_base);  
    380555        if (rc == -1) 
    381556        { 
    382             stream->_flags |= _IO_ERR_SEEN; 
     557            SETFLAG(stream, _IO_ERR_SEEN); 
    383558            return -1; 
    384559        } 
     
    386561        stream->_IO_write_ptr = stream->_IO_write_base; 
    387562        /* clear flag */ 
    388         stream->_flags &= ~_IO_CURRENTLY_PUTTING; 
     563        CLEARFLAG(stream, _IO_CURRENTLY_PUTTING); 
    389564        /* indicate read buffer empty */ 
    390565        stream->_IO_read_end = stream->_IO_read_base; 
     
    401576        if (bytes_read == -1) 
    402577        { 
    403             stream->_flags |= _IO_ERR_SEEN; 
     578            SETFLAG(stream, _IO_ERR_SEEN); 
    404579            return -1; 
    405580        } 
     
    440615                if (bytes_read == -1) 
    441616                { 
    442                     stream->_flags |= _IO_ERR_SEEN; 
     617                    SETFLAG(stream, _IO_ERR_SEEN); 
    443618                    return -1; 
    444619                } 
     
    450625                    if (bytes_read == -1) 
    451626                    { 
    452                         stream->_flags |= _IO_ERR_SEEN; 
     627                        SETFLAG(stream, _IO_ERR_SEEN); 
    453628                        return -1; 
    454629                    } 
     
    458633                } 
    459634                /* have read to EOF */ 
    460                 stream->_flags |= _IO_EOF_SEEN; 
     635                SETFLAG(stream, _IO_EOF_SEEN); 
    461636                return rsz_buf + bytes_read; 
    462637            } 
     
    466641            if (bytes_read == -1) 
    467642            { 
    468                 stream->_flags |= _IO_ERR_SEEN; 
     643                SETFLAG(stream, _IO_ERR_SEEN); 
    469644                return -1; 
    470645            } 
     
    481656            if (rsz_buf + rsz_extra < rsz) 
    482657            { 
    483                 stream->_flags |= _IO_EOF_SEEN; 
     658                SETFLAG(stream, _IO_EOF_SEEN); 
    484659            } 
    485660            return rsz_buf + rsz_extra; 
     
    488663        { 
    489664            /* at EOF so return bytes read */ 
    490             stream->_flags |= _IO_EOF_SEEN; 
     665            SETFLAG(stream, _IO_EOF_SEEN); 
    491666            return rsz_buf; 
    492667        } 
     
    501676int fclose(FILE *stream) 
    502677{ 
    503     int rc; 
    504     if (!stream || !ISFLAGSET(stream, _IO_MAGIC)) 
    505     { 
    506         errno = EINVAL; 
    507         return -1; 
    508     } 
     678    int rc = 0; 
     679    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     680    { 
     681        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     682        { 
     683            init_stdio(); 
     684            return stdio_ops.fclose(stream); 
     685        } 
     686        errno = EINVAL; 
     687        return -1; 
     688    } 
     689    lock_stream(stream); 
    509690    /* write any pending data */ 
    510691    if (ISFLAGSET(stream, _IO_CURRENTLY_PUTTING)) 
     
    512693        if (stream->_IO_write_ptr > stream->_IO_write_base) 
    513694        { 
    514             rc = write(stream->_fileno, stream->_IO_write_ptr, 
    515                     stream->_IO_write_ptr - stream->_IO_write_base); 
     695#if STDIO_DEBUG 
     696            fprintf(stderr,"fclose writing %d bytes to offset %d\n", 
     697                    (int)(stream->_IO_write_ptr - stream->_IO_write_base), 
     698                    (int)lseek(stream->_fileno, 0, SEEK_CUR)); 
     699#endif 
     700            rc = write(stream->_fileno, stream->_IO_write_base, 
     701                        stream->_IO_write_ptr - stream->_IO_write_base); 
    516702            if (rc == -1)    
    517703            { 
     704                SETFLAG(stream, _IO_ERR_SEEN); 
    518705                return -1; 
    519706            } 
     
    527714    if (!ISFLAGSET(stream, _IO_DELETE_DONT_CLOSE)) 
    528715    { 
    529         return close(stream->_fileno); 
    530     } 
     716        rc = close(stream->_fileno); 
     717    } 
     718    stream->_flags = 0; 
     719    /* can stream be locked here */ 
     720    lock_fini_stream(stream); 
    531721    free(stream); 
    532     return 0; 
     722    return rc; 
    533723} 
    534724 
     
    551741{ 
    552742    int rc = 0; 
    553     if (!stream || !ISFLAGSET(stream, _IO_MAGIC)) 
    554     { 
    555         errno = EINVAL; 
    556         return -1; 
    557     } 
    558     /* if not just getting the position */ 
     743    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     744    { 
     745        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     746        { 
     747            init_stdio(); 
     748            return stdio_ops.fseek64(stream, offset, whence); 
     749        } 
     750        errno = EINVAL; 
     751        return -1; 
     752    } 
     753    lock_stream(stream); 
     754    /* if actually changing the position */ 
    559755    if ((offset != 0L) || (whence != SEEK_CUR)) 
    560756    { 
    561757        int64_t filepos, fileend; 
     758        struct stat sbuf; 
    562759        filepos = lseek64(stream->_fileno, 0, SEEK_CUR); 
    563         fileend = lseek64(stream->_fileno, 0, SEEK_END); 
     760        /* should fileend include stuff in write buffer ??? */ 
     761        rc = fstat(stream->_fileno, &sbuf); 
     762        if (rc < 0) 
     763        { 
     764            SETFLAG(stream, _IO_ERR_SEEN); 
     765            rc = -1; 
     766            goto exitout; 
     767        } 
     768        fileend = sbuf.st_size; 
    564769        /* figure out if we are only seeking within the */ 
    565770        /* bounds of the current buffer to minimize */ 
     
    570775        { 
    571776            stream->_IO_write_ptr += offset; 
    572             return 0; 
     777            /* should we zero out buffer if past eof ??? */ 
     778            rc = 0; 
     779            goto exitout; 
    573780        } 
    574781        if (whence == SEEK_CUR && !ISFLAGSET(stream, _IO_CURRENTLY_PUTTING) && 
     
    577784        { 
    578785            stream->_IO_read_ptr += offset; 
    579             return 0; 
     786            rc = 0; 
     787            goto exitout; 
    580788        } 
    581789        if (whence == SEEK_SET && ISFLAGSET(stream, _IO_CURRENTLY_PUTTING) && 
     
    584792        { 
    585793            stream->_IO_write_ptr += offset - filepos; 
    586             return 0; 
     794            /* should we zero out buffer if past eof ??? */ 
     795            rc = 0; 
     796            goto exitout; 
    587797        } 
    588798        if (whence == SEEK_SET && !ISFLAGSET(stream, _IO_CURRENTLY_PUTTING) && 
     
    591801        { 
    592802            stream->_IO_read_ptr += offset - filepos; 
    593             return 0; 
     803            rc = 0; 
     804            goto exitout; 
    594805        } 
    595806        if (whence == SEEK_END && ISFLAGSET(stream, _IO_CURRENTLY_PUTTING) && 
     
    599810        { 
    600811            stream->_IO_write_ptr += (fileend - offset) - filepos; 
    601             return 0; 
     812            /* should we zero out buffer if past eof ??? */ 
     813            rc = 0; 
     814            goto exitout; 
    602815        } 
    603816        if (whence == SEEK_END && !ISFLAGSET(stream, _IO_CURRENTLY_PUTTING) && 
     
    607820        { 
    608821            stream->_IO_read_ptr += (fileend - offset) - filepos; 
    609             return 0; 
     822            rc = 0; 
     823            goto exitout; 
    610824        } 
    611825        /* at this point the seek is beyond the current buffer */ 
     
    615829        { 
    616830            /* write buffer back */ 
     831#if STDIO_DEBUG 
     832            fprintf(stderr,"fseek writing %d bytes to offset %d\n", 
     833                    (int)(stream->_IO_write_ptr - stream->_IO_write_base), 
     834                    (int)lseek(stream->_fileno, 0, SEEK_CUR)); 
     835#endif 
    617836            rc = write(stream->_fileno, stream->_IO_write_base, 
    618837                       stream->_IO_write_ptr - stream->_IO_write_base);  
    619838            if (rc < 0) 
    620839            { 
    621                 return rc; 
     840                SETFLAG(stream, _IO_ERR_SEEN); 
     841                goto exitout; 
    622842            } 
    623843            /* reset write pointer */ 
     
    631851            stream->_IO_read_ptr = stream->_IO_read_end; 
    632852        } 
    633         lseek64(stream->_fileno, offset, whence); 
    634     } 
    635     /* seek to current position, no change */ 
    636     return 0; 
     853        rc = lseek64(stream->_fileno, offset, whence); 
     854#if STDIO_DEBUG 
     855        fprintf(stderr,"fseek seeks to offset %d\n", 
     856                    (int)lseek(stream->_fileno, 0, SEEK_CUR)); 
     857#endif 
     858        if (rc < 0) 
     859        { 
     860            SETFLAG(stream, _IO_ERR_SEEN); 
     861            goto exitout; 
     862        } 
     863    } 
     864exitout: 
     865    /* successful call */ 
     866    lock_stream(stream); 
     867    CLEARFLAG(stream, _IO_EOF_SEEN); 
     868    return rc; 
    637869} 
    638870 
     
    652884{ 
    653885    fseek64(stream, 0L, SEEK_SET); 
     886    CLEARFLAG(stream, _IO_ERR_SEEN); 
    654887} 
    655888 
     
    665898{ 
    666899    int64_t filepos; 
    667     if (!stream || !ISFLAGSET(stream, _IO_MAGIC)) 
    668     { 
     900    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     901    { 
     902        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     903        { 
     904            init_stdio(); 
     905            return stdio_ops.ftell64(stream); 
     906        } 
    669907        errno = EINVAL; 
    670908        return -1; 
     
    698936int fflush(FILE *stream) 
    699937{ 
     938    int rc = 0; 
     939 
     940    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     941    { 
     942        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     943        { 
     944            init_stdio(); 
     945            return stdio_ops.fflush(stream); 
     946        } 
     947        errno = EINVAL; 
     948        return -1; 
     949    } 
     950    lock_stream(stream); 
     951    rc = fflush_unlocked(stream); 
     952    unlock_stream(stream); 
     953    return rc; 
     954} 
     955 
     956int fflush_unlocked(FILE *stream) 
     957{ 
    700958    int rc; 
    701     if (!stream || !ISFLAGSET(stream, _IO_MAGIC)) 
    702     { 
    703         errno = EBADF; 
     959    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     960    { 
     961        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     962        { 
     963            init_stdio(); 
     964            return stdio_ops.fflush(stream); 
     965        } 
     966        errno = EINVAL; 
    704967        return -1; 
    705968    } 
     
    709972    { 
    710973        /* write buffer back */ 
     974#if STDIO_DEBUG 
     975        fprintf(stderr,"fflush writing %d bytes to offset %d\n", 
     976                    (int)(stream->_IO_write_ptr - stream->_IO_write_base), 
     977                    (int)lseek(stream->_fileno, 0, SEEK_CUR)); 
     978#endif 
    711979        rc = write(stream->_fileno, stream->_IO_write_base, 
    712980                stream->_IO_write_ptr - stream->_IO_write_base);  
    713981        if (rc < 0) 
    714982        { 
    715             stream->_flags |= _IO_ERR_SEEN; 
     983            SETFLAG(stream, _IO_ERR_SEEN); 
    716984            return rc; 
    717985        } 
     
    727995int fputc(int c, FILE *stream) 
    728996{ 
     997    int rc = 0; 
     998 
     999    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     1000    { 
     1001        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     1002        { 
     1003            init_stdio(); 
     1004            return stdio_ops.fputc(c, stream); 
     1005        } 
     1006        errno = EINVAL; 
     1007        return -1; 
     1008    } 
     1009    lock_stream(stream); 
     1010    rc = fputc_unlocked(c, stream); 
     1011    unlock_stream(stream); 
     1012    return rc; 
     1013} 
     1014 
     1015int fputc_unlocked(int c, FILE *stream) 
     1016{ 
    7291017    int rc; 
    730     rc = fwrite(&c, 1, 1, stream); 
     1018    rc = fwrite_unlocked(&c, 1, 1, stream); 
    7311019    if (ferror(stream)) 
    7321020    { 
     
    7401028 */ 
    7411029int fputs(const char *s, FILE *stream) 
     1030{ 
     1031    int rc = 0; 
     1032 
     1033    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     1034    { 
     1035        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     1036        { 
     1037            init_stdio(); 
     1038            return stdio_ops.fputs(s, stream); 
     1039        } 
     1040        errno = EINVAL; 
     1041        return -1; 
     1042    } 
     1043    lock_stream(stream); 
     1044    rc = fputs_unlocked(s, stream); 
     1045    unlock_stream(stream); 
     1046    return rc; 
     1047} 
     1048 
     1049int fputs_unlocked(const char *s, FILE *stream) 
    7421050{ 
    7431051    size_t len; 
     
    7491057    } 
    7501058    len = strlen(s); 
    751     rc = fwrite(s, len, 1, stream); 
     1059    rc = fwrite_unlocked(s, len, 1, stream); 
    7521060    if (ferror(stream)) 
    7531061    { 
     
    7651073} 
    7661074 
     1075int putc_unlocked(int c, FILE *stream) 
     1076{ 
     1077    return fputc_unlocked(c, stream); 
     1078} 
     1079 
    7671080/** 
    7681081 * putchar wrapper 
     
    7711084{ 
    7721085    return fputc(c, stdout); 
     1086} 
     1087 
     1088int putchar_unlocked(int c) 
     1089{ 
     1090    return fputc_unlocked(c, stdout); 
    7731091} 
    7741092 
     
    7921110char *fgets(char *s, int size, FILE *stream) 
    7931111{ 
     1112    char *rc = NULL; 
     1113 
     1114    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     1115    { 
     1116        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     1117        { 
     1118            init_stdio(); 
     1119            return stdio_ops.fgets(s, size, stream); 
     1120        } 
     1121        errno = EINVAL; 
     1122        return NULL; 
     1123    } 
     1124    lock_stream(stream); 
     1125    rc = fgets_unlocked(s, size, stream); 
     1126    unlock_stream(stream); 
     1127    return rc; 
     1128} 
     1129 
     1130char *fgets_unlocked(char *s, int size, FILE *stream) 
     1131{ 
    7941132    char c, *p; 
    7951133 
     
    8071145    size--; 
    8081146    do { 
    809         *p++ = c = fgetc(stream); 
    810     } while (--size && c != '\n' && !feof(stream) && !ferror(stream)); 
    811     if (ferror(stream)) 
     1147        *p++ = c = fgetc_unlocked(stream); 
     1148    } while (--size && c != '\n' && !feof_unlocked(stream) 
     1149                    && !ferror_unlocked(stream)); 
     1150    if (ferror_unlocked(stream)) 
    8121151    { 
    8131152        return NULL; 
     
    8321171} 
    8331172 
     1173int fgetc_unlocked(FILE *stream) 
     1174{ 
     1175    int rc, ch; 
     1176 
     1177    rc = fread_unlocked(&ch, 1, 1, stream); 
     1178    if (ferror(stream)) 
     1179    { 
     1180        return EOF; 
     1181    } 
     1182    return ch; 
     1183} 
     1184 
    8341185/** 
    8351186 * getc wrapper 
     
    8391190    return fgetc(stream); 
    8401191} 
     1192int getc_unlocked(FILE *stream) 
     1193{ 
     1194    return fgetc_unlocked(stream); 
     1195} 
    8411196 
    8421197/** 
     
    8461201{ 
    8471202    return fgetc(stdin); 
     1203} 
     1204 
     1205int getchar_unlocked(void) 
     1206{ 
     1207    return fgetc_unlocked(stdin); 
    8481208} 
    8491209 
     
    9641324void clearerr (FILE *stream) 
    9651325{ 
    966     if (!stream || !ISFLAGSET(stream, _IO_MAGIC)) 
    967     { 
     1326    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     1327    { 
     1328        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     1329        { 
     1330            init_stdio(); 
     1331            stdio_ops.clearerr(stream); 
     1332            return; 
     1333        } 
    9681334        return; 
    9691335    } 
    970     stream->_flags &= ~_IO_ERR_SEEN; 
    971     stream->_flags &= ~_IO_EOF_SEEN; 
     1336    lock_stream(stream); 
     1337    CLEARFLAG(stream, _IO_ERR_SEEN); 
     1338    CLEARFLAG(stream, _IO_EOF_SEEN); 
     1339    unlock_stream(stream); 
     1340} 
     1341 
     1342void clearerr_unlocked (FILE *stream) 
     1343{ 
     1344    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     1345    { 
     1346        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     1347        { 
     1348            init_stdio(); 
     1349            stdio_ops.clearerr(stream); 
     1350            return; 
     1351        } 
     1352        return; 
     1353    } 
     1354    CLEARFLAG(stream, _IO_ERR_SEEN); 
     1355    CLEARFLAG(stream, _IO_EOF_SEEN); 
    9721356} 
    9731357 
     
    9771361int feof (FILE *stream) 
    9781362{ 
    979     if (!stream || !ISFLAGSET(stream, _IO_MAGIC)) 
    980     { 
     1363    int rc = 0; 
     1364 
     1365    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     1366    { 
     1367        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     1368        { 
     1369            init_stdio(); 
     1370            return stdio_ops.feof(stream); 
     1371        } 
     1372        errno = EINVAL; 
     1373        return -1; 
     1374    } 
     1375    lock_stream(stream); 
     1376    rc = ISFLAGSET(stream, _IO_EOF_SEEN); 
     1377    unlock_stream(stream); 
     1378    return rc; 
     1379} 
     1380 
     1381int feof_unlocked (FILE *stream) 
     1382{ 
     1383    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     1384    { 
     1385        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     1386        { 
     1387            init_stdio(); 
     1388            return stdio_ops.feof(stream); 
     1389        } 
    9811390        errno = EBADF; 
    9821391        return -1; 
    9831392    } 
    984     return stream->_flags & _IO_EOF_SEEN; 
     1393    return ISFLAGSET(stream, _IO_EOF_SEEN); 
    9851394} 
    9861395 
     
    9901399int ferror (FILE *stream) 
    9911400{ 
    992     if (!stream || !ISFLAGSET(stream, _IO_MAGIC)) 
    993     { 
     1401    int rc = 0; 
     1402 
     1403    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     1404    { 
     1405        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     1406        { 
     1407            init_stdio(); 
     1408            return stdio_ops.ferror(stream); 
     1409        } 
     1410        errno = EINVAL; 
     1411        return -1; 
     1412    } 
     1413    lock_stream(stream); 
     1414    rc = ISFLAGSET(stream, _IO_ERR_SEEN); 
     1415    unlock_stream(stream); 
     1416    return rc; 
     1417} 
     1418 
     1419int ferror_unlocked (FILE *stream) 
     1420{ 
     1421    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     1422    { 
     1423        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     1424        { 
     1425            init_stdio(); 
     1426            return stdio_ops.ferror(stream); 
     1427        } 
    9941428        errno = EBADF; 
    9951429        return -1; 
    9961430    } 
    997     return stream->_flags & _IO_ERR_SEEN; 
     1431    return ISFLAGSET(stream, _IO_ERR_SEEN); 
    9981432} 
    9991433 
     
    10031437int fileno (FILE *stream) 
    10041438{ 
    1005     if (!stream || !ISFLAGSET(stream, _IO_MAGIC)) 
    1006     { 
     1439    int rc = 0; 
     1440 
     1441    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     1442    { 
     1443        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     1444        { 
     1445            init_stdio(); 
     1446            return stdio_ops.fileno(stream); 
     1447        } 
     1448        errno = EINVAL; 
     1449        return -1; 
     1450    } 
     1451    lock_stream(stream); 
     1452    rc = stream->_fileno; 
     1453    unlock_stream(stream); 
     1454    return rc; 
     1455} 
     1456 
     1457int fileno_unlocked (FILE *stream) 
     1458{ 
     1459    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     1460    { 
     1461        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     1462        { 
     1463            init_stdio(); 
     1464            return stdio_ops.fileno(stream); 
     1465        } 
    10071466        errno = EBADF; 
    10081467        return -1; 
     
    10511510/** 
    10521511 * 
    1053  * This should only be called on a stream that has ben opened 
    1054  * but not used so we can assume any exitinf buff is not dirty 
     1512 * This should only be called on a stream that has been opened 
     1513 * but not used so we can assume any exiting buff is not dirty 
    10551514 */ 
    10561515int setvbuf (FILE *stream, char *buf, int mode, size_t size) 
    10571516{ 
    1058     if (!stream || !ISFLAGSET(stream, _IO_MAGIC)) 
    1059     { 
    1060         errno = EBADF; 
     1517    if (!stream || !ISMAGICSET(stream, _P_IO_MAGIC)) 
     1518    { 
     1519        if (stream && ISMAGICSET(stream, _IO_MAGIC)) 
     1520        { 
     1521            init_stdio(); 
     1522            return stdio_ops.setvbuf(stream, buf, mode, size); 
     1523        } 
     1524        errno = EINVAL; 
    10611525        return -1; 
    10621526    } 
     
    10651529    { 
    10661530        /* fread or fwrite has been called */ 
    1067         errno - EINVAL; 
    1068         return -1; 
    1069     } 
     1531        errno = EINVAL; 
     1532        return -1; 
     1533    } 
     1534    lock_stream(stream); 
    10701535    switch (mode) 
    10711536    { 
     
    10741539        break; 
    10751540    case _IOLBF : /* line buffered */ 
    1076         stream->_flags |= _IO_LINE_BUF; /* TODO: This is not implemented */ 
     1541        SETFLAG(stream, _IO_LINE_BUF); /* TODO: This is not implemented */ 
    10771542        break; 
    10781543    case _IONBF : /* not buffered */ 
    1079         stream->_flags |= _IO_UNBUFFERED; /* TODO: This is not implemented */ 
     1544        SETFLAG(stream, _IO_UNBUFFERED); /* TODO: This is not implemented */ 
    10801545        break; 
    10811546    default : 
    10821547        errno = EINVAL; 
     1548        unlock_stream(stream); 
    10831549        return -1; 
    10841550    } 
    10851551    if (buf && size > 0) 
    10861552    { 
    1087         stream->_flags |= _IO_USER_BUF; 
     1553        SETFLAG(stream, _IO_USER_BUF); 
    10881554        free(stream->_IO_buf_base); 
    10891555        stream->_IO_buf_base   = buf; 
     
    10961562        stream->_IO_write_end  = stream->_IO_buf_end; 
    10971563    } 
     1564    unlock_stream(stream); 
     1565    return 0; 
    10981566} 
    10991567 
     
    12011669    } 
    12021670    fd = open(name, O_RDONLY|O_DIRECTORY); 
    1203     if (fd == -1); 
     1671    if (fd < 0) 
    12041672    { 
    12051673        return NULL; 
     
    12191687        return NULL; 
    12201688    } 
    1221     dstr->flags = DIRSTREAM_MAGIC; 
     1689    SETMAGIC(dstr, DIRSTREAM_MAGIC); 
    12221690    dstr->fileno = fd; 
    12231691    dstr->buf_base = (char *)malloc(DIRBUFSIZE); 
     
    12251693    dstr->buf_act = dstr->buf_base; 
    12261694    dstr->buf_ptr = dstr->buf_base; 
     1695    return dstr; 
    12271696} 
    12281697 
     
    12321701int dirfd (DIR *dir) 
    12331702{ 
    1234     if (!dir || !(dir->flags == DIRSTREAM_MAGIC)) 
     1703    if (!dir || !ISMAGICSET(dir, DIRSTREAM_MAGIC)) 
    12351704    { 
    12361705        errno = EBADF; 
     
    12461715{ 
    12471716    struct dirent64 *de64; 
    1248     /* this buffer is so we can return the smaller struct for 
    1249      * single use as is the prerogative of the call.  This 
    1250      * approach sucks, not reentrant TODO: find a better way 
    1251      */ 
    1252     static struct dirent *de = NULL; 
    1253  
    1254     if (!de) 
    1255     { 
    1256         de = (struct dirent *)malloc(sizeof(struct dirent)); 
    1257         if (!de) 
    1258         { 
    1259             return NULL; 
    1260         } 
    1261     } 
    12621717 
    12631718    de64 = readdir64(dir); 
     
    12661721        return NULL; 
    12671722    } 
    1268     memcpy(de->d_name, de64->d_name, 256); 
    1269     de->d_ino = de64->d_ino; 
    1270     /* these are Linux specific fields from the dirent */ 
    1271 #if 1 
    1272     de->d_off = de64->d_off; 
    1273     de->d_reclen = de64->d_reclen; 
    1274     de->d_type = de64->d_type; 
     1723    memcpy(dir->de.d_name, de64->d_name, 256); 
     1724    dir->de.d_ino = de64->d_ino; 
     1725    /* these are wsystem specific fields from the dirent */ 
     1726#ifdef _DIRENT_HAVE_D_NAMELEN 
     1727    dir->de.d_namelen = strnlen(de64->d_name, 256); 
    12751728#endif 
    1276     return de; 
     1729#ifdef _DIRENT_HAVE_D_OFF 
     1730    dir->de.d_off = de64->d_off; 
     1731#endif 
     1732#ifdef _DIRENT_HAVE_D_RECLEN 
     1733    dir->de.d_reclen = de64->d_reclen; 
     1734#endif 
     1735#ifdef _DIRENT_HAVE_D_TYPE 
     1736    dir->de.d_type = de64->d_type; 
     1737#endif 
     1738    return &dir->de; 
    12771739} 
    12781740 
    12791741/** 
    12801742 * reads a single dirent64 in buffered mode from a stream 
    1281  */ 
     1743 * 
     1744 * getdents is not defined in libc, though it is a linux 
     1745 * system call and we define it in the usr lib 
     1746 */ 
     1747 
     1748int getdents(int fd, struct dirent *buf, size_t size); 
     1749int getdents64(int fd, struct dirent64 *buf, size_t size); 
     1750 
    12821751struct dirent64 *readdir64 (DIR *dir) 
    12831752{ 
    12841753    struct dirent64 *rval; 
    1285     if (!dir || !(dir->flags == DIRSTREAM_MAGIC)) 
     1754    if (!dir || !ISMAGICSET(dir, DIRSTREAM_MAGIC)) 
    12861755    { 
    12871756        errno = EBADF; 
     
    12911760    { 
    12921761        int bytes_read; 
    1293         /* read a block of dirents into the buffer */ 
    1294         bytes_read = getdents(dir->fileno, dir->buf_base, 
     1762        /* read a block of dirent64s into the buffer */ 
     1763        bytes_read = getdents64(dir->fileno, (struct dirent64 *)dir->buf_base, 
    12951764                             (dir->buf_end - dir->buf_base)); 
    12961765        dir->buf_act = dir->buf_base + bytes_read; 
     
    13081777{ 
    13091778    off_t filepos; 
    1310     if (!dir || !(dir->flags == DIRSTREAM_MAGIC)) 
     1779    if (!dir || !ISMAGICSET(dir, DIRSTREAM_MAGIC)) 
    13111780    { 
    13121781        errno = EBADF; 
     
    13271796 
    13281797/** 
    1329  * seeks in a direcotry stream 
     1798 * seeks in a directory stream 
    13301799 */ 
    13311800void seekdir (DIR *dir, off_t offset) 
    13321801{ 
    13331802    off_t filepos; 
    1334     if (!dir || !(dir->flags == DIRSTREAM_MAGIC)) 
     1803    if (!dir || !ISMAGICSET(dir, DIRSTREAM_MAGIC)) 
    13351804    { 
    13361805        errno = EBADF; 
     
    13571826{ 
    13581827    off_t filepos; 
    1359     if (!dir || !(dir->flags == DIRSTREAM_MAGIC)) 
     1828    if (!dir || !ISMAGICSET(dir, DIRSTREAM_MAGIC)) 
    13601829    { 
    13611830        errno = EBADF; 
     
    13751844int closedir (DIR *dir) 
    13761845{ 
    1377     if (!dir || !(dir->flags == DIRSTREAM_MAGIC)) 
     1846    if (!dir || !ISMAGICSET(dir, DIRSTREAM_MAGIC)) 
    13781847    { 
    13791848        errno = EBADF; 
     
    13811850    } 
    13821851    free(dir->buf_base); 
    1383     dir->flags = 0; 
     1852    dir->_flags = 0; 
    13841853    free(dir); 
    13851854    return 0; 
     
    14521921 * pass in a flag to control the copy of the dirent into the array 
    14531922 */ 
    1454 int scandir64 (const char *dir, struct dirent64 ***namelist, 
    1455              int(*filter)(const struct dirent64 *), 
    1456              int(*compar)(const void *, const void *)) 
     1923int scandir64 (const char *dir, 
     1924               struct dirent64 ***namelist, 
     1925               int(*filter)(const struct dirent64 *), 
     1926               int(*compar)(const void *, const void *)) 
    14571927{ 
    14581928    struct dirent64 *de; 
     
    15101980} 
    15111981 
     1982static void init_stdio(void) 
     1983{ 
     1984    static int init_flag = 0; 
     1985    if (init_flag) 
     1986    { 
     1987        return; 
     1988    } 
     1989    init_flag = 1; 
     1990    stdio_ops.fopen = dlsym(RTLD_NEXT, "fopen" ); 
     1991    stdio_ops.fdopen = dlsym(RTLD_NEXT, "fdopen" ); 
     1992    stdio_ops.freopen = dlsym(RTLD_NEXT, "freopen" ); 
     1993    stdio_ops.fwrite = dlsym(RTLD_NEXT, "fwrite" ); 
     1994    stdio_ops.fread = dlsym(RTLD_NEXT, "fread" ); 
     1995    stdio_ops.fclose = dlsym(RTLD_NEXT, "fclose" ); 
     1996    stdio_ops.fseek = dlsym(RTLD_NEXT, "fseek" ); 
     1997    stdio_ops.fseek64 = dlsym(RTLD_NEXT, "fseek64" ); 
     1998    stdio_ops.fsetpos = dlsym(RTLD_NEXT, "fsetpos" ); 
     1999    stdio_ops.rewind = dlsym(RTLD_NEXT, "rewind" ); 
     2000    stdio_ops.ftell = dlsym(RTLD_NEXT, "ftell" ); 
     2001    stdio_ops.ftell64 = dlsym(RTLD_NEXT, "ftell64" ); 
     2002    stdio_ops.fgetpos = dlsym(RTLD_NEXT, "fgetpos" ); 
     2003    stdio_ops.fflush = dlsym(RTLD_NEXT, "fflush" ); 
     2004    stdio_ops.fputc = dlsym(RTLD_NEXT, "fputc" ); 
     2005    stdio_ops.fputs = dlsym(RTLD_NEXT, "fputs" ); 
     2006    stdio_ops.putc = dlsym(RTLD_NEXT, "putc" ); 
     2007    stdio_ops.putchar = dlsym(RTLD_NEXT, "putchar" ); 
     2008    stdio_ops.puts = dlsym(RTLD_NEXT, "puts" ); 
     2009    stdio_ops.fgets = dlsym(RTLD_NEXT, "fgets" ); 
     2010    stdio_ops.fgetc = dlsym(RTLD_NEXT, "fgetc" ); 
     2011    stdio_ops.getc = dlsym(RTLD_NEXT, "getc" ); 
     2012    stdio_ops.getchar = dlsym(RTLD_NEXT, "getchar" ); 
     2013    stdio_ops.gets = dlsym(RTLD_NEXT, "gets" ); 
     2014    stdio_ops.ungetc = dlsym(RTLD_NEXT, "ungetc" ); 
     2015    stdio_ops.vfprintf = dlsym(RTLD_NEXT, "vfprintf" ); 
     2016    stdio_ops.vprintf = dlsym(RTLD_NEXT, "vprintf" ); 
     2017    stdio_ops.fprintf = dlsym(RTLD_NEXT, "fprintf" ); 
     2018    stdio_ops.printf = dlsym(RTLD_NEXT, "printf" ); 
     2019    stdio_ops.fscanf = dlsym(RTLD_NEXT, "fscanf" ); 
     2020    stdio_ops.scanf = dlsym(RTLD_NEXT, "scanf" ); 
     2021    stdio_ops.clearerr  = dlsym(RTLD_NEXT, "clearerr" ); 
     2022    stdio_ops.feof  = dlsym(RTLD_NEXT, "feof" ); 
     2023    stdio_ops.ferror  = dlsym(RTLD_NEXT, "ferror" ); 
     2024    stdio_ops.fileno  = dlsym(RTLD_NEXT, "fileno" ); 
     2025    stdio_ops.remove  = dlsym(RTLD_NEXT, "remove" ); 
     2026    stdio_ops.setbuf  = dlsym(RTLD_NEXT, "setbuf" ); 
     2027    stdio_ops.setbuffer  = dlsym(RTLD_NEXT, "setbuffer" ); 
     2028    stdio_ops.setlinebuf  = dlsym(RTLD_NEXT, "setlinebuf" ); 
     2029    stdio_ops.setvbuf  = dlsym(RTLD_NEXT, "setvbuf" ); 
     2030    stdio_ops.mkdtemp = dlsym(RTLD_NEXT, "mkdtemp" ); 
     2031    stdio_ops.mkstemp = dlsym(RTLD_NEXT, "mkstemp" ); 
     2032    stdio_ops.tmpfile = dlsym(RTLD_NEXT, "tmpfile" ); 
     2033    stdio_ops.opendir  = dlsym(RTLD_NEXT, "opendir" ); 
     2034    stdio_ops.fdopendir  = dlsym(RTLD_NEXT, "fdopendir" ); 
     2035    stdio_ops.dirfd  = dlsym(RTLD_NEXT, "dirfd" ); 
     2036    stdio_ops.readdir  = dlsym(RTLD_NEXT, "readdir" ); 
     2037    stdio_ops.readdir64  = dlsym(RTLD_NEXT, "readdir64" ); 
     2038    stdio_ops.rewinddir  = dlsym(RTLD_NEXT, "rewinddir" ); 
     2039    stdio_ops.seekdir  = dlsym(RTLD_NEXT, "seekdir" ); 
     2040    stdio_ops.telldir  = dlsym(RTLD_NEXT, "telldir" ); 
     2041    stdio_ops.closedir  = dlsym(RTLD_NEXT, "closedir" ); 
     2042    stdio_ops.scandir  = dlsym(RTLD_NEXT, "scandir" ); 
     2043    stdio_ops.scandir64  = dlsym(RTLD_NEXT, "scandir64" ); 
     2044}; 
     2045 
    15122046/* 
    15132047 * Local variables: 
  • branches/Orange-Branch/src/client/usrint/usrint.h

    r8975 r9016  
    1313#define USRINT_H 1 
    1414 
     15#ifndef _LARGEFILE64_SOURCE 
    1516#define _LARGEFILE64_SOURCE 
     17#endif 
     18#ifndef _GNU_SOURCE 
    1619#define _GNU_SOURCE 
     20#endif 
    1721#define __USE_MISC 1 
    1822#define __USE_ATFILE 1 
     
    3438#include <sys/socket.h> 
    3539#include <sys/resource.h> 
    36 /* #include <sys/statvfs.h> /* struct statfs on OS X */ 
     40#include <sys/sendfile.h> 
     41/* #include <sys/statvfs.h> */ /* struct statfs on OS X */ 
    3742#include <sys/vfs.h> /* struct statfs on Linux */ 
    3843#include <sys/stat.h> 
     
    9095/* Macros */ 
    9196 
     97/* debugging */ 
     98 
     99//#define USRINT_DEBUG 
     100#ifdef  USRINT_DEBUG 
     101#define debug(s,v) fprintf(stderr,s,v) 
     102#else 
     103#define debug(s,v) 
     104#endif 
     105 
     106/* FD sets */ 
     107 
    92108#ifdef FD_SET 
    93109#undef FD_SET