| 1 | package Ubic; | ||
| 2 | $Ubic::VERSION = '1.59'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # ABSTRACT: polymorphic service manager | ||
| 7 | |||
| 8 | |||
| 9 | use POSIX qw(); | ||
| 10 | use Carp; | ||
| 11 | use IO::Handle; | ||
| 12 | use Storable qw(freeze thaw); | ||
| 13 | use Try::Tiny; | ||
| 14 | use Scalar::Util qw(blessed); | ||
| 15 | use Params::Validate qw(:all); | ||
| 16 | |||
| 17 | use Ubic::Result qw(result); | ||
| 18 | use Ubic::Multiservice::Dir; | ||
| 19 | use Ubic::AccessGuard; | ||
| 20 | use Ubic::Credentials; | ||
| 21 | use Ubic::Persistent; | ||
| 22 | use Ubic::AtomicFile; | ||
| 23 | use Ubic::SingletonLock; | ||
| 24 | use Ubic::Settings; | ||
| 25 | |||
| 26 | our $SINGLETON; | ||
| 27 | |||
| 28 | my $service_name_re = qr{^[\w-]+(?:\.[\w-]+)*$}; | ||
| 29 | my $validate_service = { type => SCALAR, regex => $service_name_re }; | ||
| 30 | |||
| 31 | # singleton constructor | ||
| 32 | sub _obj { | ||
| 33 | my ($param) = validate_pos(@_, 1); | ||
| 34 | if (blessed($param)) { | ||
| 35 | return $param; | ||
| 36 | } | ||
| 37 | if ($param eq 'Ubic') { | ||
| 38 | # method called as a class method => singleton | ||
| 39 | $SINGLETON ||= Ubic->new({}); | ||
| 40 | return $SINGLETON; | ||
| 41 | } | ||
| 42 | die "Unknown argument '$param'"; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub new { | ||
| 46 | my $class = shift; | ||
| 47 | my $options = validate(@_, { | ||
| 48 | service_dir => { type => SCALAR, optional => 1 }, | ||
| 49 | data_dir => { type => SCALAR, optional => 1 }, | ||
| 50 | }); | ||
| 51 | |||
| 52 | if (caller ne 'Ubic') { | ||
| 53 | warn "Using Ubic->new constructor is discouraged. Just call methods as class methods."; | ||
| 54 | } | ||
| 55 | |||
| 56 | for my $key (qw/ service_dir data_dir /) { | ||
| 57 | Ubic::Settings->$key($options->{ $key }) if defined $options->{$key}; | ||
| 58 | } | ||
| 59 | |||
| 60 | Ubic::Settings->check_settings; | ||
| 61 | |||
| 62 | my $self = {}; | ||
| 63 | $self->{data_dir} = Ubic::Settings->data_dir; | ||
| 64 | $self->{service_dir} = Ubic::Settings->service_dir; | ||
| 65 | |||
| 66 | $self->{status_dir} = "$self->{data_dir}/status"; | ||
| 67 | $self->{lock_dir} = "$self->{data_dir}/lock"; | ||
| 68 | $self->{tmp_dir} = "$self->{data_dir}/tmp"; | ||
| 69 | |||
| 70 | $self->{service_cache} = {}; | ||
| 71 | return bless $self => $class; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub start($$) { | ||
| 75 | my $self = _obj(shift); | ||
| 76 | my ($name) = validate_pos(@_, $validate_service); | ||
| 77 | my $lock = $self->lock($name); | ||
| 78 | |||
| 79 | $self->enable($name); | ||
| 80 | my $result = $self->do_cmd($name, 'start'); | ||
| 81 | $self->set_cached_status($name, $result); | ||
| 82 | return $result; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub stop($$) { | ||
| 86 | my $self = _obj(shift); | ||
| 87 | my ($name) = validate_pos(@_, $validate_service); | ||
| 88 | my $lock = $self->lock($name); | ||
| 89 | |||
| 90 | $self->disable($name); | ||
| 91 | |||
| 92 | # FIXME - 'stop' command can fail, in this case daemon will keep running. | ||
| 93 | # This is bad. | ||
| 94 | # We probably need to implement the same logic as when starting: | ||
| 95 | # retry stop attempts until actual status matches desired status. | ||
| 96 | my $result = $self->do_cmd($name, 'stop'); | ||
| 97 | return $result; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub restart($$) { | ||
| 101 | my $self = _obj(shift); | ||
| 102 | my ($name) = validate_pos(@_, $validate_service); | ||
| 103 | my $lock = $self->lock($name); | ||
| 104 | |||
| 105 | $self->enable($name); | ||
| 106 | my $result = $self->do_cmd($name, 'stop'); | ||
| 107 | $result = $self->do_cmd($name, 'start'); | ||
| 108 | |||
| 109 | $self->set_cached_status($name, $result); | ||
| 110 | return result('restarted'); # FIXME - should return original status | ||
| 111 | } | ||
| 112 | |||
| 113 | sub try_restart($$) { | ||
| 114 | my $self = _obj(shift); | ||
| 115 | my ($name) = validate_pos(@_, $validate_service); | ||
| 116 | my $lock = $self->lock($name); | ||
| 117 | |||
| 118 | unless ($self->is_enabled($name)) { | ||
| 119 | return result('down'); | ||
| 120 | } | ||
| 121 | $self->do_cmd($name, 'stop'); | ||
| 122 | $self->do_cmd($name, 'start'); | ||
| 123 | return result('restarted'); | ||
| 124 | } | ||
| 125 | |||
| 126 | sub reload($$) { | ||
| 127 | my $self = _obj(shift); | ||
| 128 | my ($name) = validate_pos(@_, $validate_service); | ||
| 129 | my $lock = $self->lock($name); | ||
| 130 | |||
| 131 | unless ($self->is_enabled($name)) { | ||
| 132 | return result('down'); | ||
| 133 | } | ||
| 134 | |||
| 135 | # if reload isn't implemented, do nothing | ||
| 136 | # TODO - would it be better to execute reload as force-reload always? but it would be incompatible with LSB specification... | ||
| 137 | my $result = $self->do_cmd($name, 'reload'); | ||
| 138 | unless ($result->action eq 'reloaded') { | ||
| 139 | die $result; | ||
| 140 | } | ||
| 141 | return $result; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub force_reload($$) { | ||
| 145 | my $self = _obj(shift); | ||
| 146 | my ($name) = validate_pos(@_, $validate_service); | ||
| 147 | my $lock = $self->lock($name); | ||
| 148 | |||
| 149 | unless ($self->is_enabled($name)) { | ||
| 150 | return result('down'); | ||
| 151 | } | ||
| 152 | |||
| 153 | my $result = $self->do_cmd($name, 'reload'); | ||
| 154 | return $result if $result->action eq 'reloaded'; | ||
| 155 | |||
| 156 | $self->try_restart($name); | ||
| 157 | } | ||
| 158 | |||
| 159 | sub status($$) { | ||
| 160 | my $self = _obj(shift); | ||
| 161 | my ($name) = validate_pos(@_, $validate_service); | ||
| 162 | my $lock = $self->lock($name); | ||
| 163 | |||
| 164 | return $self->do_cmd($name, 'status'); | ||
| 165 | } | ||
| 166 | |||
| 167 | sub enable($$) { | ||
| 168 | my $self = _obj(shift); | ||
| 169 | my ($name) = validate_pos(@_, $validate_service); | ||
| 170 | my $lock = $self->lock($name); | ||
| 171 | my $guard = $self->access_guard($name); | ||
| 172 | |||
| 173 | my $status_obj = $self->status_obj($name); | ||
| 174 | $status_obj->{status} = 'unknown'; | ||
| 175 | $status_obj->{enabled} = 1; | ||
| 176 | $status_obj->commit; | ||
| 177 | return result('unknown'); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub is_enabled($$) { | ||
| 181 | my $self = _obj(shift); | ||
| 182 | my ($name) = validate_pos(@_, $validate_service); | ||
| 183 | |||
| 184 | die "Service '$name' not found" unless $self->root_service->has_service($name); | ||
| 185 | unless (-e $self->status_file($name)) { | ||
| 186 | return $self->service($name)->auto_start(); | ||
| 187 | } | ||
| 188 | |||
| 189 | my $status_obj = $self->status_obj_ro($name); | ||
| 190 | if ($status_obj->{enabled} or not exists $status_obj->{enabled}) { | ||
| 191 | return 1; | ||
| 192 | } | ||
| 193 | return; | ||
| 194 | } | ||
| 195 | |||
| 196 | sub disable($$) { | ||
| 197 | my $self = _obj(shift); | ||
| 198 | my ($name) = validate_pos(@_, $validate_service); | ||
| 199 | my $lock = $self->lock($name); | ||
| 200 | my $guard = $self->access_guard($name); | ||
| 201 | |||
| 202 | my $status_obj = $self->status_obj($name); | ||
| 203 | delete $status_obj->{status}; | ||
| 204 | $status_obj->{enabled} = 0; | ||
| 205 | $status_obj->commit; | ||
| 206 | } | ||
| 207 | |||
| 208 | |||
| 209 | sub cached_status($$) { | ||
| 210 | my ($self) = _obj(shift); | ||
| 211 | my ($name) = validate_pos(@_, $validate_service); | ||
| 212 | |||
| 213 | my $type; | ||
| 214 | if (not $self->is_enabled($name)) { | ||
| 215 | $type = 'disabled'; | ||
| 216 | } | ||
| 217 | elsif (-e $self->status_file($name)) { | ||
| 218 | $type = $self->status_obj_ro($name)->{status}; | ||
| 219 | } else { | ||
| 220 | $type = 'autostarting'; | ||
| 221 | } | ||
| 222 | return Ubic::Result::Class->new({ type => $type, cached => 1 }); | ||
| 223 | } | ||
| 224 | |||
| 225 | sub do_custom_command($$) { | ||
| 226 | my ($self) = _obj(shift); | ||
| 227 | my ($name, $command) = validate_pos(@_, $validate_service, 1); | ||
| 228 | |||
| 229 | # TODO - do all custom commands require locks? | ||
| 230 | # they can be distinguished in future by some custom_commands_ext method which will provide hash { command => properties }, i think... | ||
| 231 | my $lock = $self->lock($name); | ||
| 232 | |||
| 233 | # TODO - check custom_command presence by custom_commands() method first? | ||
| 234 | $self->do_sub(sub { | ||
| 235 | $self->service($name)->do_custom_command($command); # can custom commands require custom arguments? | ||
| 236 | }); | ||
| 237 | } | ||
| 238 | |||
| 239 | sub service($$) { | ||
| 240 | my $self = _obj(shift); | ||
| 241 | my ($name) = validate_pos(@_, $validate_service); | ||
| 242 | # this guarantees that : will be unambiguous separator in status filename (what??) | ||
| 243 | unless ($self->{service_cache}{$name}) { | ||
| 244 | # Service construction is a memory-leaking operation (because of package name randomization in Ubic::Multiservice::Dir), | ||
| 245 | # so we need to cache each service which we create. | ||
| 246 | $self->{service_cache}{$name} = $self->root_service->service($name); | ||
| 247 | } | ||
| 248 | return $self->{service_cache}{$name}; | ||
| 249 | } | ||
| 250 | |||
| 251 | sub has_service($$) { | ||
| 252 | my $self = _obj(shift); | ||
| 253 | my ($name) = validate_pos(@_, $validate_service); | ||
| 254 | # TODO - it would be safer to do this check without actual service construction | ||
| 255 | # but it would require cron-based script which maintains list of all services | ||
| 256 | return $self->root_service->has_service($name); | ||
| 257 | } | ||
| 258 | |||
| 259 | sub services($) { | ||
| 260 | my $self = _obj(shift); | ||
| 261 | return $self->root_service->services(); | ||
| 262 | } | ||
| 263 | |||
| 264 | sub service_names($) { | ||
| 265 | my $self = _obj(shift); | ||
| 266 | return $self->root_service->service_names(); | ||
| 267 | } | ||
| 268 | |||
| 269 | sub root_service($) { | ||
| 270 | my $self = _obj(shift); | ||
| 271 | unless (defined $self->{root}) { | ||
| 272 | $self->{root} = Ubic::Multiservice::Dir->new($self->{service_dir}, { protected => 1 }); | ||
| 273 | } | ||
| 274 | return $self->{root}; | ||
| 275 | } | ||
| 276 | |||
| 277 | sub compl_services($$) { | ||
| 278 | my $self = _obj(shift); | ||
| 279 | my $line = shift; | ||
| 280 | my @parts = split /\./, $line; | ||
| 281 | if ($line =~ /\.$/) { | ||
| 282 | push @parts, ''; | ||
| 283 | } | ||
| 284 | if (@parts == 0) { | ||
| 285 | return $self->service_names; | ||
| 286 | } | ||
| 287 | my $node = $self->root_service; | ||
| 288 | my $is_subservice = (@parts > 1); | ||
| 289 | while (@parts > 1) { | ||
| 290 | unless ($node->isa('Ubic::Multiservice')) { | ||
| 291 | return; | ||
| 292 | } | ||
| 293 | my $part = shift @parts; | ||
| 294 | return unless $node->has_service($part); # no such service | ||
| 295 | $node = $node->service($part); | ||
| 296 | } | ||
| 297 | |||
| 298 | my @variants = $node->service_names; | ||
| 299 | return | ||
| 300 | map { | ||
| 301 | ( $is_subservice ? $node->full_name.".".$_ : $_ ) | ||
| 302 | } | ||
| 303 | grep { | ||
| 304 | $_ =~ m{^\Q$parts[0]\E} | ||
| 305 | } | ||
| 306 | @variants; | ||
| 307 | } | ||
| 308 | |||
| 309 | sub set_cached_status($$$) { | ||
| 310 | my $self = _obj(shift); | ||
| 311 | my ($name, $status) = validate_pos(@_, $validate_service, 1); | ||
| 312 | my $guard = $self->access_guard($name); | ||
| 313 | |||
| 314 | if (blessed $status) { | ||
| 315 | croak "Wrong status param '$status'" unless $status->isa('Ubic::Result::Class'); | ||
| 316 | $status = $status->status; | ||
| 317 | } | ||
| 318 | my $lock = $self->lock($name); | ||
| 319 | |||
| 320 | if (-e $self->status_file($name) and $self->status_obj_ro($name)->{status} eq $status) { | ||
| 321 | # optimization - don't update status if nothing changed | ||
| 322 | return; | ||
| 323 | } | ||
| 324 | |||
| 325 | my $status_obj = $self->status_obj($name); | ||
| 326 | $status_obj->{status} = $status; | ||
| 327 | $status_obj->commit; | ||
| 328 | } | ||
| 329 | |||
| 330 | sub get_data_dir($) { | ||
| 331 | my $self = _obj(shift); | ||
| 332 | validate_pos(@_); | ||
| 333 | return $self->{data_dir}; | ||
| 334 | } | ||
| 335 | |||
| 336 | sub set_data_dir($$) { | ||
| 337 | my ($arg, $dir) = validate_pos(@_, 1, 1); | ||
| 338 | |||
| 339 | my $md = sub { | ||
| 340 | my $new_dir = shift; | ||
| 341 | mkdir $new_dir or die "mkdir $new_dir failed: $!" unless -d $new_dir; | ||
| 342 | }; | ||
| 343 | |||
| 344 | $md->($dir); | ||
| 345 | # FIXME - directory list is copy-pasted from Ubic::Admin::Setup | ||
| 346 | for my $subdir (qw[ | ||
| 347 | status simple-daemon simple-daemon/pid lock ubic-daemon tmp watchdog watchdog/lock watchdog/status | ||
| 348 | ]) { | ||
| 349 | $md->("$dir/$subdir"); | ||
| 350 | } | ||
| 351 | |||
| 352 | Ubic::Settings->data_dir($dir); | ||
| 353 | if ($SINGLETON) { | ||
| 354 | $SINGLETON->{lock_dir} = "$dir/lock"; | ||
| 355 | $SINGLETON->{status_dir} = "$dir/status"; | ||
| 356 | $SINGLETON->{tmp_dir} = "$dir/tmp"; | ||
| 357 | $SINGLETON->{data_dir} = $dir; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | sub set_ubic_dir($$); | ||
| 362 | *set_ubic_dir = \&set_data_dir; | ||
| 363 | |||
| 364 | sub set_default_user($$) { | ||
| 365 | my ($arg, $user) = validate_pos(@_, 1, 1); | ||
| 366 | |||
| 367 | Ubic::Settings->default_user($user); | ||
| 368 | } | ||
| 369 | |||
| 370 | sub get_service_dir($) { | ||
| 371 | my $self = _obj(shift); | ||
| 372 | validate_pos(@_); | ||
| 373 | return $self->{service_dir}; | ||
| 374 | } | ||
| 375 | |||
| 376 | sub set_service_dir($$) { | ||
| 377 | my ($arg, $dir) = validate_pos(@_, 1, 1); | ||
| 378 | Ubic::Settings->service_dir($dir); | ||
| 379 | if ($SINGLETON) { | ||
| 380 | $SINGLETON->{service_dir} = $dir; | ||
| 381 | undef $SINGLETON->{root}; # force lazy regeneration | ||
| 382 | } | ||
| 383 | } | ||
| 384 | |||
| 385 | sub status_file($$) { | ||
| 386 | my $self = _obj(shift); | ||
| 387 | my ($name) = validate_pos(@_, $validate_service); | ||
| 388 | return "$self->{status_dir}/".$name; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub status_obj($$) { | ||
| 392 | my $self = _obj(shift); | ||
| 393 | my ($name) = validate_pos(@_, $validate_service); | ||
| 394 | return Ubic::Persistent->new($self->status_file($name)); | ||
| 395 | } | ||
| 396 | |||
| 397 | sub status_obj_ro($$) { | ||
| 398 | my $self = _obj(shift); | ||
| 399 | my ($name) = validate_pos(@_, $validate_service); | ||
| 400 | return Ubic::Persistent->load($self->status_file($name)); | ||
| 401 | } | ||
| 402 | |||
| 403 | sub access_guard($$) { | ||
| 404 | my $self = _obj(shift); | ||
| 405 | my ($name) = validate_pos(@_, $validate_service); | ||
| 406 | return Ubic::AccessGuard->new( | ||
| 407 | Ubic::Credentials->new(service => $self->service($name)) | ||
| 408 | ); | ||
| 409 | } | ||
| 410 | |||
| 411 | sub lock($$) { | ||
| 412 | my ($self) = _obj(shift); | ||
| 413 | my ($name) = validate_pos(@_, $validate_service); | ||
| 414 | |||
| 415 | my $lock = do { | ||
| 416 | my $guard = $self->access_guard($name); | ||
| 417 | Ubic::SingletonLock->new($self->{lock_dir}."/".$name); | ||
| 418 | }; | ||
| 419 | return $lock; | ||
| 420 | } | ||
| 421 | |||
| 422 | sub do_sub($$) { | ||
| 423 | my ($self, $code) = @_; | ||
| 424 | my $result = try { | ||
| 425 | $code->(); | ||
| 426 | } catch { | ||
| 427 | die result($_); | ||
| 428 | }; | ||
| 429 | return result($result); | ||
| 430 | } | ||
| 431 | |||
| 432 | sub do_cmd($$$) { | ||
| 433 | my ($self, $name, $cmd) = @_; | ||
| 434 | $self->do_sub(sub { | ||
| 435 | my $service = $self->service($name); | ||
| 436 | |||
| 437 | my $creds = Ubic::Credentials->new( service => $service ); | ||
| 438 | |||
| 439 | if ($creds->eq(Ubic::Credentials->new)) { | ||
| 440 | # current credentials fit service expectations | ||
| 441 | return $service->$cmd(); | ||
| 442 | } | ||
| 443 | |||
| 444 | # setting just effective uid is not enough, because: | ||
| 445 | # - we can accidentally enter tainted mode, and service authors don't expect this | ||
| 446 | # - local administrator may want to allow everyone to write their own services, and leaving root as real uid is an obvious security breach | ||
| 447 | # (ubic will have to learn to compare service user with service file's owner for such policy to be safe, though - this is not implemented yet) | ||
| 448 | $self->forked_call(sub { | ||
| 449 | $creds->set(); | ||
| 450 | return $service->$cmd(); | ||
| 451 | }); | ||
| 452 | }); | ||
| 453 | } | ||
| 454 | |||
| 455 | sub forked_call { | ||
| 456 | my ($self, $callback) = @_; | ||
| 457 | my $tmp_file = $self->{tmp_dir}."/".time.".$$.".rand(1000000); | ||
| 458 | my $child; | ||
| 459 | unless ($child = fork) { | ||
| 460 | unless (defined $child) { | ||
| 461 | die "fork failed"; | ||
| 462 | } | ||
| 463 | my $result; | ||
| 464 | try { | ||
| 465 | $result = { ok => $callback->() }; | ||
| 466 | } | ||
| 467 | catch { | ||
| 468 | $result = { error => $_ }; | ||
| 469 | }; | ||
| 470 | |||
| 471 | try { | ||
| 472 | Ubic::AtomicFile::store( freeze($result) => $tmp_file ); | ||
| 473 | STDOUT->flush; | ||
| 474 | STDERR->flush; | ||
| 475 | POSIX::_exit(0); # don't allow to lock to be released - this process was forked from unknown environment, don't want to run unknown destructors | ||
| 476 | } | ||
| 477 | catch { | ||
| 478 | # probably tmp_file is not writable | ||
| 479 | warn $_; | ||
| 480 | POSIX::_exit(1); | ||
| 481 | }; | ||
| 482 | } | ||
| 483 | waitpid($child, 0); | ||
| 484 | unless (-e $tmp_file) { | ||
| 485 | die "temp file $tmp_file not found after fork"; | ||
| 486 | } | ||
| 487 | open my $fh, '<', $tmp_file or die "Can't read $tmp_file: $!"; | ||
| 488 | my $content = do { local $/; <$fh>; }; | ||
| 489 | close $fh or die "Can't close $tmp_file: $!"; | ||
| 490 | unlink $tmp_file; | ||
| 491 | my $result = thaw($content); | ||
| 492 | if ($result->{error}) { | ||
| 493 | die $result->{error}; | ||
| 494 | } | ||
| 495 | else { | ||
| 496 | return $result->{ok}; | ||
| 497 | } | ||
| 498 | } | ||
| 499 | |||
| 500 | |||
| 501 | 1; | ||
| 502 | |||
| 503 | __END__ |