1
0
mirror of https://github.com/samba-team/samba.git synced 2025-03-27 22:50:26 +03:00

lib: Split out write_data[_iov]

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
This commit is contained in:
Volker Lendecke 2014-11-19 14:25:56 +00:00 committed by Jeremy Allison
parent 214fc09a34
commit 0013001e70
18 changed files with 156 additions and 85 deletions

View File

@ -567,8 +567,6 @@ NTSTATUS read_fd_with_timeout(int fd, char *buf,
size_t *size_ret);
NTSTATUS read_data_ntstatus(int fd, char *buffer, size_t N);
ssize_t write_data(int fd, const char *buffer, size_t N);
ssize_t write_data_iov(int fd, const struct iovec *orig_iov, int iovcnt);
bool send_keepalive(int client);
NTSTATUS read_smb_length_return_keepalive(int fd, char *inbuf,
unsigned int timeout,

View File

@ -23,6 +23,7 @@
#include "serverid.h"
#include "ctdbd_conn.h"
#include "system/select.h"
#include "lib/sys_rw_data.h"
#include "messages.h"

107
source3/lib/sys_rw_data.c Normal file
View File

@ -0,0 +1,107 @@
/*
* Unix SMB/CIFS implementation.
* Samba system utilities
* Copyright (C) Andrew Tridgell 1992-1998
* Copyright (C) Jeremy Allison 1998-2005
* Copyright (C) Timur Bakeyev 2005
* Copyright (C) Bjoern Jacke 2006-2007
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "replace.h"
#include "system/filesys.h"
#include "lib/sys_rw_data.h"
#include "lib/sys_rw.h"
#include "lib/iov_buf.h"
/****************************************************************************
Write all data from an iov array
NB. This can be called with a non-socket fd, don't add dependencies
on socket calls.
****************************************************************************/
ssize_t write_data_iov(int fd, const struct iovec *orig_iov, int iovcnt)
{
ssize_t to_send;
ssize_t thistime;
size_t sent;
struct iovec iov_copy[iovcnt];
struct iovec *iov;
to_send = iov_buflen(orig_iov, iovcnt);
if (to_send == -1) {
errno = EINVAL;
return -1;
}
thistime = sys_writev(fd, orig_iov, iovcnt);
if ((thistime <= 0) || (thistime == to_send)) {
return thistime;
}
sent = thistime;
/*
* We could not send everything in one call. Make a copy of iov that
* we can mess with. We keep a copy of the array start in iov_copy for
* the TALLOC_FREE, because we're going to modify iov later on,
* discarding elements.
*/
memcpy(iov_copy, orig_iov, sizeof(struct iovec) * iovcnt);
iov = iov_copy;
while (sent < to_send) {
/*
* We have to discard "thistime" bytes from the beginning
* iov array, "thistime" contains the number of bytes sent
* via writev last.
*/
while (thistime > 0) {
if (thistime < iov[0].iov_len) {
char *new_base =
(char *)iov[0].iov_base + thistime;
iov[0].iov_base = (void *)new_base;
iov[0].iov_len -= thistime;
break;
}
thistime -= iov[0].iov_len;
iov += 1;
iovcnt -= 1;
}
thistime = sys_writev(fd, iov, iovcnt);
if (thistime <= 0) {
break;
}
sent += thistime;
}
return sent;
}
/****************************************************************************
Write data to a fd.
NB. This can be called with a non-socket fd, don't add dependencies
on socket calls.
****************************************************************************/
ssize_t write_data(int fd, const char *buffer, size_t n)
{
struct iovec iov;
iov.iov_base = discard_const_p(void, buffer);
iov.iov_len = n;
return write_data_iov(fd, &iov, 1);
}

33
source3/lib/sys_rw_data.h Normal file
View File

@ -0,0 +1,33 @@
/*
* Unix SMB/CIFS implementation.
* Samba system utilities
* Copyright (C) Andrew Tridgell 1992-1998
* Copyright (C) Jeremy Allison 1998-2005
* Copyright (C) Timur Bakeyev 2005
* Copyright (C) Bjoern Jacke 2006-2007
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __LIB_SYS_RW_DATA_H__
#define __LIB_SYS_RW_DATA_H__
#include <unistd.h>
struct iovec;
ssize_t write_data_iov(int fd, const struct iovec *iov, int iovcnt);
ssize_t write_data(int fd, const char *buffer, size_t n);
#endif

View File

@ -32,6 +32,7 @@
#include "libcli/security/security.h"
#include "serverid.h"
#include "lib/sys_rw.h"
#include "lib/sys_rw_data.h"
#ifdef HAVE_SYS_PRCTL_H
#include <sys/prctl.h>

View File

@ -29,7 +29,7 @@
#include "../lib/util/tevent_ntstatus.h"
#include "../lib/tsocket/tsocket.h"
#include "lib/sys_rw.h"
#include "lib/iov_buf.h"
#include "lib/sys_rw_data.h"
const char *client_addr(int fd, char *addr, size_t addrlen)
{
@ -203,86 +203,6 @@ NTSTATUS read_data_ntstatus(int fd, char *buffer, size_t N)
return read_fd_with_timeout(fd, buffer, N, N, 0, NULL);
}
/****************************************************************************
Write all data from an iov array
NB. This can be called with a non-socket fd, don't add dependencies
on socket calls.
****************************************************************************/
ssize_t write_data_iov(int fd, const struct iovec *orig_iov, int iovcnt)
{
ssize_t to_send;
ssize_t thistime;
size_t sent;
struct iovec iov_copy[iovcnt];
struct iovec *iov;
to_send = iov_buflen(orig_iov, iovcnt);
if (to_send == -1) {
errno = EINVAL;
return -1;
}
thistime = sys_writev(fd, orig_iov, iovcnt);
if ((thistime <= 0) || (thistime == to_send)) {
return thistime;
}
sent = thistime;
/*
* We could not send everything in one call. Make a copy of iov that
* we can mess with. We keep a copy of the array start in iov_copy for
* the TALLOC_FREE, because we're going to modify iov later on,
* discarding elements.
*/
memcpy(iov_copy, orig_iov, sizeof(struct iovec) * iovcnt);
iov = iov_copy;
while (sent < to_send) {
/*
* We have to discard "thistime" bytes from the beginning
* iov array, "thistime" contains the number of bytes sent
* via writev last.
*/
while (thistime > 0) {
if (thistime < iov[0].iov_len) {
char *new_base =
(char *)iov[0].iov_base + thistime;
iov[0].iov_base = (void *)new_base;
iov[0].iov_len -= thistime;
break;
}
thistime -= iov[0].iov_len;
iov += 1;
iovcnt -= 1;
}
thistime = sys_writev(fd, iov, iovcnt);
if (thistime <= 0) {
break;
}
sent += thistime;
}
return sent;
}
/****************************************************************************
Write data to a fd.
NB. This can be called with a non-socket fd, don't add dependencies
on socket calls.
****************************************************************************/
ssize_t write_data(int fd, const char *buffer, size_t N)
{
struct iovec iov;
iov.iov_base = discard_const_p(void, buffer);
iov.iov_len = N;
return write_data_iov(fd, &iov, 1);
}
/****************************************************************************
Send a keepalive packet (rfc1002).
****************************************************************************/

View File

@ -27,6 +27,7 @@
#include "lib/async_req/async_sock.h"
#include "lib/util/tevent_unix.h"
#include "lib/sys_rw.h"
#include "lib/sys_rw_data.h"
#if !defined(HAVE_STRUCT_MSGHDR_MSG_CONTROL) && !defined(HAVE_STRUCT_MSGHDR_MSG_ACCRIGHTS)
# error Can not pass file descriptors

View File

@ -21,6 +21,7 @@
#include "includes.h"
#include "system/filesys.h"
#include "smbd/smbd.h"
#include "lib/sys_rw_data.h"
struct preopen_state;

View File

@ -29,6 +29,7 @@
#include "../librpc/gen_ndr/ndr_netlogon.h"
#include "auth.h"
#include "../lib/tsocket/tsocket.h"
#include "lib/sys_rw_data.h"
/* abstraction for the send_over_network function */
enum sock_type {INTERNET_SOCKET = 0, UNIX_DOMAIN_SOCKET};

View File

@ -19,6 +19,7 @@
#include "includes.h"
#include "nmbd/nmbd.h"
#include "lib/sys_rw_data.h"
/***************************************************************************
Add a DNS result to the name cache.

View File

@ -36,6 +36,7 @@
#include "messages.h"
#include "util_tdb.h"
#include "lib/param/loadparm.h"
#include "lib/sys_rw_data.h"
extern struct current_user current_user;
extern userdom_struct current_user_info;

View File

@ -39,6 +39,7 @@
#include "../libcli/security/dom_sid.h"
#include "../libcli/security/security_token.h"
#include "lib/id_cache.h"
#include "lib/sys_rw_data.h"
#include "serverid.h"
#include "system/threads.h"

View File

@ -43,6 +43,7 @@
#include "../lib/tsocket/tsocket.h"
#include "lib/tevent_wait.h"
#include "libcli/smb/smb_signing.h"
#include "lib/sys_rw_data.h"
/****************************************************************************
Ensure we check the path in *exactly* the same way as W2K for a findfirst/findnext

View File

@ -26,6 +26,7 @@
#include "libcli/security/security.h"
#include "../lib/util/tevent_ntstatus.h"
#include "rpc_server/srv_pipe_hnd.h"
#include "lib/sys_rw_data.h"
static struct tevent_req *smbd_smb2_read_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,

View File

@ -41,6 +41,7 @@
#include "util_tdb.h"
#include "../libcli/smb/read_smb.h"
#include "../libcli/smb/smbXcli_base.h"
#include "lib/sys_rw_data.h"
extern char *optarg;
extern int optind;

View File

@ -22,6 +22,7 @@
#include "system/select.h"
#include "../lib/util/select.h"
#include "libsmb/nmblib.h"
#include "lib/sys_rw_data.h"
#define SECURITY_MASK 0
#define SECURITY_SET 0

View File

@ -39,6 +39,7 @@
#include "../lib/util/tevent_unix.h"
#include "lib/param/loadparm.h"
#include "lib/sys_rw.h"
#include "lib/sys_rw_data.h"
#undef DBGC_CLASS
#define DBGC_CLASS DBGC_WINBIND

View File

@ -254,8 +254,8 @@ bld.SAMBA3_SUBSYSTEM('KRBCLIENT',
public_deps='krb5samba k5crypto gssapi LIBTSOCKET CLDAP LIBNMB')
bld.SAMBA3_LIBRARY('sys_rw',
source='lib/sys_rw.c',
deps='replace',
source='lib/sys_rw.c lib/sys_rw_data.c',
deps='replace iov_buf',
private_library=True)
bld.SAMBA3_LIBRARY('iov_buf',