This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
/
pods.go
1222 lines (1062 loc) · 34 KB
/
pods.go
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
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2015 The rkt 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 pod
import (
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"syscall"
"time"
"github.com/appc/spec/schema"
"github.com/appc/spec/schema/types"
"github.com/hashicorp/errwrap"
"github.com/opencontainers/selinux/go-selinux/label"
"github.com/pborman/uuid"
"github.com/rkt/rkt/common"
"github.com/rkt/rkt/networking/netinfo"
"github.com/rkt/rkt/pkg/lock"
"github.com/rkt/rkt/pkg/sys"
)
// Pod is the struct that reflects a pod and its lifecycle.
// It provides the necessary methods for state transitions and methods for querying internal state.
//
// Unless documented otherwise methods do not refresh the pod state as it is reflected on the file system
// but only the pod state at the point where this struct was created.
//
// See Documentation/devel/pod-lifecycle.md for some explanation.
type Pod struct {
UUID *types.UUID
Nets []netinfo.NetInfo // list of networks (name, IP, iface) this pod is using
MountLabel string // Label to use for container image
*lock.FileLock // the lock for the whole pod
manifestLock *lock.FileLock // the lock for the pod manifest in case this pod is mutable
dataDir string // The data directory where the pod lives in.
createdByMe bool // true if we're the creator of this pod (only the creator can ToPrepare or ToRun directly from preparing)
mutable bool // if true, the pod manifest of the underlying pod can be modified
isEmbryo bool // directory starts as embryo before entering preparing state, serves as stage for acquiring lock before rename to prepare/.
isPreparing bool // when locked at pods/prepare/$uuid the pod is actively being prepared
isAbortedPrepare bool // when unlocked at pods/prepare/$uuid the pod never finished preparing
isPrepared bool // when at pods/prepared/$uuid the pod is prepared, serves as stage for acquiring lock before rename to run/.
isExited bool // when locked at pods/run/$uuid the pod is running, when unlocked it's exited.
isExitedGarbage bool // when unlocked at pods/exited-garbage/$uuid the pod is exited and is garbage
isExitedDeleting bool // when locked at pods/exited-garbage/$uuid the pod is exited, garbage, and is being actively deleted
isGarbage bool // when unlocked at pods/garbage/$uuid the pod is garbage that never ran
isDeleting bool // when locked at pods/garbage/$uuid the pod is garbage that never ran, and is being actively deleted
isGone bool // when a pod no longer can be located at its uuid anywhere XXX: only set by refreshState()
}
// Exported state. See Documentation/devel/pod-lifecycle.md for some explanation
const (
Embryo = "embryo"
Preparing = "preparing"
AbortedPrepare = "aborted prepare"
Prepared = "prepared"
Running = "running"
Deleting = "deleting"
ExitedDeleting = "exited deleting"
Exited = "exited"
ExitedGarbage = "exited garbage"
Garbage = "garbage"
)
type IncludeMask byte
const (
IncludeEmbryoDir IncludeMask = 1 << iota
IncludePrepareDir
IncludePreparedDir
IncludeRunDir
IncludeExitedGarbageDir
IncludeGarbageDir
IncludeMostDirs IncludeMask = (IncludeRunDir | IncludeExitedGarbageDir | IncludePrepareDir | IncludePreparedDir)
IncludeAllDirs IncludeMask = (IncludeMostDirs | IncludeEmbryoDir | IncludeGarbageDir)
)
var (
podsInitialized = false
)
// embryoDir returns where pod directories are created and locked before moving to prepared
func embryoDir(dataDir string) string {
return filepath.Join(dataDir, "pods", "embryo")
}
// prepareDir returns where pod trees reside during (locked) and after failing to complete preparation (unlocked)
func prepareDir(dataDir string) string {
return filepath.Join(dataDir, "pods", "prepare")
}
// prepareDir returns where pod trees reside upon successful preparation
func preparedDir(dataDir string) string {
return filepath.Join(dataDir, "pods", "prepared")
}
// runDir returns where pod trees reside once run
func runDir(dataDir string) string {
return filepath.Join(dataDir, "pods", "run")
}
// exitedGarbageDir returns where pod trees reside once exited & marked as garbage by a gc pass
func exitedGarbageDir(dataDir string) string {
return filepath.Join(dataDir, "pods", "exited-garbage")
}
// garbageDir returns where never-executed pod trees reside once marked as garbage by a gc pass (failed prepares, expired prepareds)
func garbageDir(dataDir string) string {
return filepath.Join(dataDir, "pods", "garbage")
}
// initPods creates the required global directories
func initPods(dataDir string) error {
if !podsInitialized {
dirs := []string{embryoDir(dataDir), prepareDir(dataDir), preparedDir(dataDir), runDir(dataDir), exitedGarbageDir(dataDir), garbageDir(dataDir)}
for _, d := range dirs {
if err := os.MkdirAll(d, 0750); err != nil {
return errwrap.Wrap(errors.New("error creating directory"), err)
}
}
podsInitialized = true
}
return nil
}
// NewPod creates a new pod directory in the "preparing" state, allocating a unique uuid for it in the process.
// The returned pod is always left in an exclusively locked state (preparing is locked in the prepared directory)
// The pod must be closed using pod.Close()
func NewPod(dataDir string) (*Pod, error) {
if err := initPods(dataDir); err != nil {
return nil, err
}
p := &Pod{
dataDir: dataDir,
createdByMe: true,
isEmbryo: true, // starts as an embryo, then ToPreparing locks, renames, and sets isPreparing
// rest start false.
}
var err error
p.UUID, err = types.NewUUID(uuid.New())
if err != nil {
return nil, errwrap.Wrap(errors.New("error creating UUID"), err)
}
err = os.Mkdir(p.embryoPath(), 0750)
if err != nil {
return nil, err
}
p.FileLock, err = lock.NewLock(p.embryoPath(), lock.Dir)
if err != nil {
os.Remove(p.embryoPath())
return nil, err
}
err = p.ToPreparing()
if err != nil {
return nil, err
}
// At this point we we have:
// /var/lib/rkt/pods/prepare/$uuid << exclusively locked to indicate "preparing"
return p, nil
}
// getPod returns a pod struct representing the given pod.
// The returned lock is always left in an open but unlocked state.
// The pod must be closed using pod.Close()
func getPod(dataDir string, uuid *types.UUID) (*Pod, error) {
if err := initPods(dataDir); err != nil {
return nil, err
}
p := &Pod{UUID: uuid, dataDir: dataDir}
// dirStates is a list of directories -> state that directory existing
// implies.
// Its order matches the order states occur.
dirStates := []struct {
dir string
impliedStates []*bool
}{
{dir: p.embryoPath(), impliedStates: []*bool{&p.isEmbryo}},
// For prepare, assume it's aborted prepare until it gets updated below
{dir: p.preparePath(), impliedStates: []*bool{&p.isAbortedPrepare}},
{dir: p.preparedPath(), impliedStates: []*bool{&p.isPrepared}},
// For run, assume exited until the lock is tested
{dir: p.runPath(), impliedStates: []*bool{&p.isExited}},
// Exited garbage implies exited
{dir: p.exitedGarbagePath(), impliedStates: []*bool{&p.isExitedGarbage, &p.isExited}},
{dir: p.garbagePath(), impliedStates: []*bool{&p.isGarbage}},
}
var l *lock.FileLock
var err error
for _, dirState := range dirStates {
l, err = lock.NewLock(dirState.dir, lock.Dir)
if err == nil {
for _, s := range dirState.impliedStates {
*s = true
}
break
}
if err == lock.ErrNotExist {
continue
}
// unexpected lock error
return nil, errwrap.Wrap(fmt.Errorf("error opening pod %q", uuid), err)
}
if err == lock.ErrNotExist {
// This means it didn't exist in any state, something else might have
// deleted it.
return nil, errwrap.Wrap(fmt.Errorf("pod %q was not present", uuid), err)
}
if !p.isPrepared && !p.isEmbryo {
// preparing, run, exitedGarbage, and garbage dirs use exclusive locks to indicate preparing/aborted, running/exited, and deleting/marked
if err = l.TrySharedLock(); err != nil {
if err != lock.ErrLocked {
l.Close()
return nil, errwrap.Wrap(errors.New("unexpected lock error"), err)
}
if p.isExitedGarbage {
// locked exitedGarbage is also being deleted
p.isExitedDeleting = true
} else if p.isExited {
// locked exited and !exitedGarbage is not exited (default in the run dir)
p.isExited = false
} else if p.isAbortedPrepare {
// locked in preparing is preparing, not aborted (default in the preparing dir)
p.isAbortedPrepare = false
p.isPreparing = true
} else if p.isGarbage {
// locked in non-exited garbage is deleting
p.isDeleting = true
}
err = nil
} else {
l.Unlock()
}
}
p.FileLock = l
if p.isRunning() || p.isExit() {
cfd, err := p.Fd()
if err != nil {
return nil, errwrap.Wrap(fmt.Errorf("error acquiring pod %v dir fd", uuid), err)
}
p.Nets, err = netinfo.LoadAt(cfd)
// ENOENT is ok -- assume running with --net=host
if err != nil && !os.IsNotExist(err) {
return nil, errwrap.Wrap(fmt.Errorf("error opening pod %v netinfo", uuid), err)
}
}
return p, nil
}
// PodFromUUIDString attempts to resolve the supplied UUID and return a pod.
// The pod must be closed using pod.Close()
func PodFromUUIDString(dataDir, uuid string) (*Pod, error) {
podUUID, err := resolveUUID(dataDir, uuid)
if err != nil {
return nil, errwrap.Wrap(errors.New("unable to resolve UUID"), err)
}
p, err := getPod(dataDir, podUUID)
if err != nil {
return nil, errwrap.Wrap(errors.New("unable to get pod"), err)
}
return p, nil
}
// embryoPath returns the path to the pod where it would be in the embryoDir in its embryonic state.
func (p *Pod) embryoPath() string {
return filepath.Join(embryoDir(p.dataDir), p.UUID.String())
}
// preparePath returns the path to the pod where it would be in the prepareDir in its preparing state.
func (p *Pod) preparePath() string {
return filepath.Join(prepareDir(p.dataDir), p.UUID.String())
}
// preparedPath returns the path to the pod where it would be in the preparedDir.
func (p *Pod) preparedPath() string {
return filepath.Join(preparedDir(p.dataDir), p.UUID.String())
}
// runPath returns the path to the pod where it would be in the runDir.
func (p *Pod) runPath() string {
return filepath.Join(runDir(p.dataDir), p.UUID.String())
}
// exitedGarbagePath returns the path to the pod where it would be in the exitedGarbageDir.
func (p *Pod) exitedGarbagePath() string {
return filepath.Join(exitedGarbageDir(p.dataDir), p.UUID.String())
}
// garbagePath returns the path to the pod where it would be in the garbageDir.
func (p *Pod) garbagePath() string {
return filepath.Join(garbageDir(p.dataDir), p.UUID.String())
}
// ToPrepare transitions a pod from embryo -> preparing, leaves the pod locked in the prepare directory.
// only the creator of the pod (via NewPod()) may do this, nobody to race with.
// This method refreshes the pod state.
func (p *Pod) ToPreparing() error {
if !p.createdByMe {
return fmt.Errorf("bug: only pods created by me may transition to preparing")
}
if !p.isEmbryo {
return fmt.Errorf("bug: only embryonic pods can transition to preparing")
}
if err := p.ExclusiveLock(); err != nil {
return err
}
if err := os.Rename(p.embryoPath(), p.preparePath()); err != nil {
return err
}
df, err := os.Open(prepareDir(p.dataDir))
if err != nil {
return err
}
defer df.Close()
if err := df.Sync(); err != nil {
return err
}
p.isEmbryo = false
p.isPreparing = true
return nil
}
// ToPrepared transitions a pod from preparing -> prepared, leaves the pod unlocked in the prepared directory.
// only the creator of the pod (via NewPod()) may do this, nobody to race with.
// This method refreshes the pod state.
func (p *Pod) ToPrepared() error {
if !p.createdByMe {
return fmt.Errorf("bug: only pods created by me may transition to prepared")
}
if !p.isPreparing {
return fmt.Errorf("bug: only preparing pods may transition to prepared")
}
if err := os.Rename(p.Path(), p.preparedPath()); err != nil {
return err
}
if err := p.Unlock(); err != nil {
return err
}
df, err := os.Open(preparedDir(p.dataDir))
if err != nil {
return err
}
defer df.Close()
if err := df.Sync(); err != nil {
return err
}
p.isPreparing = false
p.isPrepared = true
return nil
}
// ToRun transitions a pod from prepared -> run, leaves the pod locked in the run directory.
// the creator of the pod (via NewPod()) may also jump directly from preparing -> run
// This method refreshes the pod state.
func (p *Pod) ToRun() error {
if !p.createdByMe && !p.isPrepared {
return fmt.Errorf("bug: only prepared pods may transition to run")
}
if p.createdByMe && !p.isPrepared && !p.isPreparing {
return fmt.Errorf("bug: only prepared or preparing pods may transition to run")
}
if err := p.ExclusiveLock(); err != nil {
return err
}
label.Relabel(p.Path(), p.MountLabel, false)
if err := os.Rename(p.Path(), p.runPath()); err != nil {
// TODO(vc): we could race here with a concurrent ToRun(), let caller deal with the error.
return err
}
df, err := os.Open(runDir(p.dataDir))
if err != nil {
return err
}
defer df.Close()
if err := df.Sync(); err != nil {
return err
}
p.isPreparing = false
p.isPrepared = false
return nil
}
// ToExitedGarbage transitions a pod from run -> exitedGarbage
// This method refreshes the pod state.
func (p *Pod) ToExitedGarbage() error {
if !p.isExited || p.isExitedGarbage {
return fmt.Errorf("bug: only exited non-garbage pods may transition to exited-garbage")
}
if err := os.Rename(p.runPath(), p.exitedGarbagePath()); err != nil {
// TODO(vc): another case where we could race with a concurrent ToExitedGarbage(), let caller deal with the error.
return err
}
df, err := os.Open(exitedGarbageDir(p.dataDir))
if err != nil {
return err
}
defer df.Close()
if err := df.Sync(); err != nil {
return err
}
p.isExitedGarbage = true
return nil
}
// ToGarbage transitions a pod from abortedPrepared -> garbage or prepared -> garbage
// This method refreshes the pod state.
func (p *Pod) ToGarbage() error {
if !p.isAbortedPrepare && !p.isPrepared {
return fmt.Errorf("bug: only failed prepare or prepared pods may transition to garbage")
}
if err := os.Rename(p.Path(), p.garbagePath()); err != nil {
return err
}
df, err := os.Open(garbageDir(p.dataDir))
if err != nil {
return err
}
defer df.Close()
if err := df.Sync(); err != nil {
return err
}
p.isAbortedPrepare = false
p.isPrepared = false
p.isGarbage = true
return nil
}
// listPods returns a list of pod uuids in string form.
func listPods(dataDir string, include IncludeMask) ([]string, error) {
// uniqued due to the possibility of a pod being renamed from across directories during the list operation
ups := make(map[string]struct{})
dirs := []struct {
kind IncludeMask
path string
}{
{ // the order here is significant: embryo -> preparing -> prepared -> running -> exitedGarbage
kind: IncludeEmbryoDir,
path: embryoDir(dataDir),
}, {
kind: IncludePrepareDir,
path: prepareDir(dataDir),
}, {
kind: IncludePreparedDir,
path: preparedDir(dataDir),
}, {
kind: IncludeRunDir,
path: runDir(dataDir),
}, {
kind: IncludeExitedGarbageDir,
path: exitedGarbageDir(dataDir),
}, {
kind: IncludeGarbageDir,
path: garbageDir(dataDir),
},
}
for _, d := range dirs {
if include&d.kind != 0 {
ps, err := listPodsFromDir(d.path)
if err != nil {
return nil, err
}
for _, p := range ps {
ups[p] = struct{}{}
}
}
}
ps := make([]string, 0, len(ups))
for p := range ups {
ps = append(ps, p)
}
return ps, nil
}
// listPodsFromDir returns a list of pod uuids in string form from a specific directory.
func listPodsFromDir(cdir string) ([]string, error) {
var ps []string
ls, err := ioutil.ReadDir(cdir)
if err != nil {
if os.IsNotExist(err) {
return ps, nil
}
return nil, errwrap.Wrap(errors.New("cannot read pods directory"), err)
}
for _, p := range ls {
if !p.IsDir() {
fmt.Fprintf(os.Stderr, "unrecognized entry: %q, ignoring", p.Name())
continue
}
ps = append(ps, p.Name())
}
return ps, nil
}
// refreshState() updates the cached members of the pod to reflect current reality.
// Assumes p.FileLock is currently unlocked, and always returns with it unlocked.
func (p *Pod) refreshState() error {
p.isEmbryo = false
p.isPreparing = false
p.isAbortedPrepare = false
p.isPrepared = false
p.isExited = false
p.isExitedGarbage = false
p.isExitedDeleting = false
p.isGarbage = false
p.isDeleting = false
p.isGone = false
// dirStates is a list of directories -> state that directory existing
// implies.
// Its order matches the order states occur.
dirStates := []struct {
dir string
impliedStates []*bool
}{
{dir: p.embryoPath(), impliedStates: []*bool{&p.isEmbryo}},
// For prepare, assume it's aborted prepare until it gets updated below
{dir: p.preparePath(), impliedStates: []*bool{&p.isAbortedPrepare}},
{dir: p.preparedPath(), impliedStates: []*bool{&p.isPrepared}},
// For run, assume exited until the lock is tested
{dir: p.runPath(), impliedStates: []*bool{&p.isExited}},
// Exited garbage implies exited
{dir: p.exitedGarbagePath(), impliedStates: []*bool{&p.isExitedGarbage, &p.isExited}},
{dir: p.garbagePath(), impliedStates: []*bool{&p.isGarbage}},
}
anyMatched := false
for _, dirState := range dirStates {
_, err := os.Stat(dirState.dir)
if err == nil {
for _, s := range dirState.impliedStates {
*s = true
}
anyMatched = true
break
}
if os.IsNotExist(err) {
// just try the next one if it didn't exist
continue
}
// Unknown error statting directory
return errwrap.Wrap(fmt.Errorf("error refreshing state of pod %q", p.UUID.String()), err)
}
if !anyMatched {
// default to isGone if nothing else matched
p.isGone = true
return nil
}
if p.isPrepared || p.isEmbryo {
// no need to try a shared lock for these; our state is already accurate
return nil
}
// preparing, run, and exitedGarbage dirs use exclusive locks to indicate preparing/aborted, running/exited, and deleting/marked
err := p.TrySharedLock()
if err == nil {
// if the lock isn't held, then the impliedState above is accurate so we can just return
p.Unlock()
return nil
}
if err != lock.ErrLocked {
p.Close()
return errwrap.Wrap(errors.New("unexpected lock error"), err)
}
if p.isExitedGarbage {
// locked exitedGarbage is also being deleted
p.isExitedDeleting = true
} else if p.isExited {
// locked exited and !exitedGarbage is not exited (default in the run dir)
p.isExited = false
} else if p.isAbortedPrepare {
// locked in preparing is preparing, not aborted (default in the preparing dir)
p.isAbortedPrepare = false
p.isPreparing = true
} else if p.isGarbage {
// locked in non-exited garbage is deleting
p.isDeleting = true
}
return nil
}
// readFile reads an entire file from a pod's directory.
func (p *Pod) readFile(path string) ([]byte, error) {
f, err := p.openFile(path, syscall.O_RDONLY)
if err != nil {
return nil, err
}
defer f.Close()
return ioutil.ReadAll(f)
}
// readIntFromFile reads an int from a file in a pod's directory.
func (p *Pod) readIntFromFile(path string) (i int, err error) {
b, err := p.readFile(path)
if err != nil {
return
}
_, err = fmt.Sscanf(string(b), "%d", &i)
return
}
// openFile opens a file from a pod's directory returning a file descriptor.
func (p *Pod) openFile(path string, flags int) (*os.File, error) {
cdirfd, err := p.Fd()
if err != nil {
return nil, err
}
fd, err := syscall.Openat(cdirfd, path, flags, 0)
if err != nil {
return nil, err
}
return os.NewFile(uintptr(fd), path), nil
}
func (p *Pod) getModTime(path string) (time.Time, error) {
f, err := p.openFile(path, syscall.O_RDONLY)
if err != nil {
return time.Time{}, err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return time.Time{}, err
}
return fi.ModTime(), nil
}
type ErrChildNotReady struct {
}
func (e ErrChildNotReady) Error() string {
return fmt.Sprintf("Child not ready")
}
// Returns the pid of the child, or ErrChildNotReady if not ready
func getChildPID(ppid int) (int, error) {
var pid int
// If possible, get the child in O(1). Fallback on O(n) when the kernel does not have
// either CONFIG_PROC_CHILDREN or CONFIG_CHECKPOINT_RESTORE
_, err := os.Stat("/proc/1/task/1/children")
if err == nil {
b, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/task/%d/children", ppid, ppid))
if err == nil {
children := strings.SplitN(string(b), " ", 2)
if len(children) == 2 && children[1] != "" {
return -1, fmt.Errorf("too many children of pid %d", ppid)
}
if _, err := fmt.Sscanf(children[0], "%d ", &pid); err == nil {
return pid, nil
}
}
return -1, ErrChildNotReady{}
}
// Fallback on the slower method
fdir, err := os.Open(`/proc`)
if err != nil {
return -1, err
}
defer fdir.Close()
for {
fi, err := fdir.Readdir(1)
if err == io.EOF {
break
}
if err != nil {
return -1, err
}
if len(fi) == 0 {
// See https://github.com/rkt/rkt/issues/3109#issuecomment-242209246
continue
}
var pid64 int64
if pid64, err = strconv.ParseInt(fi[0].Name(), 10, 0); err != nil {
continue
}
filename := fmt.Sprintf("/proc/%d/stat", pid64)
statBytes, err := ioutil.ReadFile(filename)
if err != nil {
// The process just died? It's not the one we want then.
continue
}
statFields := strings.SplitN(string(statBytes), " ", 5)
if len(statFields) != 5 {
return -1, fmt.Errorf("incomplete file %q", filename)
}
if statFields[3] == fmt.Sprintf("%d", ppid) {
return int(pid64), nil
}
}
return -1, ErrChildNotReady{}
}
// GetStage1TreeStoreID returns the treeStoreID of the stage1 image used in
// this pod
// TODO(yifan): Maybe make this unexported.
func (p *Pod) GetStage1TreeStoreID() (string, error) {
s1IDb, err := p.readFile(common.Stage1TreeStoreIDFilename)
if err != nil {
return "", err
}
return string(s1IDb), nil
}
// GetAppTreeStoreID returns the treeStoreID of the provided app.
// TODO(yifan): Maybe make this unexported.
func (p *Pod) GetAppTreeStoreID(app types.ACName) (string, error) {
path, err := filepath.Rel("/", common.AppTreeStoreIDPath("", app))
if err != nil {
return "", err
}
treeStoreID, err := p.readFile(path)
if err != nil {
// When not using overlayfs, apps don't have a treeStoreID file. In
// other cases we've got a problem.
if !(os.IsNotExist(err) && !p.UsesOverlay()) {
return "", errwrap.Wrap(fmt.Errorf("no treeStoreID found for app %s", app), err)
}
}
return string(treeStoreID), nil
}
// GetAppsTreeStoreIDs returns the treeStoreIDs of the apps images used in
// this pod.
// TODO(yifan): Maybe make this unexported.
func (p *Pod) GetAppsTreeStoreIDs() ([]string, error) {
var treeStoreIDs []string
apps, err := p.getApps()
if err != nil {
return nil, err
}
for _, a := range apps {
id, err := p.GetAppTreeStoreID(a.Name)
if err != nil {
return nil, err
}
if id != "" {
treeStoreIDs = append(treeStoreIDs, id)
}
}
return treeStoreIDs, nil
}
// getAppsHashes returns a list of the app hashes in the pod
func (p *Pod) getAppsHashes() ([]types.Hash, error) {
apps, err := p.getApps()
if err != nil {
return nil, err
}
var hashes []types.Hash
for _, a := range apps {
hashes = append(hashes, a.Image.ID)
}
return hashes, nil
}
// getApps returns a list of apps in the pod
func (p *Pod) getApps() (schema.AppList, error) {
_, pm, err := p.PodManifest()
if err != nil {
return nil, err
}
return pm.Apps, nil
}
// getDirNames returns the list of names from a pod's directory
func (p *Pod) getDirNames(path string) ([]string, error) {
dir, err := p.openFile(path, syscall.O_RDONLY|syscall.O_DIRECTORY)
if err != nil {
return nil, errwrap.Wrap(errors.New("unable to open directory"), err)
}
defer dir.Close()
ld, err := dir.Readdirnames(0)
if err != nil {
return nil, errwrap.Wrap(errors.New("unable to read directory"), err)
}
return ld, nil
}
// UsesOverlay returns whether the pod Uses overlayfs.
// TODO(yifan): Maybe make this function unexported.
func (p *Pod) UsesOverlay() bool {
f, err := p.openFile(common.OverlayPreparedFilename, syscall.O_RDONLY)
defer f.Close()
return err == nil
}
// Sync syncs the pod data. By now it calls a syncfs on the filesystem
// containing the pod's directory.
func (p *Pod) Sync() error {
cfd, err := p.Fd()
if err != nil {
return errwrap.Wrap(fmt.Errorf("error acquiring pod %v dir fd", p.UUID.String()), err)
}
if err := sys.Syncfs(cfd); err != nil {
return errwrap.Wrap(fmt.Errorf("failed to sync pod %v data", p.UUID.String()), err)
}
return nil
}
// WalkPods iterates over the included directories calling function f for every pod found.
// The pod will be closed after the function 'f' is executed.
func WalkPods(dataDir string, include IncludeMask, f func(*Pod)) error {
if err := initPods(dataDir); err != nil {
return err
}
ls, err := listPods(dataDir, include)
if err != nil {
return errwrap.Wrap(errors.New("failed to get pods"), err)
}
sort.Strings(ls)
for _, uuid := range ls {
u, err := types.NewUUID(uuid)
if err != nil {
fmt.Fprintf(os.Stderr, "skipping %q: %v", uuid, err)
continue
}
p, err := getPod(dataDir, u)
if err != nil {
fmt.Fprintf(os.Stderr, "skipping %q: %v", uuid, err)
continue
}
// omit pods found in unrequested states
// this is to cover a race between listPods finding the uuids and pod states changing
// it's preferable to keep these operations lock-free, for example a `rkt gc` shouldn't block `rkt run`.
if p.isEmbryo && include&IncludeEmbryoDir == 0 ||
p.isExitedGarbage && include&IncludeExitedGarbageDir == 0 ||
p.isGarbage && include&IncludeGarbageDir == 0 ||
p.isPrepared && include&IncludePreparedDir == 0 ||
((p.isPreparing || p.isAbortedPrepare) && include&IncludePrepareDir == 0) ||
p.isRunning() && include&IncludeRunDir == 0 {
p.Close()
continue
}
f(p)
p.Close()
}
return nil
}
// PodManifest reads the pod manifest, returns the raw bytes and the unmarshalled object.
func (p *Pod) PodManifest() ([]byte, *schema.PodManifest, error) {
pmb, err := p.readFile("pod")
if err != nil {
return nil, nil, errwrap.Wrap(errors.New("error reading pod manifest"), err)
}
pm := &schema.PodManifest{}
if err = pm.UnmarshalJSON(pmb); err != nil {
return nil, nil, errwrap.Wrap(errors.New("invalid pod manifest"), err)
}
return pmb, pm, nil
}
// AppImageManifest returns an ImageManifest for the app.
func (p *Pod) AppImageManifest(appName string) (*schema.ImageManifest, error) {
appACName, err := types.NewACName(appName)
if err != nil {
return nil, err
}
imb, err := ioutil.ReadFile(common.AppImageManifestPath(p.Path(), *appACName))
if err != nil {
return nil, err
}
aim := &schema.ImageManifest{}
if err := aim.UnmarshalJSON(imb); err != nil {
return nil, errwrap.Wrap(fmt.Errorf("invalid image manifest for app %q", appName), err)
}
return aim, nil
}
// CreationTime returns the time when the pod was created.
// This happens at prepare time.
func (p *Pod) CreationTime() (time.Time, error) {
if !(p.isPrepared || p.isRunning() || p.IsAfterRun()) {
return time.Time{}, nil
}
t, err := p.getModTime("pod-created")
if err == nil {
return t, nil
}
if !os.IsNotExist(err) {
return t, err
}
// backwards compatibility with rkt before v1.20
return p.getModTime("pod")
}
// StartTime returns the time when the pod was started.
func (p *Pod) StartTime() (time.Time, error) {
var (
t time.Time
retErr error
)
if !p.isRunning() && !p.IsAfterRun() {
// hasn't started
return t, nil
}
// check pid and ppid since stage1s can choose one xor the other
for _, ctimeFile := range []string{"pid", "ppid"} {
t, err := p.getModTime(ctimeFile)
if err == nil {
return t, nil
}
// if there's an error starting the pod, it can go to "exited" without
// creating a ppid/pid file, so ignore not-exist errors.
if !os.IsNotExist(err) {
retErr = err
}