📄 netdrv.c
字号:
/* netDrv.c - network remote file I/O driver *//* Copyright 1984 - 2002 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------05j,10jun02,elr Correct unresolved reference to netDrvFileDoesNotExist()05i,06jun02,elr Documentation corrections (SPRs 78010, 78020)05h,07may02,kbw man page edits05g,31mar02,jkf SPR#74251, increase path length05f,14mar02,elr added more debugging facilities via netDrvDebugLevelSet() split if statements for more precise debugging fixed compilation warnings concerning checking of print specifiers fixed netFileExists with new configlette for flexibility fixed netOpen so that O_CREAT now works (SPR 8283) Made all error messages optional (SPR 71496)05e,05nov01,vvv fixed compilation warnings05d,15oct01,rae merge from truestack ver 05k, base 05b (compile warnings, etc.)05c,21jun00,rsh upgrade to dosFs 2.005c,28seo99,jkf fixed SPR#28927, netLsByName() causes exception.05b,14mar99,jdi doc: removed refs to config.h and/or configAll.h (SPR 25663).05a,12mar99,p_m Fixed SPR 8295 by adding documentation on FIOFSTATGET.04z,24mar95,jag netLs ftpXfer call was change to send just "NLST" when the remote directory is "." (SPR #4160)04x,18jan95,rhp doc: explain cannot always distinguish directories over netDrv04w,11aug94,dzb initialize variables.04v,22apr93,caf ansification: added cast to netIoctl().04u,30sep93,jmm netLsByName now frees the NET_FD it uses04t,30aug93,jag Changed netCreate to delete the end slash from a directory name (SPR #2492)04s,11aug93,jmm Changed ioctl.h and socket.h to sys/ioctl.h and sys/socket.h04r,23jul93,jmm added netLsByName() (spr 2305)04q,08feb93,jmm added doc for FIOFSTATGET ioctl call04p,08feb93,jmm added support for FIOFSTATGET ioctl call (needed for windX) SPR #185604o,01feb93,jdi documentation cleanup for 5.1; added third param to ioctl() examples.04n,13nov92,dnw added include of semLibP.h04m,21oct92,jdi removed mangen SECTION designation.04l,02oct92,srh added ioctl(FIOGETFL) to return file's open mode04k,19jul92,smb added include fcntl.h.04j,18jul92,smb Changed errno.h to errnoLib.h.04i,26may92,rrr the tree shuffle04h,08may92,jmm Fixed ansi prototype warnings04g,04oct91,rrr passed through the ansification filter -changed functions to ansi style -changed includes to have absolute path from h/ -changed READ, WRITE and UPDATE to O_RDONLY O_WRONLY and ... -changed VOID to void -changed copyright notice04f,05apr91,jdi documentation -- removed header parens and x-ref numbers; doc review by dnw.04e,20feb91,jaa documentation cleanup.04d,01oct90,jcf changed network fd semaphore to not be inversion safe.04c,18sep90,kdl removed forward declaration of netWhere().04b,30jul90,dnw changed pathLastName() to pathLastNamePtr(). added forward declaration of void routines.04a,26jun90,jcf changed network fd semaphore to mutex. removed super semaphores.03z,20jun90,jcf changed binary semaphore interface.03y,11may90,yao added missing modification history (03x) for the last checkin.03x,09may90,yao typecasted malloc to (char *).03w,01may90,llk changed call to pathCat(). Now, pathCat() checks if a name is too long, and returns ERROR if it is. 03v,17apr90,jcf changed semaphores to binary for efficiency. 03u,25aug89,dab fixed coding convention violations. 03t,23aug89,dab lint.03s,11jul89,dab made netOpen() check for O_CREAT flag and create a file that doesn't already exist. made netLs() check for ftp protocol. added netFileExists(). added netDelete (). closed all data sockets before calling ftpReplyGet(). QUIT ftp command now sent before closing a ftp control socket. made netOpen() check for O_TRUNC flag.03r,06jul89,llk included pathLib.h. changed call to pathLastName().03q,04apr89,gae allowed open() to succeed for NFS directories so that ls() +dab would work correctly. Added NET_LS_STRING variable.03p,07jul88,jcf lint.03o,22jun88,dnw name tweaks. coalesced 3 malloc's into 1 in netCreate().03n,16jun88,llk added netLs.03m,04jun88,llk changed how netCreate retrieves directory and file name. moved pathCat to pathLib.03l,30may88,dnw changed to v4 names.03k,04may88,jcf changed semaphores to be compatible with new semLib.03j,21apr88,gae fixed documentation about fseek().03i,17mar88,gae fixed netPut() bug. fixed potential ssemGive() & releaseNetFd() ordering bug. fixed bug in netCreate() - file not created if nothing written.03h,28jan88,jcf made kernel independent.03g,18nov87,ecs documentation.03f,06nov87,ecs documentation.03e,16oct87,gae fixed bug in netClose() - return value indeterminate. changed "cd/cat" to use pathCat to construct UNIX filename. Added pathCat(). Semaphored fd usage. Added "super semaphores" to allow successive semTake's on the same semaphore by the same task (needed for recursion).03d,25mar87,jlf documentation dnw added missing parameter on ftpGetReply calls. prepended FTP_ to ftp reply codes.03c,12feb86,dnw fixed bug in netPut. This time for sure.03b,14jan87,dnw fixed bug in netPut.03a,01dec86,dnw added ftp protocol option. changed to not get include files from default directories.02h,08nov86,llk documentation.02g,07oct86,gae made netCreate() use ioGetDefDevTail() instead of global 'currentDirectory' to conclude the remote directory name. Use remGetCurId() to get 'currentIdentity'...included remLib.h02f,05sep86,jlf made netIoctrl return nbytes for FIONREAD in arg. minor documentation.02e,29jul86,llk cleaned up netGet.02d,27jul86,llk receives standard error messages from Unix when giving a remote command (remRcmd). Forwards it on to user. Added getNetStatus.02c,02jul86,jlf documentation02b,21jun86,rdc fixed bug in netCreate - wasn't allocating sufficient space for strings.02a,21may86,llk got rid of ram disk. Implemented network file desciptors. Wrote all routines except netDrv and netDevCreate.01c,03mar86,jlf changed ioctrl calls to ioctl. changed netIoctrl to netIoctl.01b,28jan86,jlf documentation, made netIoctrl be LOCAL.01a,14sep85,rdc written.*//*This driver provides facilities for accessing files transparently over thenetwork via FTP or RSH. By creating a network device with netDevCreate(),files on a remote UNIX machine may be accessed as if they were local.When a remote file is opened, the entire file is copied over the networkto a local buffer. When a remote file is created, an empty local bufferis opened. Any reads, writes, or ioctl() calls are performed on the localcopy of the file. If the file was opened with the flags O_WRONLY or O_RDWRand modified, the local copy is sent back over the network to the UNIXmachine when the file is closed.Note that this copying of the entire file back and forth can make netDrvdevices awkward to use. A preferable mechanism is NFS as provided bynfsDrv.USER-CALLABLE ROUTINESMost of the routines in this driver are accessible only through the I/Osystem. However, two routines must be called directly: netDrv() toinitialize the driver and netDevCreate() to create devices.FILE OPERATIONSThis driver supports the creation, deletion, opening, reading,writing, and appending of files. The renaming of files is notsupported.INITIALIZATIONBefore using the driver, it must be initialized by calling the routinenetDrv(). This routine should be called only once, before any reads,writes, netDevCreate(), or netDevCreate2() calls. Initialization isperformed automatically when INCLUDE_NET_DRV is defined.CREATING NETWORK DEVICESTo access files on a remote host, a network device must be created bycalling netDevCreate() or netDevCreate2(). The arguments to netDevCreate()are the name of the device, the name of the host the device will access,and the remote file access protocol to be used -- RSH or FTP.The arguments to netDevCreate2() are ones described above and a size ofbuffer used in the network device as a fourth argument.By convention, a network device name is the remote machine name followedby a colon ":". For example, for a UNIX host on the network "wrs", filescan be accessed by creating a device called "wrs:". For more information,see the manual entry for netDevCreate() and netDevCreate2().IOCTL FUNCTIONSThe network driver responds to the following ioctl() functions:.iP "FIOGETNAME" 18 3Gets the file name of the file descriptor <fd> and copies it to the bufferspecified by <nameBuf>:.CS status = ioctl (fd, FIOGETNAME, &nameBuf);.CE.iP "FIONREAD"Copies to <nBytesUnread> the number of bytes remaining in the filespecified by <fd>:.CS status = ioctl (fd, FIONREAD, &nBytesUnread);.CE.iP "FIOSEEK"Sets the current byte offset in the file to the position specified by<newOffset>. If the seek goes beyond the end-of-file, the file grows.The end-of-file pointer changes to the new position, and the new space isfilled with zeroes:.CS status = ioctl (fd, FIOSEEK, newOffset);.CE.iP "FIOWHERE"Returns the current byte position in the file.This is the byte offset of the next byte to be read or written.It takes no additional argument:.CS position = ioctl (fd, FIOWHERE, 0);.CE.iP "FIOFSTATGET"Gets file status information. The argument <statStruct> is a pointerto a stat structure that is filled with data describing the specifiedfile. Normally, the stat() or fstat() routine is used to obtain fileinformation, rather than using the FIOFSTATGET function directly.netDrv only fills in three fields of the stat structure: st_dev,st_mode, and st_size. st_mode is always filled with S_IFREG..CS struct stat statStruct; fd = open ("file", O_RDONLY); status = ioctl (fd, FIOFSTATGET, &statStruct);.CELIMITATIONSThe netDrv implementation strategy implies that directories cannotalways be distinguished from plain files. Thus, opendir() does notwork for directories mounted on netDrv devices, and ll() does not flagsubdirectories with the label "DIR" in listings from netDrv devices.When the access method is FTP, operations can only be done on files thatthe FTP server allows to download. In particular it is not possible to'stat' a directory, doing so will result in "<dirname>: not a plain file"error.INCLUDE FILES: netDrv.hSEE ALSO: remLib, netLib, sockLib, hostAdd()*/#include "vxWorks.h"#include "sys/socket.h"#include "iosLib.h"#include "ioLib.h"#include "logLib.h"#include "memLib.h"#include "memPartLib.h"#include "semLib.h"#include "string.h"#include "remLib.h"#include "fcntl.h"#include "ftpLib.h"#include "lstLib.h"#include "netDrv.h"#include "errnoLib.h"#include "unistd.h"#include "stdio.h"#include "pathLib.h"#include "fioLib.h"#include "stdlib.h"#include "private/semLibP.h"#include "private/iosLibP.h"#include "private/funcBindP.h"#include "sys/stat.h"#define FD_DIRTY 4 /* mask for the dirty bit */ /* in the options field of NET_FD, bit [2] */#define DATASIZE 512 /* bytes per block of data *//* remote file access protocols */#define PROTO_RSH 0 /* UNIX RSH protocol */#define PROTO_FTP 1 /* ARPA FTP protocol */#define RSHD 514 /* rshd service */#define NETDRV_DEBUG(string, param1, param2, param3, param4, param5, param6)\ {\ if ((_func_logMsg != NULL) && (netDrvDebugErrors ))\ (* _func_logMsg) (string, param1, param2, param3, param4, param5, \ param6);\ }STATUS netDrvFileDoesNotExist (char *filename, char *response);FUNCPTR _func_fileDoesNotExist = netDrvFileDoesNotExist; /* Use default configlette */typedef struct /* NET_DEV */ { DEV_HDR devHdr; /* device header */ char host[MAX_FILENAME_LENGTH]; /* host for this device */ int protocol; /* remote file access protocol*/ UINT bufSize; /* size of buffer */ int fdMode; /* FD_MODE of this device */ } NET_DEV;/* A network file descriptor consists of a NET_FD header with a linked list * of DATABLOCKs maintained by lstLib. The file pointer (filePtrByte) and * end of file pointer (endOfFile) contain absolute byte numbers starting * at 0. For instance, the first byte in the first block is #0. The first * byte in the second block is #512 for 512 byte blocks. The first byte in * the third block is #1024, etc. Therefore, a byte number modulo 512 gives * the index of the byte in databuf. */typedef struct /* DATA_BLOCK */ { NODE link; /* link to adjacent BLOCKs in file */ int used; /* bytes used in databuf (offset) */ char databuf[DATASIZE]; /* store data here */ } DATABLOCK;/* network file descriptor */typedef struct /* NET_FD */ { LIST dataLst; /* list of data blocks */ NET_DEV * pNetDev; /* file descriptors network device */ DATABLOCK * filePtrBlock; /* pointer to current datablock */ int filePtrByte; /* number of current byte */ int endOfFile; /* byte number after last file byte */ int options; /* contains mode and dirty bit */ char * remDirName; /* directory name on remote machine */ char * remFileName; /* file name on remote machine */ SEM_ID netFdSem; /* mutex semaphore */ char * buf; /* read buffer */ int bufBytes; /* available bytes in the buffer */ int bufOffset; /* current offset in the buffer */ int dataSock; /* data socket */ int ctrlSock; /* control socket */ int status [8]; /* status counter */ } NET_FD;/* globals */#define NET_LS_STRING "/bin/ls -a %s"int mutexOptionsNetDrv = SEM_Q_PRIORITY | SEM_DELETE_SAFE;/* locals */LOCAL BOOL netDrvDebugStats = FALSE; LOCAL BOOL netDrvDebugErrors = FALSE; LOCAL int netDrvNum; /* this particular driver number *//* forward static functions */static int netCreate (NET_DEV *pNetDev, char *name, int mode);static int netCreate2 (NET_FD *pNetFd, char *name, int mode);static STATUS netDelete (NET_DEV *pNetDev, char *name);static int netOpen (NET_DEV *pNetDev, char *name, int mode, int perm);static STATUS netClose (NET_FD *pNetFd);static STATUS netLs (NET_FD *pNetFd);static STATUS netFileExists (NET_DEV *pNetDev, char *name);static void getNetStatus (char *buf);static int netRead (NET_FD *pNetFd, char *buf, int maxBytes);static int netWrite (NET_FD *pNetFd, char *buffer, int nBytes);static int netIoctl (NET_FD *pNetFd, int function, int arg);static int netFdCreate (NET_DEV *pNetDev, char *name, int mode);static void netFdRelease (NET_FD *pNetFd);static STATUS netGet (NET_FD *pNetFd);static STATUS netPut (NET_FD *pNetFd);static int netSeek (NET_FD *pNetFd, int newPos);static STATUS moveEndOfFile (NET_FD *pNetFd, int newPos);static STATUS netSockOpen (NET_FD *pNetFd);static STATUS netSockClose (NET_FD *pNetFd);extern STATUS netDevCreate2 (char *devName, char *host, int protocol, UINT bufSize);/* forward declarations */IMPORT void lstInternalFree (LIST *pList, VOIDFUNCPTR freeFunc);/********************************************************************************* netDrv - install the network remote file driver** This routine initializes and installs the network driver.* It must be called before other network remote file functions are performed.* It is called automatically when INCLUDE_NET_DRV is defined.** VXWORKS AE PROTECTION DOMAINS* Under VxWorks AE, you can call this function from within the kernel * protection domain only. This restriction does not apply under non-AE * versions of VxWorks. ** RETURNS: OK or ERROR.*/STATUS netDrv (void) { if (netDrvNum > 0) return (OK); /* driver already installed */ netDrvNum = iosDrvInstall (netCreate, netDelete, netOpen, netClose, netRead, netWrite, netIoctl); return (netDrvNum == ERROR ? ERROR : OK); }/********************************************************************************* netDevCreate - create a remote file device** This routine creates a remote file device. Normally, a network device is* created for each remote machine whose files are to be accessed. By* convention, a network device name is the remote machine name followed by a* colon ":". For example, for a UNIX host on the network whose name is* "wrs", files can be accessed by creating a device called "wrs:". Files* can be accessed via RSH as follows:** .CS* netDevCreate ("wrs:", "wrs", rsh);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -