• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #if !defined(_GNU_SOURCE)
26 #define _GNU_SOURCE
27 #endif
28 #include "private-lib-core.h"
29 
30 #include <pwd.h>
31 #include <grp.h>
32 
33 #ifdef LWS_WITH_PLUGINS
34 #include <dlfcn.h>
35 #endif
36 #include <dirent.h>
37 
lws_plat_apply_FD_CLOEXEC(int n)38 int lws_plat_apply_FD_CLOEXEC(int n)
39 {
40 	if (n == -1)
41 		return 0;
42 
43 	return fcntl(n, F_SETFD, FD_CLOEXEC);
44 }
45 
46 int
lws_plat_write_file(const char * filename,void * buf,int len)47 lws_plat_write_file(const char *filename, void *buf, int len)
48 {
49 	int m, fd;
50 
51 	fd = lws_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
52 
53 	if (fd == -1)
54 		return 1;
55 
56 	m = write(fd, buf, len);
57 	close(fd);
58 
59 	return m != len;
60 }
61 
62 int
lws_plat_read_file(const char * filename,void * buf,int len)63 lws_plat_read_file(const char *filename, void *buf, int len)
64 {
65 	int n, fd = lws_open(filename, O_RDONLY);
66 	if (fd == -1)
67 		return -1;
68 
69 	n = read(fd, buf, len);
70 	close(fd);
71 
72 	return n;
73 }
74 
75 lws_fop_fd_t
_lws_plat_file_open(const struct lws_plat_file_ops * fops,const char * filename,const char * vpath,lws_fop_flags_t * flags)76 _lws_plat_file_open(const struct lws_plat_file_ops *fops, const char *filename,
77 		    const char *vpath, lws_fop_flags_t *flags)
78 {
79 	struct stat stat_buf;
80 	int ret = lws_open(filename, (*flags) & LWS_FOP_FLAGS_MASK, 0664);
81 	lws_fop_fd_t fop_fd;
82 
83 	if (ret < 0)
84 		return NULL;
85 
86 	if (fstat(ret, &stat_buf) < 0)
87 		goto bail;
88 
89 	fop_fd = malloc(sizeof(*fop_fd));
90 	if (!fop_fd)
91 		goto bail;
92 
93 	fop_fd->fops = fops;
94 	fop_fd->flags = *flags;
95 	fop_fd->fd = ret;
96 	fop_fd->filesystem_priv = NULL; /* we don't use it */
97 	fop_fd->len = stat_buf.st_size;
98 	fop_fd->pos = 0;
99 
100 	return fop_fd;
101 
102 bail:
103 	close(ret);
104 	return NULL;
105 }
106 
107 int
_lws_plat_file_close(lws_fop_fd_t * fop_fd)108 _lws_plat_file_close(lws_fop_fd_t *fop_fd)
109 {
110 	int fd = (*fop_fd)->fd;
111 
112 	free(*fop_fd);
113 	*fop_fd = NULL;
114 
115 	return close(fd);
116 }
117 
118 lws_fileofs_t
_lws_plat_file_seek_cur(lws_fop_fd_t fop_fd,lws_fileofs_t offset)119 _lws_plat_file_seek_cur(lws_fop_fd_t fop_fd, lws_fileofs_t offset)
120 {
121 	lws_fileofs_t r;
122 
123 	if (offset > 0 &&
124 	    offset > (lws_fileofs_t)fop_fd->len - (lws_fileofs_t)fop_fd->pos)
125 		offset = fop_fd->len - fop_fd->pos;
126 
127 	if ((lws_fileofs_t)fop_fd->pos + offset < 0)
128 		offset = -fop_fd->pos;
129 
130 	r = lseek(fop_fd->fd, offset, SEEK_CUR);
131 
132 	if (r >= 0)
133 		fop_fd->pos = r;
134 	else
135 		lwsl_err("error seeking from cur %ld, offset %ld\n",
136                         (long)fop_fd->pos, (long)offset);
137 
138 	return r;
139 }
140 
141 int
_lws_plat_file_read(lws_fop_fd_t fop_fd,lws_filepos_t * amount,uint8_t * buf,lws_filepos_t len)142 _lws_plat_file_read(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
143 		    uint8_t *buf, lws_filepos_t len)
144 {
145 	long n;
146 
147 	n = read((int)fop_fd->fd, buf, len);
148 	if (n == -1) {
149 		*amount = 0;
150 		return -1;
151 	}
152 	fop_fd->pos += n;
153 	lwsl_debug("%s: read %ld of req %ld, pos %ld, len %ld\n", __func__, n,
154                   (long)len, (long)fop_fd->pos, (long)fop_fd->len);
155 	*amount = n;
156 
157 	return 0;
158 }
159 
160 int
_lws_plat_file_write(lws_fop_fd_t fop_fd,lws_filepos_t * amount,uint8_t * buf,lws_filepos_t len)161 _lws_plat_file_write(lws_fop_fd_t fop_fd, lws_filepos_t *amount,
162 		     uint8_t *buf, lws_filepos_t len)
163 {
164 	long n;
165 
166 	n = write((int)fop_fd->fd, buf, len);
167 	if (n == -1) {
168 		*amount = 0;
169 		return -1;
170 	}
171 
172 	fop_fd->pos += n;
173 	*amount = n;
174 
175 	return 0;
176 }
177 
178