HostProperties.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # ex:ts=8 sw=4:
  2. # $OpenBSD: HostProperties.pm,v 1.16 2016/04/30 09:15:58 espie Exp $
  3. #
  4. # Copyright (c) 2010-2013 Marc Espie <espie@openbsd.org>
  5. #
  6. # Permission to use, copy, modify, and distribute this software for any
  7. # purpose with or without fee is hereby granted, provided that the above
  8. # copyright notice and this permission notice appear in all copies.
  9. #
  10. # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. use strict;
  18. use warnings;
  19. package DPB::HostProperties;
  20. use DPB::User;
  21. my $has_sf = 0;
  22. my $has_mem = 0;
  23. my $sf;
  24. sub new
  25. {
  26. my ($class, $default) = @_;
  27. $default //= {};
  28. bless {%$default}, $class;
  29. }
  30. sub add_overrides
  31. {
  32. my ($prop, $override) = @_;
  33. while (my ($k, $v) = each %$override) {
  34. $prop->{$k} = $v;
  35. }
  36. $sf //= $prop->{sf};
  37. if (defined $prop->{sf} && $prop->{sf} != $sf) {
  38. $has_sf = 1;
  39. }
  40. }
  41. sub has_sf
  42. {
  43. return $has_sf;
  44. }
  45. sub has_mem
  46. {
  47. return $has_mem;
  48. }
  49. sub finalize
  50. {
  51. my $prop = shift;
  52. $prop->{sf} //= 1;
  53. $prop->{umask} //= sprintf("0%o", umask);
  54. if (defined $prop->{stuck}) {
  55. $prop->{stuck_timeout} = $prop->{stuck} * $prop->{sf};
  56. }
  57. if (defined $prop->{mem}) {
  58. $prop->{memory} = $prop->{mem};
  59. }
  60. if (defined $prop->{chroot}) {
  61. if ($prop->{chroot} eq '/' || $prop->{chroot} eq '') {
  62. delete $prop->{chroot};
  63. } else {
  64. }
  65. }
  66. if (defined $prop->{build_user}) {
  67. $prop->{build_user} = DPB::User->new($prop->{build_user})
  68. unless ref $prop->{build_user};
  69. } else {
  70. if ($prop->{base_user}{uid} != 0) {
  71. $prop->{build_user} = $prop->{base_user};
  72. } else {
  73. $prop->{build_user} = DPB::User->new('_pbuild');
  74. }
  75. }
  76. if (defined $prop->{dirmode}) {
  77. $prop->{build_user}{dirmode} = oct($prop->{dirmode});
  78. }
  79. if (defined $prop->{droppriv}) {
  80. $prop->{build_user}{droppriv} = $prop->{droppriv};
  81. }
  82. if (defined $prop->{memory}) {
  83. my $m = $prop->{memory};
  84. if ($m =~ s/K$//) {
  85. } elsif ($m =~ s/M$//) {
  86. $m *= 1024;
  87. } elsif ($m =~ s/G$//) {
  88. $m *= 1024 * 1024;
  89. }
  90. $prop->{memory} = $m;
  91. if ($prop->{memory} > 0) {
  92. $has_mem = 1;
  93. }
  94. }
  95. $prop->{small} //= 120;
  96. $prop->{small_timeout} = $prop->{small} * $prop->{sf};
  97. return $prop;
  98. }
  99. sub finalize_with_overrides
  100. {
  101. my ($self, $overrides) = @_;
  102. $self->add_overrides($overrides);
  103. $self->finalize;
  104. }
  105. sub taint
  106. {
  107. my ($self, $v) = @_;
  108. my $t2 = $v->{info}->has_property('tag');
  109. if (!defined $t2) {
  110. return;
  111. }
  112. my $t1 = $self->{tainted};
  113. if (!defined $t1) {
  114. $self->{tainted} = $t2;
  115. $self->{tainted_source} = $v->fullpkgpath;
  116. return;
  117. }
  118. if ($t1 ne $t2) {
  119. DPB::Util->die("Retainting badly:$t2 / $t1 ".
  120. $v->fullpkgpath." / ".$self->{tainted_source});
  121. }
  122. }
  123. sub untaint
  124. {
  125. my $self = shift;
  126. delete $self->{tainted};
  127. delete $self->{tainted_source};
  128. }
  129. sub taint_incompatible
  130. {
  131. my ($self, $v) = @_;
  132. my $t1 = $self->{tainted};
  133. if (!defined $t1) {
  134. return undef;
  135. }
  136. my $t2 = $v->{info}->has_property('tag');
  137. if (!defined $t2) {
  138. return undef;
  139. }
  140. if ($t1 eq $t2) {
  141. return undef;
  142. }
  143. return "$t1 vs $t2";
  144. }
  145. 1;