vfs_error_inject: add pwrite
[Samba.git] / source3 / modules / vfs_error_inject.c
blob9f0a25fb73f15c103445d0a8c820c4c8fdd9f839
1 /*
2 * Unix SMB/CIFS implementation.
3 * Samba VFS module for error injection in VFS calls
4 * Copyright (C) Christof Schmitt 2017
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "smbd/smbd.h"
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_VFS
26 struct unix_error_map {
27 const char *err_str;
28 int error;
29 } unix_error_map_array[] = {
30 { "ESTALE", ESTALE },
33 static int find_unix_error_from_string(const char *err_str)
35 int i;
37 for (i = 0; i < ARRAY_SIZE(unix_error_map_array); i++) {
38 struct unix_error_map *m = &unix_error_map_array[i];
40 if (strequal(err_str, m->err_str)) {
41 return m->error;
45 return 0;
48 static int inject_unix_error(const char *vfs_func, vfs_handle_struct *handle)
50 const char *err_str;
52 err_str = lp_parm_const_string(SNUM(handle->conn),
53 "error_inject", vfs_func, NULL);
55 if (err_str != NULL) {
56 int error;
58 error = find_unix_error_from_string(err_str);
59 if (error != 0) {
60 DBG_WARNING("Returning error %s for VFS function %s\n",
61 err_str, vfs_func);
62 return error;
65 if (strequal(err_str, "panic")) {
66 DBG_ERR("Panic in VFS function %s\n", vfs_func);
67 smb_panic("error_inject");
70 DBG_ERR("Unknown error inject %s requested "
71 "for vfs function %s\n", err_str, vfs_func);
74 return 0;
77 static int vfs_error_inject_chdir(vfs_handle_struct *handle,
78 const struct smb_filename *smb_fname)
80 int error;
82 error = inject_unix_error("chdir", handle);
83 if (error != 0) {
84 errno = error;
85 return -1;
88 return SMB_VFS_NEXT_CHDIR(handle, smb_fname);
91 static ssize_t vfs_error_inject_pwrite(vfs_handle_struct *handle,
92 files_struct *fsp,
93 const void *data,
94 size_t n,
95 off_t offset)
97 int error;
99 error = inject_unix_error("pwrite", handle);
100 if (error != 0) {
101 errno = error;
102 return -1;
105 return SMB_VFS_NEXT_PWRITE(handle, fsp, data, n, offset);
108 static struct vfs_fn_pointers vfs_error_inject_fns = {
109 .chdir_fn = vfs_error_inject_chdir,
110 .pwrite_fn = vfs_error_inject_pwrite,
113 static_decl_vfs;
114 NTSTATUS vfs_error_inject_init(TALLOC_CTX *ctx)
116 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "error_inject",
117 &vfs_error_inject_fns);