1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/* fhandler_raw.cc. See fhandler.h for a description of the fhandler classes.
Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2011,
2012, 2013 Red Hat, Inc.
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include <unistd.h>
#include <cygwin/rdevio.h>
#include <sys/mtio.h>
#include <sys/param.h>
#include "cygerrno.h"
#include "path.h"
#include "fhandler.h"
/**********************************************************************/
/* fhandler_dev_raw */
fhandler_dev_raw::fhandler_dev_raw ()
: fhandler_base (),
status ()
{
need_fork_fixup (true);
}
fhandler_dev_raw::~fhandler_dev_raw ()
{
if (devbufsiz > 1L)
delete [] devbufalloc;
}
int __reg2
fhandler_dev_raw::fstat (struct stat *buf)
{
debug_printf ("here");
fhandler_base::fstat (buf);
if (is_auto_device ())
{
if (get_major () == DEV_TAPE_MAJOR)
buf->st_mode = S_IFCHR | STD_RBITS | STD_WBITS | S_IWGRP | S_IWOTH;
else
buf->st_mode = S_IFBLK | STD_RBITS | STD_WBITS | S_IWGRP | S_IWOTH;
buf->st_uid = geteuid32 ();
buf->st_gid = getegid32 ();
buf->st_nlink = 1;
buf->st_blksize = PREFERRED_IO_BLKSIZE;
time_as_timestruc_t (&buf->st_ctim);
buf->st_atim = buf->st_mtim = buf->st_birthtim = buf->st_ctim;
}
return 0;
}
int
fhandler_dev_raw::open (int flags, mode_t)
{
/* Check for illegal flags. */
if (get_major () != DEV_TAPE_MAJOR && (flags & O_APPEND))
{
set_errno (EINVAL);
return 0;
}
/* Always open a raw device existing and binary. */
flags &= ~(O_CREAT | O_TRUNC);
flags |= O_BINARY;
/* Write-only doesn't work well with raw devices */
if ((flags & O_ACCMODE) == O_WRONLY)
flags = ((flags & ~O_WRONLY) | O_RDWR);
int res = fhandler_base::open (flags, 0);
return res;
}
int
fhandler_dev_raw::dup (fhandler_base *child, int flags)
{
int ret = fhandler_base::dup (child, flags);
if (!ret)
{
fhandler_dev_raw *fhc = (fhandler_dev_raw *) child;
if (devbufsiz > 1L)
{
/* Create sector-aligned buffer */
fhc->devbufalloc = new char [devbufsiz + devbufalign];
fhc->devbuf = (char *) roundup2 ((uintptr_t) fhc->devbufalloc,
(uintptr_t) devbufalign);
}
fhc->devbufstart = 0;
fhc->devbufend = 0;
fhc->lastblk_to_read (false);
}
return ret;
}
void
fhandler_dev_raw::fixup_after_fork (HANDLE parent)
{
fhandler_base::fixup_after_fork (parent);
devbufstart = 0;
devbufend = 0;
lastblk_to_read (false);
}
void
fhandler_dev_raw::fixup_after_exec ()
{
if (!close_on_exec ())
{
if (devbufsiz > 1L)
{
/* Create sector-aligned buffer */
devbufalloc = new char [devbufsiz + devbufalign];
devbuf = (char *) roundup2 ((uintptr_t) devbufalloc,
(uintptr_t) devbufalign);
}
devbufstart = 0;
devbufend = 0;
lastblk_to_read (false);
}
}
int
fhandler_dev_raw::ioctl (unsigned int cmd, void *buf)
{
int ret = NO_ERROR;
if (cmd == RDIOCDOP)
{
struct rdop *op = (struct rdop *) buf;
if (!op)
ret = ERROR_INVALID_PARAMETER;
else
switch (op->rd_op)
{
case RDSETBLK:
if (get_major () == DEV_TAPE_MAJOR)
{
struct mtop mop;
mop.mt_op = MTSETBLK;
mop.mt_count = op->rd_parm;
ret = ioctl (MTIOCTOP, &mop);
}
else if ((op->rd_parm <= 1 && get_major () != DEV_TAPE_MAJOR)
|| (op->rd_parm > 1 && (op->rd_parm % devbufalign))
|| (get_flags () & O_DIRECT))
/* The conditions for a valid parameter are:
- The new size is either 0 or 1, both indicating unbuffered
I/O, and the device is a tape device.
- Or, the new buffersize must be a multiple of the
required buffer alignment.
- In the O_DIRECT case, the whole request is invalid. */
ret = ERROR_INVALID_PARAMETER;
else if (!devbuf || op->rd_parm != devbufsiz)
{
char *buf = NULL;
off_t curpos = lseek (0, SEEK_CUR);
if (op->rd_parm > 1L)
buf = new char [op->rd_parm + devbufalign];
if (devbufsiz > 1L)
delete [] devbufalloc;
devbufalloc = buf;
devbuf = (char *) roundup2 ((uintptr_t) buf,
(uintptr_t) devbufalign);
devbufsiz = op->rd_parm ?: 1L;
devbufstart = devbufend = 0;
lseek (curpos, SEEK_SET);
}
break;
default:
break;
}
}
else if (cmd == RDIOCGET)
{
struct rdget *get = (struct rdget *) buf;
if (!get)
ret = ERROR_INVALID_PARAMETER;
else
get->bufsiz = devbufsiz;
}
else
return fhandler_base::ioctl (cmd, buf);
if (ret != NO_ERROR)
{
SetLastError (ret);
__seterrno ();
return -1;
}
return 0;
}
|