/* * Copyright (C) 1998 by Southwest Research Institute (SwRI) * * All rights reserved under U.S. Copyright Law and International Conventions. * * The development of this Software was supported by contracts NAG5-3148, * NAG5-6855, NAS8-36840, NAG5-2323, and NAG5-7043 issued on behalf of * the United States Government by its National Aeronautics and Space * Administration. Southwest Research Institute grants to the Government, * and others acting on its behalf, a paid-up nonexclusive, irrevocable, * worldwide license to reproduce, prepare derivative works, and perform * publicly and display publicly, by or on behalf of the Government. * Other than those rights granted to the United States Government, no part * of this Software may be reproduced in any form or by any means, electronic * or mechanical, including photocopying, without permission in writing from * Southwest Research Institute. All inquiries should be addressed to: * * Director of Contracts * Southwest Research Institute * P. O. Drawer 28510 * San Antonio, Texas 78228-0510 * * * Use of this Software is governed by the terms of the end user license * agreement, if any, which accompanies or is included with the Software * (the "License Agreement"). An end user will be unable to install any * Software that is accompanied by or includes a License Agreement, unless * the end user first agrees to the terms of the License Agreement. Except * as set forth in the applicable License Agreement, any further copying, * reproduction or distribution of this Software is expressly prohibited. * Installation assistance, product support and maintenance, if any, of the * Software is available from SwRI and/or the Third Party Providers, as the * case may be. * * Disclaimer of Warranty * * SOFTWARE IS WARRANTED, IF AT ALL, IN ACCORDANCE WITH THESE TERMS OF THE * LICENSE AGREEMENT. UNLESS OTHERWISE EXPLICITLY STATED, THIS SOFTWARE IS * PROVIDED "AS IS", IS EXPERIMENTAL, AND IS FOR NON-COMMERCIAL USE ONLY, * AND ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED, EXCEPT TO THE EXTENT THAT * SUCH DISCLAIMERS ARE HELD TO BE LEGALLY INVALID. * * Limitation of Liability * * SwRI SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED AS A RESULT OF USING, * MODIFYING, CONTRIBUTING, COPYING, DISTRIBUTING, OR DOWNLOADING THIS * SOFTWARE. IN NO EVENT SHALL SwRI BE LIABLE FOR ANY INDIRECT, PUNITIVE, * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGE (INCLUDING LOSS OF BUSINESS, * REVENUE, PROFITS, USE, DATA OR OTHER ECONOMIC ADVANTAGE) HOWEVER IT ARISES, * WHETHER FOR BREACH OF IN TORT, EVEN IF SwRI HAS BEEN PREVIOUSLY ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. YOU HAVE SOLE RESPONSIBILITY FOR ADEQUATE * PROTECTION AND BACKUP OF DATA AND/OR EQUIPMENT USED IN CONNECTION WITH THE * SOFTWARE AND WILL NOT MAKE A CLAIM AGAINST SwRI FOR LOST DATA, RE-RUN TIME, * INACCURATE OUTPUT, WORK DELAYS OR LOST PROFITS RESULTING FROM THE USE OF * THIS SOFTWARE. YOU AGREE TO HOLD SwRI HARMLESS FROM, AND YOU COVENANT NOT * TO SUE SwRI FOR, ANY CLAIMS BASED ON USING THE SOFTWARE. * * Local Laws: Export Control * * You acknowledge and agree this Software is subject to the U.S. Export * Administration Laws and Regulations. Diversion of such Software contrary * to U.S. law is prohibited. You agree that none of the Software, nor any * direct product therefrom, is being or will be acquired for, shipped, * transferred, or reexported, directly or indirectly, to proscribed or * embargoed countries or their nationals, nor be used for nuclear activities, * chemical biological weapons, or missile projects unless authorized by U.S. * Government. Proscribed countries are set forth in the U.S. Export * Administration Regulations. Countries subject to U.S embargo are: Cuba, * Iran, Iraq, Libya, North Korea, Syria, and the Sudan. This list is subject * to change without further notice from SwRI, and you must comply with the * list as it exists in fact. You certify that you are not on the U.S. * Department of Commerce's Denied Persons List or affiliated lists or on the * U.S. Department of Treasury's Specially Designated Nationals List. You agree * to comply strictly with all U.S. export laws and assume sole responsibilities * for obtaining licenses to export or reexport as may be required. * * General * * These Terms represent the entire understanding relating to the use of the * Software and prevail over any prior or contemporaneous, conflicting or * additional, communications. SwRI can revise these Terms at any time * without notice by updating this posting. * * Trademarks * * The SwRI logo is a trademark of SwRI in the United States and other countries. * */ #ident "@(#) rtime_dfiles.c 1.36 05/08/19 SwRI" #include #include #include #include #include #include #include #include #include #include "ret_codes.h" #include "libbase_idfs.h" #include "libdb.h" #define NTRIES 100 /******************************************************************************* * * * IR_REALTIME_DATA_FILES SUBROUTINE * * * * DESCRIPTION * * This module is called to determine if the realtime data files for the * * requested data set are available for usage. All realtime data files are * * kept in a specific directory. In order to integrate with the listener * * cleanly, more than one attempt is made to open the data and/or header file * * in case processing is a little ahead of the opening of the new file by the * * listener. In addition, a check is made to ensure that the same file is * * not utilized more than one (listener has not moved the old file yet). * * * * INPUT VARIABLES * * SDDAS_SHORT btime_yr the start time requested (year component) * * SDDAS_SHORT btime_day the start time requested (day component) * * SDDAS_LONG btime_sec the start time requested (seconds component)* * SDDAS_LONG btime_nsec the start time requested (nanoseconds) * * SDDAS_SHORT etime_yr the stop time requested (year component) * * SDDAS_SHORT etime_day the stop time requested (day component) * * SDDAS_LONG etime_sec the stop time requested (seconds component) * * SDDAS_LONG etime_nsec the stop time requested (nanoseconds) * * SDDAS_ULONG data_key key which uniquely identifies the data set * * being processed * * SDDAS_CHAR *exten filename extension for the data to be used * * * * USAGE * * x = ir_realtime_data_files (btime_yr, btime_day, btime_sec, btime_nsec, * * etime_yr, etime_day, etime_sec, etime_nsec, * * data_key, exten) * * * * NECESSARY SUBPROGRAMS * * memset () memory initialization routine * * setitimer() set value of interval timer * * sizeof () the size of the specified object in bytes * * strcpy() copies a string to another string variable * * open() opens the file requested * * fstat() gets directory information for a file * * close() closes the file associated with the file * * descriptor specified * * setitimer() sets the alarm timer * * select() used to provide a timeout for retry * * dbGetRealTimeFile() returns the name of the data file(s) to open* * sigaction () software signal facilities * * * * EXTERNAL VARIABLES * * struct general_info structure that holds information concerning * * ginfo the experiment that is being processed * * * * INTERNAL VARIABLES * * struct experiment_info *ex a pointer to the structure that holds * * specific experiment information * * struct stat file_info directory information for a file * * struct itimerval dtold alarm timer control structure * * struct itimerval dtnew alarm timer control structure * * struct sigaction sigstate software signal structure * * struct timeval t time value structure * * struct timeval tset time value structure * * int rval1 holds value returned by the database request* * int masked flag checking for the presence of a timer * * SDDAS_CHAR hname[] the full path name of the header file * * SDDAS_CHAR dname[] the full path name of the data file * * char num_try number of tries on a file open * * * * SUBSYSTEM * * Display Level * * * ******************************************************************************/ SDDAS_SHORT ir_realtime_data_files (SDDAS_SHORT btime_yr, SDDAS_SHORT btime_day, SDDAS_LONG btime_sec, SDDAS_LONG btime_nsec, SDDAS_SHORT etime_yr, SDDAS_SHORT etime_day, SDDAS_LONG etime_sec, SDDAS_LONG etime_nsec, SDDAS_ULONG data_key, SDDAS_CHAR *exten) { extern struct general_info ginfo; struct experiment_info *ex; #if !(defined (__MINGW32__)) && !(defined (__MSVCRT__)) struct stat file_info; struct itimerval dtold, dtnew; struct sigaction sigstate; struct timeval t, tset; int masked; char num_try; #endif SDDAS_CHAR hname[MAXPATHLEN], dname[MAXPATHLEN]; int rval1; /***************************************************************************/ /* If the data files have already been opened, no need to open them again.*/ /***************************************************************************/ ex = ginfo.expt; if (ex->fdh != -1) return (ALL_OKAY); /**************************************************************************/ /* When plotting the data, both files must be online and ready to */ /* go. */ /**************************************************************************/ rval1 = dbIDFSGetRealTimeFile (data_key, _H_AND_D_, exten, hname, dname); if (rval1 < 0) return ((SDDAS_SHORT) rval1); #if !(defined (__MINGW32__)) && !(defined (__MSVCRT__)) /***************************************************************************/ /* If there is a signal handler present, disable the alarm timer, saving */ /* any old timer setting in dtold. */ /* Set dtold.it_value to dtold.it_interval to make sure the value */ /* field is non-zero. */ /* Set tset to .5 seconds. */ /***************************************************************************/ masked = 0; sigaction(SIGALRM, NULL, &sigstate); if (sigstate.sa_handler != SIG_DFL) { masked = 1; memset((void *)&dtnew, 0, (size_t) (sizeof(dtnew))); memset((void *)&dtold, 0, (size_t) (sizeof(dtold))); setitimer(ITIMER_REAL, &dtnew, &dtold); dtold.it_value = dtold.it_interval; } tset.tv_sec = 0; tset.tv_usec = 500000; /************************************************************************/ /* The program will try to open the named file many times if either */ /* the file was not there or if the same file has been opened (timing */ /* between this program and listener). */ /************************************************************************/ strcpy (ex->header_name, hname); for (num_try = NTRIES ; num_try ; num_try--) { if ((ex->fdh = open (hname, O_RDONLY | O_BINARY)) >= 0) { fstat (ex->fdh, &file_info); /*****************************************************************/ /* If the same file has been opened, try again. */ /*****************************************************************/ if (file_info.st_ino == ex->inh) { close (ex->fdh); ex->fdh = -1; } else { ex->inh = file_info.st_ino; break; } } /*****************************************************************/ /* Call select to wait .5 seconds. */ /* Wait for the timer to expire before trying again. */ /*****************************************************************/ t = tset; select (0, (fd_set *) 0x0, (fd_set *) 0x0, (fd_set *) 0x0, &t); } /*****************************************************************/ /* Error opening the designated header file. */ /*****************************************************************/ if (!num_try) { if (masked) setitimer(ITIMER_REAL, &dtold, (struct itimerval *) 0x0); return (RTIME_NO_HEADER); } /************************************************************************/ /* The program will try to open the named file many times if either */ /* the file was not there or if the same file has been opened (timing */ /* between this program and listener). */ /************************************************************************/ strcpy (ex->data_name, dname); for (num_try = NTRIES ; num_try ; num_try--) { if ((ex->fdd = open (dname, O_RDONLY | O_BINARY)) >= 0) { fstat (ex->fdd, &file_info); /*****************************************************************/ /* If the same file has been opened, try again. */ /*****************************************************************/ if (file_info.st_ino == ex->ind) { close (ex->fdd); ex->fdd = -1; } else { ex->ind = file_info.st_ino; break; } } /*****************************************************************/ /* Call select to wait .5 seconds. */ /* Wait for the timer to expire before trying again. */ /*****************************************************************/ t = tset; select (0, (fd_set *) 0x0, (fd_set *) 0x0, (fd_set *) 0x0, &t); } /*****************************************************************/ /* Error opening the designated data file. */ /*****************************************************************/ if (!num_try) { if (masked) setitimer(ITIMER_REAL, &dtold, (struct itimerval *) 0x0); return (RTIME_NO_DATA); } if (masked) setitimer(ITIMER_REAL, &dtold, (struct itimerval *) 0x0); #endif return (ALL_OKAY); }