?? ioslib.c
字號(hào):
/* iosLib.c - I/O system library *//* Copyright 1984-2001 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------04n,03oct01,dcb Fix SPR 20033. iosFdSet uses malloc without checking return code.04m,31aug98,ms add _func_ioTaskStdSet initialization.04l,24aug96,sgv fix for spr 6802. Modified iosFdNew to check for maxFiles after a fd value is chosen.04k,23aug96,ms writing to null device no longer an error (SPR 7076)04k,11jan95,rhp explain "default device" in iosDevFind() man page (SPR#2462)04j,17oct94,rhp removed obsolete reference to pathLib in doc (SPR#3712).04i,26jul94,dvs made iosWrite/iosRead consistant so they both return ERROR if no driver routine - also changed doc (SPR #2019 and 2020).04h,14jul94,dvs made write return 0 if no driver routine (SPR #2020).04g,07dec93,elh added asynchronous I/O support for Posix.04f,21jan93,jdi documentation cleanup for 5.1.04e,13nov92,dnw added include of semLibP.h04d,23aug92,jcf split show routines into iosShow.c. moved typedefs/defines to private/iosLibP.h. changed semMInit->semBInit. changed lst* to dll*.04c,18jul92,smb Changed errno.h to errnoLib.h.04b,26may92,rrr the tree shuffle04a,25nov91,rrr cleanup of some ansi warnings.03z,04oct91,rrr passed through the ansification filter -changed functions to ansi style -changed includes to have absolute path from h/ -changed VOID to void -changed copyright notice03y,30apr91,jdi documentation tweaks.03x,26apr91,shl fixed potential race in iosFdFree() (spr 997).03w,05apr91,jdi documentation -- removed header parens and x-ref numbers; moved position of iosDrvShow(); doc review by dnw.03v,08feb92,jaa documentation cleanup.03u,05oct90,dnw made iosNextDevGet() be NOMANUAL03t,10aug90,kdl added forward declarations for functions returning void.03s,26jun90,jcf changed iosSemaphore to mutex.03r,29may90,dnw fixed iosDevDelete to free memory allocated for name string. tweaked documentation for iosDevAdd & iosDevDelete.03q,25may90,dnw fixed printf format in ios{Dev,Fd}Show to show entire name instead of just 1st 20 chars. changed iosFdSet to malloc space for filename, unless filename is same as device name. Note that callers should NOT malloc the name before calling iosFdSet anymore. See doc in iosFdSet. made several obscure iosFd... routines be "no manual".03p,11may90,yao added missing modification history (03o) for the last checkin.03o,09may90,yao typecasted malloc to (char *).03n,07may90,hjb added iosFdDevFind routine.03m,14mar90,jdi documentation cleanup.03l,14nov88,dnw changed ioTaskStdGet to take taskId arg.03i,08oct88,gae fixed checking for task specific std. input/output/error fd's.03h,15aug88,jcf lint.03g,15jul88,llk changed iosFdSet. fd names are now allocated before iosFdSet is called.03f,30jun88,llk added iosNextDevGet(). changed iosDevAdd() so that it will not add devices with duplicate names.03e,04jun88,llk replaced ioDefDev with ioDefPath.03d,30may88,dnw changed to v4 names.03c,28may88,dnw made ios{Create,Delete,Open,Close,Read,Write,Ioctl} LOCAL.03b,23apr88,jcf fixed semaphore calls for new semLib.03a,30mar88,gae made drvTable & fdTable definition local. made fd's 0, 1, & 2 be standard in/out/err a la UNIX. added ios{Creat,Delete,Open,Read,Write,Ioctl,Close}() for use by ioLib.c. Got rid of iosFdGetFree() and added iosFdNew() to simplify setting up of new file descriptor. iosFdCheck() turned into a macro for speed/jsr overhead. iosDevList() shows driver number. iosFdList() shows driver number and indicates std. in/out/err.02b,20feb88,dnw fixed missing return value in iosDrvRemove().02a,16dec87,jlf added iosDevDelete (), iosDrvRemove (), and iosDrvList (). changed iosDrvInstall to look for an unused entry, instead of just adding new drivers at the end.01p,05nov87,rdc fixed documentation for iosDevFind.01o,30sep87,gae made fdTable LOCAL; added iosFdSet() to set values in fdTable; now keep file name in table; added iosFdList().01n,28apr87,gae made iosLock() and iosUnlock() LOCAL.01m,24mar87,jlf documentation.01l,20dec86,dnw changed iosDevMatch() to find longest (instead of just first) device name that is initial substring of file name. changed to not get include files from default directories.01k,01dec86,dnw changed iosDevAdd() to put device name in allocated memory, instead of in fixed length array.01j,14apr86,rdc changed memAllocates to mallocs.01i,11oct85,dnw de-linted.01h,20jul85,jlf documentation.01g,19sep84,jlf worked on the comments a little.01f,05sep84,jlf fixed iosDevMatch to work properly. Added copyright. Added comments.01e,04sep84,dnw added iosDevList.01d,10aug84,dnw changed ioDevFind to include "default device" search if device name not explicitly specified.01c,07aug84,ecs added calls to setStatus to iosDevFind, iosDrvInstall, iosFdCheck, and iosFdGetFree01b,29jun84,ecs changed iosDevFind to use new version of cmpbuf01a,11jun84,dnw culled from old drvLib and ioLib*//*This library is the driver-level interface to the I/O system. Itsprimary purpose is to route user I/O requests to the proper drivers, usingthe proper parameters. To do this, iosLib keeps tables describing theavailable drivers (e.g., names, open files).The I/O system should be initialized by calling iosInit(), before callingany other routines in iosLib. Each driver then installs itself by callingiosDrvInstall(). The devices serviced by each driver are added to the I/Osystem with iosDevAdd().The I/O system is described more fully in the .I "I/O System"chapter of the.I "Programmer's Guide."INCLUDE FILES: iosLib.hSEE ALSO: intLib, ioLib,.pG "I/O System"*/#include "vxWorks.h"#include "dllLib.h"#include "stdlib.h"#include "semLib.h"#include "ioLib.h"#include "string.h"#include "errnoLib.h"#include "stdio.h"#include "private/iosLibP.h"#include "private/semLibP.h"#include "private/funcBindP.h"/* globals */DL_LIST iosDvList; /* list of I/O device headers */DRV_ENTRY * drvTable; /* driver entry point table */FD_ENTRY * fdTable; /* table of fd entries */int maxDrivers; /* max installed drivers */int maxFiles; /* max open files */char ioDefPath[MAX_FILENAME_LENGTH]; /* default I/O prefix */int mutexOptionsIosLib = SEM_Q_FIFO | SEM_DELETE_SAFE;BOOL iosLibInitialized = FALSE;VOIDFUNCPTR iosFdNewHookRtn = NULL;VOIDFUNCPTR iosFdFreeHookRtn = NULL;/* forward static functions */static DEV_HDR *iosDevMatch (char *name);static void iosLock (void);static void iosUnlock (void);/* locals */LOCAL SEMAPHORE iosSemaphore; /* semaphore to interlock access to io tables */LOCAL DEV_HDR nullDevHdr; /* device header for null device *//******************************************************************************** nullWrite - NULL device write routine*/ static STATUS nullWrite ( int dummy, char * pBuf, int nBytes ) { return (nBytes); }/********************************************************************************* iosInit - initialize the I/O system** This routine initializes the I/O system.* It must be called before any other I/O system routine.** RETURNS: OK, or ERROR if memory is insufficient.*/STATUS iosInit ( int max_drivers, /* maximum number of drivers allowed */ int max_files, /* max number of files allowed open at once */ char *nullDevName /* name of the null device (bit bucket) */ ) { int i; int size; maxDrivers = max_drivers; maxFiles = max_files; ioDefPath [0] = EOS; /* allocate file table and make all entries free */ size = maxFiles * sizeof (FD_ENTRY); fdTable = (FD_ENTRY *) malloc ((unsigned) size); if (fdTable == NULL) return (ERROR); bzero ((char *)fdTable, size); for (i = 0; i < maxFiles; i++) iosFdFree (STD_FIX(i)); /* allocate driver table and make all entries null */ size = maxDrivers * sizeof (DRV_ENTRY); drvTable = (DRV_ENTRY *) malloc ((unsigned) size); if (drvTable == NULL) return (ERROR); bzero ((char *) drvTable, size); for (i = 0; i < maxDrivers; i++) drvTable [i].de_inuse = FALSE; /* initialize the device list and data structure semaphore; * add the null device to the system */ semBInit (&iosSemaphore, SEM_Q_PRIORITY, SEM_FULL); dllInit (&iosDvList); drvTable [0].de_write = nullWrite; iosDevAdd (&nullDevHdr, nullDevName, 0); iosLibInitialized = TRUE; _func_ioTaskStdSet = (FUNCPTR)ioTaskStdSet; return (OK); }/********************************************************************************* iosDrvInstall - install an I/O driver** This routine should be called once by each I/O driver. It hooks up the* various I/O service calls to the driver service routines, assigns* the driver a number, and adds the driver to the driver table.** RETURNS:* The driver number of the new driver, or ERROR if there is no room for the* driver.*/int iosDrvInstall ( FUNCPTR pCreate, /* pointer to driver create function */ FUNCPTR pDelete, /* pointer to driver delete function */ FUNCPTR pOpen, /* pointer to driver open function */ FUNCPTR pClose, /* pointer to driver close function */ FUNCPTR pRead, /* pointer to driver read function */ FUNCPTR pWrite, /* pointer to driver write function */ FUNCPTR pIoctl /* pointer to driver ioctl function */ ) { FAST DRV_ENTRY *pDrvEntry = NULL; FAST int drvnum; iosLock (); /* Find a free driver table entry. Never assign driver number 0. */ for (drvnum = 1; drvnum < maxDrivers; drvnum++) if (! drvTable [drvnum].de_inuse) { /* We've got a free entry */ pDrvEntry = &drvTable [drvnum]; break; } if (pDrvEntry == NULL) { /* we couldn't find a free driver table entry */ errnoSet (S_iosLib_DRIVER_GLUT); iosUnlock (); return (ERROR); } pDrvEntry->de_inuse = TRUE; pDrvEntry->de_create= pCreate; pDrvEntry->de_delete= pDelete; pDrvEntry->de_open = pOpen; pDrvEntry->de_close = pClose; pDrvEntry->de_read = pRead; pDrvEntry->de_write = pWrite; pDrvEntry->de_ioctl = pIoctl; iosUnlock (); return (drvnum); }/********************************************************************************* iosDrvRemove - remove an I/O driver** This routine removes an I/O driver (added by iosDrvInstall()) from * the driver table.* ** RETURNS: OK, or ERROR if the driver has open files.** SEE ALSO: iosDrvInstall()*/STATUS iosDrvRemove ( int drvnum, /* no. of driver to remove, */ /* returned by iosDrvInstall() */ BOOL forceClose /* if TRUE, force closure of open files */ ) { DEV_HDR *pDevHdr; FAST int fd; FAST FD_ENTRY *pFdEntry; FAST DRV_ENTRY *pDrvEntry = &drvTable [drvnum]; FUNCPTR drvClose = pDrvEntry->de_close; iosLock (); /* See if there are any open fd's for this driver */ for (fd = 0; fd < maxFiles; fd++) { pFdEntry = &fdTable [fd]; if (pFdEntry->inuse && pFdEntry->pDevHdr->drvNum == drvnum) { if (! forceClose) { iosUnlock (); return (ERROR); } else { if (drvClose != NULL) (* drvClose) (pFdEntry->value); iosFdFree (STD_FIX(fd)); } } } /* remove any devices for this driver */ for (pDevHdr = (DEV_HDR *) DLL_FIRST (&iosDvList); pDevHdr != NULL; pDevHdr = (DEV_HDR *) DLL_NEXT (&pDevHdr->node)) { if (pDevHdr->drvNum == drvnum) { free (pDevHdr->name); dllRemove (&iosDvList, &pDevHdr->node); } } pDrvEntry->de_inuse = FALSE; pDrvEntry->de_create= NULL; pDrvEntry->de_delete= NULL; pDrvEntry->de_open = NULL; pDrvEntry->de_close = NULL; pDrvEntry->de_read = NULL; pDrvEntry->de_write = NULL; pDrvEntry->de_ioctl = NULL; iosUnlock (); return (OK); }/********************************************************************************* iosDevAdd - add a device to the I/O system** This routine adds a device to the I/O system device list, making the* device available for subsequent open() and creat() calls.** The parameter <pDevHdr> is a pointer to a device header, DEV_HDR (defined* in iosLib.h), which is used as the node in the device list. Usually this* is the first item in a larger device structure for the specific device* type. The parameters <name> and <drvnum> are entered in <pDevHdr>.** RETURNS: OK, or ERROR if there is already a device with the specified name.*/STATUS iosDevAdd ( DEV_HDR *pDevHdr, /* pointer to device's structure */ char *name, /* name of device */ int drvnum /* no. of servicing driver, */ /* returned by iosDrvInstall() */ ) { DEV_HDR *pDevMatch = iosDevMatch (name); /* don't add a device with a name that already exists in the device list. * iosDevMatch will return NULL if a device name is a substring of the * named argument, so check that the two names really are identical. */ if ((pDevMatch != NULL) && (strcmp (pDevMatch->name, name) == 0)) { errnoSet (S_iosLib_DUPLICATE_DEVICE_NAME); return (ERROR); } pDevHdr->name = (char *) malloc ((unsigned) (strlen (name) + 1)); pDevHdr->drvNum = drvnum; if (pDevHdr->name == NULL) return (ERROR); strcpy (pDevHdr->name, name); iosLock (); dllAdd (&iosDvList, &pDevHdr->node); iosUnlock (); return (OK); }/********************************************************************************* iosDevDelete - delete a device from the I/O system** This routine deletes a device from the I/O system device list, making it* unavailable to subsequent open() or creat() calls. No interaction with* the driver occurs, and any file descriptors open on the device or pending* operations are unaffected.** If the device was never added to the device list, unpredictable results* may occur.** RETURNS: N/A*/void iosDevDelete ( DEV_HDR *pDevHdr /* pointer to device's structure */ ) { iosLock (); dllRemove (&iosDvList, &pDevHdr->node); iosUnlock (); free (pDevHdr->name); }/********************************************************************************* iosDevFind - find an I/O device in the device list** This routine searches the device list for a device whose name matches the* first portion of <name>. If a device is found, iosDevFind() sets the* character pointer pointed to by <pNameTail> to point to the first* character in <name>, following the portion which matched the device name.* It then returns a pointer to the device. If the routine fails, it returns* a pointer to the default device (that is, the device where the current* working directory is mounted) and sets <pNameTail> to point to the* beginning of <name>. If there is no default device, iosDevFind() * returns NULL.** RETURNS:* A pointer to the device header, or NULL if the device is not found.**/DEV_HDR *iosDevFind ( char *name, /* name of the device */ char **pNameTail /* where to put ptr to tail of name */ ) { FAST DEV_HDR *pDevHdr = iosDevMatch (name); if (pDevHdr != NULL) *pNameTail = name + strlen (pDevHdr->name); else { pDevHdr = iosDevMatch (ioDefPath); *pNameTail = name; } if (pDevHdr == NULL) errnoSet (S_iosLib_DEVICE_NOT_FOUND); return (pDevHdr); }/************************************************************************ iosDevMatch - find device whose name matches specified string** RETURNS: Pointer to device header, or NULL if device not found.*/LOCAL DEV_HDR *iosDevMatch ( char *name ) { FAST DEV_HDR *pDevHdr; FAST int len; DEV_HDR *pBestDevHdr = NULL; int maxLen = 0; iosLock (); for (pDevHdr = (DEV_HDR *) DLL_FIRST (&iosDvList); pDevHdr != NULL; pDevHdr = (DEV_HDR *) DLL_NEXT (&pDevHdr->node)) { len = strlen (pDevHdr->name); if (strncmp (pDevHdr->name, name, len) == 0) { /* this device name is initial substring of name; * if it is longer than any other such device name so far, * remember it. */ if (len > maxLen) { pBestDevHdr = pDevHdr; maxLen = len; } }
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -