• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

RecFile.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   Record compatibility definitions
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 #include "libts.h"
00025 #include "P_RecFile.h"
00026 #include "P_RecDefs.h"
00027 #include "P_RecUtils.h"
00028 
00029 //-------------------------------------------------------------------------
00030 // RecFileOpenR
00031 //-------------------------------------------------------------------------
00032 
00033 RecHandle
00034 RecFileOpenR(const char *file)
00035 {
00036   RecHandle h_file;
00037   return ((h_file =::open(file, O_RDONLY)) < 0) ? REC_HANDLE_INVALID : h_file;
00038 }
00039 
00040 //-------------------------------------------------------------------------
00041 // RecFileOpenW
00042 //-------------------------------------------------------------------------
00043 
00044 RecHandle
00045 RecFileOpenW(const char *file)
00046 {
00047   RecHandle h_file;
00048 
00049   if ((h_file =::open(file, O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0) {
00050     return REC_HANDLE_INVALID;
00051   }
00052   fcntl(h_file, F_SETFD, 1);
00053   return h_file;
00054 }
00055 
00056 //-------------------------------------------------------------------------
00057 // RecFileSync
00058 //-------------------------------------------------------------------------
00059 
00060 int RecFileSync(RecHandle h_file)
00061 {
00062   return (fsync(h_file) == 0) ? REC_ERR_OKAY : REC_ERR_FAIL;
00063 }
00064 
00065 //-------------------------------------------------------------------------
00066 // RecFileClose
00067 //-------------------------------------------------------------------------
00068 
00069 int
00070 RecFileClose(RecHandle h_file)
00071 {
00072   return (close(h_file) == 0) ? REC_ERR_OKAY : REC_ERR_FAIL;
00073 }
00074 
00075 //-------------------------------------------------------------------------
00076 // RecFileRead
00077 //-------------------------------------------------------------------------
00078 
00079 int
00080 RecFileRead(RecHandle h_file, char *buf, int size, int *bytes_read)
00081 {
00082   if ((*bytes_read =::read(h_file, buf, size)) <= 0) {
00083     *bytes_read = 0;
00084     return REC_ERR_FAIL;
00085   }
00086   return REC_ERR_OKAY;
00087 }
00088 
00089 //-------------------------------------------------------------------------
00090 // RecFileWrite
00091 //-------------------------------------------------------------------------
00092 
00093 int
00094 RecFileWrite(RecHandle h_file, char *buf, int size, int *bytes_written)
00095 {
00096   if ((*bytes_written =::write(h_file, buf, size)) < 0) {
00097     *bytes_written = 0;
00098     return REC_ERR_FAIL;
00099   }
00100   return REC_ERR_OKAY;
00101 }
00102 
00103 //-------------------------------------------------------------------------
00104 // RecFileGetSize
00105 //-------------------------------------------------------------------------
00106 
00107 int
00108 RecFileGetSize(RecHandle h_file)
00109 {
00110   struct stat fileStats;
00111   fstat(h_file, &fileStats);
00112   return (int) fileStats.st_size;
00113 }
00114 
00115 //-------------------------------------------------------------------------
00116 // RecFileExists
00117 //-------------------------------------------------------------------------
00118 
00119 int
00120 RecFileExists(const char *file)
00121 {
00122   RecHandle h_file;
00123   if ((h_file = RecFileOpenR(file)) == REC_HANDLE_INVALID) {
00124     return REC_ERR_FAIL;
00125   }
00126   RecFileClose(h_file);
00127   return REC_ERR_OKAY;
00128 
00129 }
00130 
00131 //-------------------------------------------------------------------------
00132 // RecPipeCreate
00133 //-------------------------------------------------------------------------
00134 
00135 RecHandle
00136 RecPipeCreate(const char *base_path, const char *name)
00137 {
00138 
00139   RecHandle listenfd;
00140   RecHandle acceptfd;
00141   struct sockaddr_un servaddr;
00142   struct sockaddr_un cliaddr;
00143   int servaddr_len;
00144   socklen_t cliaddr_len;
00145 
00146   // first, let's disable SIGPIPE (move out later!)
00147   struct sigaction act, oact;
00148   act.sa_handler = SIG_IGN;
00149   sigemptyset(&act.sa_mask);
00150   act.sa_flags = 0;
00151   act.sa_flags |= SA_RESTART;
00152   sigaction(SIGPIPE, &act, &oact);
00153 
00154   // construct a path/filename for the pipe
00155 #define SEPERATOR "/"
00156   char path[PATH_NAME_MAX];
00157   snprintf(path, sizeof(path), "%s%s%s", base_path, SEPERATOR, name);
00158 #undef SEPERATOR
00159   if (strlen(path) > (sizeof(servaddr.sun_path) - 1)) {
00160     RecLog(DL_Warning, "[RecPipeCreate] Path name too long; exiting\n");
00161     return REC_HANDLE_INVALID;
00162   }
00163 
00164   unlink(path);
00165 
00166   if ((listenfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
00167     RecLog(DL_Warning, "[RecPipeCreate] socket error\n");
00168     return REC_HANDLE_INVALID;
00169   }
00170   // set so that child process doesn't inherit our fd
00171   if (fcntl(listenfd, F_SETFD, 1) < 0) {
00172     RecLog(DL_Warning, "[RecPipeCreate] fcntl error\n");
00173     return REC_HANDLE_INVALID;
00174   }
00175 
00176   memset(&servaddr, 0, sizeof(servaddr));
00177   servaddr.sun_family = AF_UNIX;
00178   ink_strlcpy(servaddr.sun_path, path, sizeof(servaddr.sun_path));
00179 
00180   int optval = 1;
00181   if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, (char *) &optval, sizeof(int)) < 0) {
00182     RecLog(DL_Warning, "[RecPipeCreate] setsockopt error\n");
00183     return REC_HANDLE_INVALID;
00184   }
00185 
00186   servaddr_len = sizeof(servaddr.sun_family) + strlen(servaddr.sun_path);
00187   if ((bind(listenfd, (struct sockaddr *) &servaddr, servaddr_len)) < 0) {
00188     RecLog(DL_Warning, "[RecPipeCreate] bind error\n");
00189     close(listenfd);
00190     return REC_HANDLE_INVALID;
00191   }
00192   // listen, backlog of 1 (expecting only one client)
00193   if ((listen(listenfd, 1)) < 0) {
00194     RecLog(DL_Warning, "[RecPipeCreate] listen error\n");
00195     close(listenfd);
00196     return REC_HANDLE_INVALID;
00197   }
00198   // block until we get a connection from the other side
00199   cliaddr_len = sizeof(cliaddr);
00200   if ((acceptfd = accept(listenfd, (struct sockaddr *) &cliaddr,
00201                          &cliaddr_len)) < 0) {
00202     close(listenfd);
00203     return REC_HANDLE_INVALID;
00204   }
00205 
00206   close(listenfd);
00207 
00208   return acceptfd;
00209 }
00210 
00211 //-------------------------------------------------------------------------
00212 // RecPipeConnect
00213 //-------------------------------------------------------------------------
00214 
00215 RecHandle
00216 RecPipeConnect(const char *base_path, const char *name)
00217 {
00218 
00219   RecHandle sockfd;
00220   struct sockaddr_un servaddr;
00221   int servaddr_len;
00222 
00223   // construct a path/filename for the pipe
00224 #define SEPERATOR "/"
00225   char path[PATH_NAME_MAX];
00226   snprintf(path, sizeof(path), "%s%s%s", base_path, SEPERATOR, name);
00227 #undef SEPERATOR
00228   if (strlen(path) > (sizeof(servaddr.sun_path) - 1)) {
00229     RecLog(DL_Warning, "[RecPipeConnect] Path name too long\n");
00230     return REC_HANDLE_INVALID;
00231   }
00232   // Setup Connection to LocalManager */
00233   memset((char *) &servaddr, 0, sizeof(servaddr));
00234   servaddr.sun_family = AF_UNIX;
00235   ink_strlcpy(servaddr.sun_path, path, sizeof(servaddr.sun_path));
00236   servaddr_len = sizeof(servaddr.sun_family) + strlen(servaddr.sun_path);
00237 
00238   if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
00239     RecLog(DL_Warning, "[RecPipeConnect] socket error\n");
00240     return REC_HANDLE_INVALID;
00241   }
00242   // set so that child process doesn't inherit our fd
00243   if (fcntl(sockfd, F_SETFD, 1) < 0) {
00244     RecLog(DL_Warning, "[RecPipeConnect] fcntl error\n");
00245     close(sockfd);
00246     return REC_HANDLE_INVALID;
00247   }
00248   // blocking connect
00249   if ((connect(sockfd, (struct sockaddr *) &servaddr, servaddr_len)) < 0) {
00250     RecLog(DL_Warning, "[RecPipeConnect] connect error\n");
00251     close(sockfd);
00252     return REC_HANDLE_INVALID;
00253   }
00254 
00255   return sockfd;
00256 }
00257 
00258 //-------------------------------------------------------------------------
00259 // RecPipeRead
00260 //-------------------------------------------------------------------------
00261 
00262 int
00263 RecPipeRead(RecHandle h_pipe, char *buf, int size)
00264 {
00265   int bytes_read = 0;
00266   int bytes_wanted = size;
00267   char *p = buf;
00268   while (bytes_wanted > 0) {
00269     bytes_read = read(h_pipe, p, bytes_wanted);
00270     if (bytes_read < 0) {
00271       // FIXME: something more intelligent please!
00272       return REC_ERR_FAIL;
00273     }
00274     bytes_wanted -= bytes_read;
00275     p += bytes_read;
00276   }
00277   return REC_ERR_OKAY;
00278 }
00279 
00280 //-------------------------------------------------------------------------
00281 // RecPipeWrite
00282 //-------------------------------------------------------------------------
00283 
00284 int
00285 RecPipeWrite(RecHandle h_pipe, char *buf, int size)
00286 {
00287   int bytes_written = 0;
00288   int bytes_to_write = size;
00289   char *p = buf;
00290   while (bytes_to_write > 0) {
00291     bytes_written = write(h_pipe, p, bytes_to_write);
00292     if (bytes_written < 0) {
00293       // FIXME: something more intelligent please!
00294       return REC_ERR_FAIL;
00295     }
00296     bytes_to_write -= bytes_written;
00297     p += bytes_written;
00298   }
00299 
00300   return REC_ERR_OKAY;
00301 }
00302 
00303 //-------------------------------------------------------------------------
00304 // RecPipeClose
00305 //-------------------------------------------------------------------------
00306 
00307 int
00308 RecPipeClose(RecHandle h_pipe)
00309 {
00310   return (close(h_pipe) == 0) ? REC_ERR_OKAY : REC_ERR_FAIL;
00311 }

Generated by  doxygen 1.7.1