2 * Unix SMB/CIFS implementation.
3 * SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 3 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "lib/util_file.h"
22 #include "lib/util/debug.h"
23 #include "lib/util/samba_util.h"
24 #include "lib/util/sys_rw.h"
25 #include "lib/sys_popen.h"
26 #include "lib/async_req/async_sock.h"
27 #include "lib/util/tevent_unix.h"
29 struct file_pload_state
{
30 struct tevent_context
*ev
;
36 static int file_pload_state_destructor(struct file_pload_state
*s
);
37 static void file_pload_readable(struct tevent_req
*subreq
);
39 struct tevent_req
*file_pload_send(TALLOC_CTX
*mem_ctx
,
40 struct tevent_context
*ev
,
41 const char *syscmd
, size_t maxsize
)
43 struct tevent_req
*req
, *subreq
;
44 struct file_pload_state
*state
;
46 req
= tevent_req_create(mem_ctx
, &state
, struct file_pload_state
);
51 state
->maxsize
= maxsize
;
53 state
->fd
= sys_popen(syscmd
);
54 if (state
->fd
== -1) {
55 tevent_req_error(req
, errno
);
56 return tevent_req_post(req
, ev
);
58 talloc_set_destructor(state
, file_pload_state_destructor
);
60 subreq
= wait_for_read_send(state
, state
->ev
, state
->fd
, false);
61 if (tevent_req_nomem(subreq
, req
)) {
62 return tevent_req_post(req
, ev
);
64 tevent_req_set_callback(subreq
, file_pload_readable
, req
);
68 static int file_pload_state_destructor(struct file_pload_state
*s
)
77 static void file_pload_readable(struct tevent_req
*subreq
)
79 struct tevent_req
*req
= tevent_req_callback_data(
80 subreq
, struct tevent_req
);
81 struct file_pload_state
*state
= tevent_req_data(
82 req
, struct file_pload_state
);
90 ok
= wait_for_read_recv(subreq
, &err
);
93 tevent_req_error(req
, err
);
97 nread
= sys_read(state
->fd
, buf
, sizeof(buf
));
99 tevent_req_error(req
, errno
);
103 tevent_req_done(req
);
107 bufsize
= talloc_get_size(state
->buf
);
109 if (((bufsize
+ nread
) < bufsize
) ||
110 ((bufsize
+ nread
+ 1) < bufsize
)) {
112 tevent_req_error(req
, EMSGSIZE
);
116 if ((state
->maxsize
!= 0) && ((bufsize
+ nread
) > state
->maxsize
)) {
117 tevent_req_error(req
, EMSGSIZE
);
121 tmp
= talloc_realloc(state
, state
->buf
, uint8_t, bufsize
+ nread
+ 1);
122 if (tevent_req_nomem(tmp
, req
)) {
127 memcpy(state
->buf
+ bufsize
, buf
, nread
);
128 state
->buf
[bufsize
+nread
] = '\0';
130 subreq
= wait_for_read_send(state
, state
->ev
, state
->fd
, false);
131 if (tevent_req_nomem(subreq
, req
)) {
134 tevent_req_set_callback(subreq
, file_pload_readable
, req
);
137 int file_pload_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
140 struct file_pload_state
*state
= tevent_req_data(
141 req
, struct file_pload_state
);
144 if (tevent_req_is_unix_error(req
, &err
)) {
147 *buf
= talloc_move(mem_ctx
, &state
->buf
);
149 tevent_req_received(req
);
155 Load from a pipe into memory.
158 static char *file_pload(const char *syscmd
, size_t *size
)
165 fd
= sys_popen(syscmd
);
173 while ((n
= sys_read(fd
, buf
, sizeof(buf
))) > 0) {
174 p
= talloc_realloc(NULL
, p
, char, total
+ n
+ 1);
176 DEBUG(0,("file_pload: failed to expand buffer!\n"));
180 memcpy(p
+total
, buf
, n
);
188 /* FIXME: Perhaps ought to check that the command completed
189 * successfully (returned 0); if not the data may be
203 Load a pipe into memory and return an array of pointers to lines in the data
204 must be freed with TALLOC_FREE.
207 char **file_lines_pload(TALLOC_CTX
*mem_ctx
, const char *syscmd
,
213 p
= file_pload(syscmd
, &size
);
218 return file_lines_parse(p
, size
, numlines
, mem_ctx
);