2013-07-01 02:03:57 +04:00
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd .
Copyright 2013 Lennart Poettering
systemd is free software ; you can redistribute it and / or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation ; either version 2.1 of the License , or
( at your option ) any later version .
systemd is distributed in the hope that it will be useful , but
WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
Lesser General Public License for more details .
You should have received a copy of the GNU Lesser General Public License
along with systemd ; If not , see < http : //www.gnu.org/licenses/>.
* * */
2013-11-20 00:12:59 +04:00
# include "unit.h"
# include "scope.h"
2013-07-01 02:03:57 +04:00
# include "dbus-unit.h"
# include "dbus-cgroup.h"
# include "dbus-kill.h"
# include "dbus-scope.h"
2013-11-20 00:12:59 +04:00
# include "bus-util.h"
2013-07-01 02:03:57 +04:00
2013-11-20 00:12:59 +04:00
static BUS_DEFINE_PROPERTY_GET_ENUM ( property_get_result , scope_result , ScopeResult ) ;
2013-07-01 02:03:57 +04:00
2013-11-20 00:12:59 +04:00
const sd_bus_vtable bus_scope_vtable [ ] = {
SD_BUS_VTABLE_START ( 0 ) ,
SD_BUS_PROPERTY ( " TimeoutStopUSec " , " t " , bus_property_get_usec , offsetof ( Scope , timeout_stop_usec ) , 0 ) ,
SD_BUS_PROPERTY ( " Result " , " s " , property_get_result , offsetof ( Scope , result ) , SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE ) ,
SD_BUS_VTABLE_END
} ;
2013-07-01 02:03:57 +04:00
2013-11-20 00:12:59 +04:00
const char * const bus_scope_changing_properties [ ] = {
" Result " ,
NULL
} ;
2013-07-01 02:03:57 +04:00
2013-07-01 02:40:56 +04:00
static int bus_scope_set_transient_property (
2013-07-01 02:03:57 +04:00
Scope * s ,
const char * name ,
2013-11-20 00:12:59 +04:00
sd_bus_message * message ,
2013-07-01 02:03:57 +04:00
UnitSetPropertiesMode mode ,
2013-11-20 00:12:59 +04:00
sd_bus_error * error ) {
2013-07-01 02:03:57 +04:00
int r ;
assert ( s ) ;
2013-11-20 00:12:59 +04:00
assert ( name ) ;
assert ( message ) ;
2013-07-01 02:03:57 +04:00
if ( streq ( name , " PIDs " ) ) {
2013-07-02 18:44:02 +04:00
unsigned n = 0 ;
2013-11-20 00:12:59 +04:00
uint32_t pid ;
2013-07-01 02:03:57 +04:00
r = set_ensure_allocated ( & s - > pids , trivial_hash_func , trivial_compare_func ) ;
if ( r < 0 )
return r ;
2013-11-20 00:12:59 +04:00
r = sd_bus_message_enter_container ( message , ' a ' , " u " ) ;
if ( r < 0 )
return r ;
2013-07-01 02:03:57 +04:00
2013-11-20 00:12:59 +04:00
while ( ( r = sd_bus_message_read ( message , " u " , & pid ) ) > 0 ) {
2013-07-01 02:03:57 +04:00
if ( pid < = 1 )
return - EINVAL ;
2013-07-01 04:47:11 +04:00
if ( mode ! = UNIT_CHECK ) {
r = set_put ( s - > pids , LONG_TO_PTR ( pid ) ) ;
if ( r < 0 & & r ! = - EEXIST )
return r ;
}
2013-07-01 02:03:57 +04:00
2013-07-01 04:47:11 +04:00
n + + ;
2013-07-01 02:03:57 +04:00
}
2013-11-20 00:12:59 +04:00
if ( r < 0 )
return r ;
r = sd_bus_message_exit_container ( message ) ;
if ( r < 0 )
return r ;
2013-07-01 02:03:57 +04:00
2013-07-01 04:47:11 +04:00
if ( n < = 0 )
2013-07-01 02:03:57 +04:00
return - EINVAL ;
return 1 ;
2013-07-02 03:34:04 +04:00
} else if ( streq ( name , " TimeoutStopUSec " ) ) {
if ( mode ! = UNIT_CHECK ) {
2013-11-20 00:12:59 +04:00
r = sd_bus_message_read ( message , " t " , & s - > timeout_stop_usec ) ;
if ( r < 0 )
return r ;
unit_write_drop_in_format ( UNIT ( s ) , mode , name , " [Scope] \n TimeoutStopSec=%lluus \n " , ( unsigned long long ) s - > timeout_stop_usec ) ;
} else {
r = sd_bus_message_skip ( message , " t " ) ;
if ( r < 0 )
return r ;
2013-07-02 03:34:04 +04:00
}
return 1 ;
2013-07-01 02:03:57 +04:00
}
return 0 ;
}
int bus_scope_set_property (
Unit * u ,
const char * name ,
2013-11-20 00:12:59 +04:00
sd_bus_message * message ,
2013-07-01 02:03:57 +04:00
UnitSetPropertiesMode mode ,
2013-11-20 00:12:59 +04:00
sd_bus_error * error ) {
2013-07-01 02:03:57 +04:00
Scope * s = SCOPE ( u ) ;
int r ;
2013-11-20 00:12:59 +04:00
assert ( s ) ;
2013-07-01 02:03:57 +04:00
assert ( name ) ;
2013-11-20 00:12:59 +04:00
assert ( message ) ;
2013-07-01 02:03:57 +04:00
2013-11-20 00:12:59 +04:00
r = bus_cgroup_set_property ( u , & s - > cgroup_context , name , message , mode , error ) ;
2013-07-01 02:03:57 +04:00
if ( r ! = 0 )
return r ;
if ( u - > load_state = = UNIT_STUB ) {
/* While we are created we still accept PIDs */
2013-11-20 00:12:59 +04:00
r = bus_scope_set_transient_property ( s , name , message , mode , error ) ;
2013-07-01 02:03:57 +04:00
if ( r ! = 0 )
return r ;
2013-07-30 04:28:22 +04:00
2013-11-20 00:12:59 +04:00
r = bus_kill_context_set_transient_property ( u , & s - > kill_context , name , message , mode , error ) ;
2013-07-30 04:28:22 +04:00
if ( r ! = 0 )
return r ;
2013-07-01 02:03:57 +04:00
}
return 0 ;
}
int bus_scope_commit_properties ( Unit * u ) {
assert ( u ) ;
unit_realize_cgroup ( u ) ;
return 0 ;
}