00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "libts.h"
00025 #include "P_RecFile.h"
00026 #include "P_RecDefs.h"
00027 #include "P_RecUtils.h"
00028
00029
00030
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
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
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
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
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
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
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
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
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
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
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
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
00193 if ((listen(listenfd, 1)) < 0) {
00194 RecLog(DL_Warning, "[RecPipeCreate] listen error\n");
00195 close(listenfd);
00196 return REC_HANDLE_INVALID;
00197 }
00198
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
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
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
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
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
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
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
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
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
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
00305
00306
00307 int
00308 RecPipeClose(RecHandle h_pipe)
00309 {
00310 return (close(h_pipe) == 0) ? REC_ERR_OKAY : REC_ERR_FAIL;
00311 }