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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
|
// Copyright 2021 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package lisafs
import (
"fmt"
"path"
"path/filepath"
"strings"
"golang.org/x/sys/unix"
"gvisor.dev/gvisor/pkg/flipcall"
"gvisor.dev/gvisor/pkg/fspath"
"gvisor.dev/gvisor/pkg/log"
"gvisor.dev/gvisor/pkg/marshal/primitive"
)
const (
allowedOpenFlags = unix.O_ACCMODE | unix.O_TRUNC
setStatSupportedMask = unix.STATX_MODE | unix.STATX_UID | unix.STATX_GID | unix.STATX_SIZE | unix.STATX_ATIME | unix.STATX_MTIME
)
// RPCHandler defines a handler that is invoked when the associated message is
// received. The handler is responsible for:
//
// * Unmarshalling the request from the passed payload and interpreting it.
// * Marshalling the response into the communicator's payload buffer.
// * Return the number of payload bytes written.
// * Donate any FDs (if needed) to comm which will in turn donate it to client.
type RPCHandler func(c *Connection, comm Communicator, payloadLen uint32) (uint32, error)
var handlers = [...]RPCHandler{
Error: ErrorHandler,
Mount: MountHandler,
Channel: ChannelHandler,
FStat: FStatHandler,
SetStat: SetStatHandler,
Walk: WalkHandler,
WalkStat: WalkStatHandler,
OpenAt: OpenAtHandler,
OpenCreateAt: OpenCreateAtHandler,
Close: CloseHandler,
FSync: FSyncHandler,
PWrite: PWriteHandler,
PRead: PReadHandler,
MkdirAt: MkdirAtHandler,
MknodAt: MknodAtHandler,
SymlinkAt: SymlinkAtHandler,
LinkAt: LinkAtHandler,
FStatFS: FStatFSHandler,
FAllocate: FAllocateHandler,
ReadLinkAt: ReadLinkAtHandler,
Flush: FlushHandler,
Connect: ConnectHandler,
UnlinkAt: UnlinkAtHandler,
RenameAt: RenameAtHandler,
Getdents64: Getdents64Handler,
FGetXattr: FGetXattrHandler,
FSetXattr: FSetXattrHandler,
FListXattr: FListXattrHandler,
FRemoveXattr: FRemoveXattrHandler,
}
// ErrorHandler handles Error message.
func ErrorHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
// Client should never send Error.
return 0, unix.EINVAL
}
// MountHandler handles the Mount RPC. Note that there can not be concurrent
// executions of MountHandler on a connection because the connection enforces
// that Mount is the first message on the connection. Only after the connection
// has been successfully mounted can other channels be created.
func MountHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req MountReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
mountPath := path.Clean(string(req.MountPath))
if !filepath.IsAbs(mountPath) {
log.Warningf("mountPath %q is not absolute", mountPath)
return 0, unix.EINVAL
}
if c.mounted {
log.Warningf("connection has already been mounted at %q", mountPath)
return 0, unix.EBUSY
}
rootFD, rootIno, err := c.ServerImpl().Mount(c, mountPath)
if err != nil {
return 0, err
}
c.server.addMountPoint(rootFD.FD())
c.mounted = true
resp := MountResp{
Root: rootIno,
SupportedMs: c.ServerImpl().SupportedMessages(),
MaxMessageSize: primitive.Uint32(c.ServerImpl().MaxMessageSize()),
}
respPayloadLen := uint32(resp.SizeBytes())
resp.MarshalBytes(comm.PayloadBuf(respPayloadLen))
return respPayloadLen, nil
}
// ChannelHandler handles the Channel RPC.
func ChannelHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
ch, desc, fdSock, err := c.createChannel(c.ServerImpl().MaxMessageSize())
if err != nil {
return 0, err
}
// Start servicing the channel in a separate goroutine.
c.activeWg.Add(1)
go func() {
if err := c.service(ch); err != nil {
// Don't log shutdown error which is expected during server shutdown.
if _, ok := err.(flipcall.ShutdownError); !ok {
log.Warningf("lisafs.Connection.service(channel = @%p): %v", ch, err)
}
}
c.activeWg.Done()
}()
clientDataFD, err := unix.Dup(desc.FD)
if err != nil {
unix.Close(fdSock)
ch.shutdown()
return 0, err
}
// Respond to client with successful channel creation message.
if err := comm.DonateFD(clientDataFD); err != nil {
return 0, err
}
if err := comm.DonateFD(fdSock); err != nil {
return 0, err
}
resp := ChannelResp{
dataOffset: desc.Offset,
dataLength: uint64(desc.Length),
}
respLen := uint32(resp.SizeBytes())
resp.MarshalUnsafe(comm.PayloadBuf(respLen))
return respLen, nil
}
// FStatHandler handles the FStat RPC.
func FStatHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req StatReq
req.UnmarshalUnsafe(comm.PayloadBuf(payloadLen))
fd, err := c.lookupFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
switch t := fd.(type) {
case *ControlFD:
return t.impl.Stat(c, comm)
case *OpenFD:
return t.impl.Stat(c, comm)
default:
panic(fmt.Sprintf("unknown fd type %T", t))
}
}
// SetStatHandler handles the SetStat RPC.
func SetStatHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
if c.readonly {
return 0, unix.EROFS
}
var req SetStatReq
req.UnmarshalUnsafe(comm.PayloadBuf(payloadLen))
fd, err := c.LookupControlFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if req.Mask&^setStatSupportedMask != 0 {
return 0, unix.EPERM
}
return fd.impl.SetStat(c, comm, req)
}
// WalkHandler handles the Walk RPC.
func WalkHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req WalkReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
fd, err := c.LookupControlFD(req.DirFD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if !fd.IsDir() {
return 0, unix.ENOTDIR
}
for _, name := range req.Path {
if err := checkSafeName(name); err != nil {
return 0, err
}
}
return fd.impl.Walk(c, comm, req.Path)
}
// WalkStatHandler handles the WalkStat RPC.
func WalkStatHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req WalkReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
fd, err := c.LookupControlFD(req.DirFD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
// Note that this fd is allowed to not actually be a directory when the
// only path component to walk is "" (self).
if !fd.IsDir() {
if len(req.Path) > 1 || (len(req.Path) == 1 && len(req.Path[0]) > 0) {
return 0, unix.ENOTDIR
}
}
for i, name := range req.Path {
// First component is allowed to be "".
if i == 0 && len(name) == 0 {
continue
}
if err := checkSafeName(name); err != nil {
return 0, err
}
}
return fd.impl.WalkStat(c, comm, req.Path)
}
// OpenAtHandler handles the OpenAt RPC.
func OpenAtHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req OpenAtReq
req.UnmarshalUnsafe(comm.PayloadBuf(payloadLen))
// Only keep allowed open flags.
if allowedFlags := req.Flags & allowedOpenFlags; allowedFlags != req.Flags {
log.Debugf("discarding open flags that are not allowed: old open flags = %d, new open flags = %d", req.Flags, allowedFlags)
req.Flags = allowedFlags
}
accessMode := req.Flags & unix.O_ACCMODE
trunc := req.Flags&unix.O_TRUNC != 0
if c.readonly && (accessMode != unix.O_RDONLY || trunc) {
return 0, unix.EROFS
}
fd, err := c.LookupControlFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if fd.IsDir() {
// Directory is not truncatable and must be opened with O_RDONLY.
if accessMode != unix.O_RDONLY || trunc {
return 0, unix.EISDIR
}
}
return fd.impl.Open(c, comm, req.Flags)
}
// OpenCreateAtHandler handles the OpenCreateAt RPC.
func OpenCreateAtHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
if c.readonly {
return 0, unix.EROFS
}
var req OpenCreateAtReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
// Only keep allowed open flags.
if allowedFlags := req.Flags & allowedOpenFlags; allowedFlags != req.Flags {
log.Debugf("discarding open flags that are not allowed: old open flags = %d, new open flags = %d", req.Flags, allowedFlags)
req.Flags = allowedFlags
}
name := string(req.Name)
if err := checkSafeName(name); err != nil {
return 0, err
}
fd, err := c.LookupControlFD(req.DirFD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if !fd.IsDir() {
return 0, unix.ENOTDIR
}
return fd.impl.OpenCreate(c, comm, req.Mode, req.UID, req.GID, name, uint32(req.Flags))
}
// CloseHandler handles the Close RPC.
func CloseHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req CloseReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
for _, fd := range req.FDs {
c.RemoveFD(fd)
}
// There is no response message for this.
return 0, nil
}
// FSyncHandler handles the FSync RPC.
func FSyncHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req FsyncReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
// Return the first error we encounter, but sync everything we can
// regardless.
var retErr error
for _, fdid := range req.FDs {
if err := c.fsyncFD(fdid); err != nil && retErr == nil {
retErr = err
}
}
// There is no response message for this.
return 0, retErr
}
func (c *Connection) fsyncFD(id FDID) error {
fd, err := c.LookupOpenFD(id)
if err != nil {
return err
}
return fd.impl.Sync(c)
}
// PWriteHandler handles the PWrite RPC.
func PWriteHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
if c.readonly {
return 0, unix.EROFS
}
var req PWriteReq
// Note that it is an optimized Unmarshal operation which avoids any buffer
// allocation and copying. req.Buf just points to payload. This is safe to do
// as the handler owns payload and req's lifetime is limited to the handler.
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
fd, err := c.LookupOpenFD(req.FD)
if err != nil {
return 0, err
}
if !fd.writable {
return 0, unix.EBADF
}
return fd.impl.Write(c, comm, req.Buf, uint64(req.Offset))
}
// PReadHandler handles the PRead RPC.
func PReadHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req PReadReq
req.UnmarshalUnsafe(comm.PayloadBuf(payloadLen))
fd, err := c.LookupOpenFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if !fd.readable {
return 0, unix.EBADF
}
return fd.impl.Read(c, comm, req.Offset, req.Count)
}
// MkdirAtHandler handles the MkdirAt RPC.
func MkdirAtHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
if c.readonly {
return 0, unix.EROFS
}
var req MkdirAtReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
name := string(req.Name)
if err := checkSafeName(name); err != nil {
return 0, err
}
fd, err := c.LookupControlFD(req.DirFD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if !fd.IsDir() {
return 0, unix.ENOTDIR
}
return fd.impl.Mkdir(c, comm, req.Mode, req.UID, req.GID, name)
}
// MknodAtHandler handles the MknodAt RPC.
func MknodAtHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
if c.readonly {
return 0, unix.EROFS
}
var req MknodAtReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
name := string(req.Name)
if err := checkSafeName(name); err != nil {
return 0, err
}
fd, err := c.LookupControlFD(req.DirFD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if !fd.IsDir() {
return 0, unix.ENOTDIR
}
return fd.impl.Mknod(c, comm, req.Mode, req.UID, req.GID, name, uint32(req.Minor), uint32(req.Major))
}
// SymlinkAtHandler handles the SymlinkAt RPC.
func SymlinkAtHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
if c.readonly {
return 0, unix.EROFS
}
var req SymlinkAtReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
name := string(req.Name)
if err := checkSafeName(name); err != nil {
return 0, err
}
fd, err := c.LookupControlFD(req.DirFD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if !fd.IsDir() {
return 0, unix.ENOTDIR
}
return fd.impl.Symlink(c, comm, name, string(req.Target), req.UID, req.GID)
}
// LinkAtHandler handles the LinkAt RPC.
func LinkAtHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
if c.readonly {
return 0, unix.EROFS
}
var req LinkAtReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
name := string(req.Name)
if err := checkSafeName(name); err != nil {
return 0, err
}
fd, err := c.LookupControlFD(req.DirFD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if !fd.IsDir() {
return 0, unix.ENOTDIR
}
targetFD, err := c.LookupControlFD(req.Target)
if err != nil {
return 0, err
}
return targetFD.impl.Link(c, comm, fd.impl, name)
}
// FStatFSHandler handles the FStatFS RPC.
func FStatFSHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req FStatFSReq
req.UnmarshalUnsafe(comm.PayloadBuf(payloadLen))
fd, err := c.LookupControlFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
return fd.impl.StatFS(c, comm)
}
// FAllocateHandler handles the FAllocate RPC.
func FAllocateHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
if c.readonly {
return 0, unix.EROFS
}
var req FAllocateReq
req.UnmarshalUnsafe(comm.PayloadBuf(payloadLen))
fd, err := c.LookupOpenFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if !fd.writable {
return 0, unix.EBADF
}
return 0, fd.impl.Allocate(c, req.Mode, req.Offset, req.Length)
}
// ReadLinkAtHandler handles the ReadLinkAt RPC.
func ReadLinkAtHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req ReadLinkAtReq
req.UnmarshalUnsafe(comm.PayloadBuf(payloadLen))
fd, err := c.LookupControlFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if !fd.IsSymlink() {
return 0, unix.EINVAL
}
return fd.impl.Readlink(c, comm)
}
// FlushHandler handles the Flush RPC.
func FlushHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req FlushReq
req.UnmarshalUnsafe(comm.PayloadBuf(payloadLen))
fd, err := c.LookupOpenFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
return 0, fd.impl.Flush(c)
}
// ConnectHandler handles the Connect RPC.
func ConnectHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req ConnectReq
req.UnmarshalUnsafe(comm.PayloadBuf(payloadLen))
fd, err := c.LookupControlFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if !fd.IsSocket() {
return 0, unix.ENOTSOCK
}
return 0, fd.impl.Connect(c, comm, req.SockType)
}
// UnlinkAtHandler handles the UnlinkAt RPC.
func UnlinkAtHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
if c.readonly {
return 0, unix.EROFS
}
var req UnlinkAtReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
name := string(req.Name)
if err := checkSafeName(name); err != nil {
return 0, err
}
fd, err := c.LookupControlFD(req.DirFD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if !fd.IsDir() {
return 0, unix.ENOTDIR
}
return 0, fd.impl.Unlink(c, name, uint32(req.Flags))
}
// RenameAtHandler handles the RenameAt RPC.
func RenameAtHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
if c.readonly {
return 0, unix.EROFS
}
var req RenameAtReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
newName := string(req.NewName)
if err := checkSafeName(newName); err != nil {
return 0, err
}
renamed, err := c.LookupControlFD(req.Renamed)
if err != nil {
return 0, err
}
defer renamed.DecRef(nil)
newDir, err := c.LookupControlFD(req.NewDir)
if err != nil {
return 0, err
}
defer newDir.DecRef(nil)
if !newDir.IsDir() {
return 0, unix.ENOTDIR
}
// Hold RenameMu for writing during rename, this is important.
c.server.RenameMu.Lock()
defer c.server.RenameMu.Unlock()
if renamed.parent == nil {
// renamed is root.
return 0, unix.EBUSY
}
oldParentPath := renamed.parent.FilePathLocked()
oldPath := oldParentPath + "/" + renamed.name
if newName == renamed.name && oldParentPath == newDir.FilePathLocked() {
// Nothing to do.
return 0, nil
}
updateControlFD, cleanUp, err := renamed.impl.RenameLocked(c, newDir.impl, newName)
if err != nil {
return 0, err
}
c.server.forEachMountPoint(func(root *ControlFD) {
if !strings.HasPrefix(oldPath, root.name) {
return
}
pit := fspath.Parse(oldPath[len(root.name):]).Begin
root.renameRecursiveLocked(newDir, newName, pit, updateControlFD)
})
if cleanUp != nil {
cleanUp()
}
return 0, nil
}
// Precondition: rename mutex must be locked for writing.
func (fd *ControlFD) renameRecursiveLocked(newDir *ControlFD, newName string, pit fspath.Iterator, updateControlFD func(ControlFDImpl)) {
if !pit.Ok() {
// fd should be renamed.
fd.clearParentLocked()
fd.setParentLocked(newDir)
fd.name = newName
if updateControlFD != nil {
updateControlFD(fd.impl)
}
return
}
cur := pit.String()
next := pit.Next()
// No need to hold fd.childrenMu because RenameMu is locked for writing.
for child := fd.children.Front(); child != nil; child = child.Next() {
if child.name == cur {
child.renameRecursiveLocked(newDir, newName, next, updateControlFD)
}
}
}
// Getdents64Handler handles the Getdents64 RPC.
func Getdents64Handler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req Getdents64Req
req.UnmarshalUnsafe(comm.PayloadBuf(payloadLen))
fd, err := c.LookupOpenFD(req.DirFD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
if !fd.controlFD.IsDir() {
return 0, unix.ENOTDIR
}
seek0 := false
if req.Count < 0 {
seek0 = true
req.Count = -req.Count
}
return fd.impl.Getdent64(c, comm, uint32(req.Count), seek0)
}
// FGetXattrHandler handles the FGetXattr RPC.
func FGetXattrHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req FGetXattrReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
fd, err := c.LookupControlFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
return fd.impl.GetXattr(c, comm, string(req.Name), uint32(req.BufSize))
}
// FSetXattrHandler handles the FSetXattr RPC.
func FSetXattrHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
if c.readonly {
return 0, unix.EROFS
}
var req FSetXattrReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
fd, err := c.LookupControlFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
return 0, fd.impl.SetXattr(c, string(req.Name), string(req.Value), uint32(req.Flags))
}
// FListXattrHandler handles the FListXattr RPC.
func FListXattrHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
var req FListXattrReq
req.UnmarshalUnsafe(comm.PayloadBuf(payloadLen))
fd, err := c.LookupControlFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
return fd.impl.ListXattr(c, comm, req.Size)
}
// FRemoveXattrHandler handles the FRemoveXattr RPC.
func FRemoveXattrHandler(c *Connection, comm Communicator, payloadLen uint32) (uint32, error) {
if c.readonly {
return 0, unix.EROFS
}
var req FRemoveXattrReq
req.UnmarshalBytes(comm.PayloadBuf(payloadLen))
fd, err := c.LookupControlFD(req.FD)
if err != nil {
return 0, err
}
defer fd.DecRef(nil)
return 0, fd.impl.RemoveXattr(c, comm, string(req.Name))
}
// checkSafeName validates the name and returns nil or returns an error.
func checkSafeName(name string) error {
if name != "" && !strings.Contains(name, "/") && name != "." && name != ".." {
return nil
}
return unix.EINVAL
}
|