Merge "Add sm-stop and sm-start commands for SM"
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright (c) 2013-2014, 2016 Wind River Systems, Inc.
|
||||
# Copyright (c) 2013-2014, 2016, 2026 Wind River Systems, Inc.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
@@ -23,6 +23,8 @@ setuptools.setup(
|
||||
'sm-unmanage = sm_tools.sm_action:main ',
|
||||
'sm-restart-safe = sm_tools.sm_action:main ',
|
||||
'sm-restart = sm_tools.sm_action:main ',
|
||||
'sm-iface-state = sm_tools.sm_domain_interface_set_state:main '
|
||||
'sm-iface-state = sm_tools.sm_domain_interface_set_state:main ',
|
||||
'sm-stop = sm_tools.sm_action:main',
|
||||
'sm-start = sm_tools.sm_action:main'
|
||||
]}
|
||||
)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright (c) 2016 Wind River Systems, Inc.
|
||||
# Copyright (c) 2016, 2026 Wind River Systems, Inc.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
@@ -12,6 +12,8 @@ import sqlite3
|
||||
|
||||
from sm_tools.sm_api_msg_utils import restart_service as restart_service
|
||||
from sm_tools.sm_api_msg_utils import restart_service_safe as restart_service_safe
|
||||
from sm_tools.sm_api_msg_utils import stop_service as stop_service
|
||||
from sm_tools.sm_api_msg_utils import start_service as start_service
|
||||
from sm_tools.sm_api_msg_utils import database_running_name as database_name
|
||||
|
||||
|
||||
@@ -23,6 +25,10 @@ def main():
|
||||
action = "unmanage"
|
||||
elif "sm-restart-safe" == filename:
|
||||
action = "restart-safe"
|
||||
elif "sm-stop" == filename:
|
||||
action = "stop"
|
||||
elif "sm-start" == filename:
|
||||
action = "start"
|
||||
else:
|
||||
action = "restart"
|
||||
|
||||
@@ -71,6 +77,20 @@ def main():
|
||||
|
||||
print("Service (%s) is no longer being managed." % args.service)
|
||||
|
||||
elif 'stop' == action:
|
||||
# Check if already unmanaged
|
||||
if os.path.isfile(unmanage_filepath + unmanage_filename):
|
||||
print("Service (%s) is already unmanaged. "
|
||||
"sm-stop requires a managed service." % args.service)
|
||||
sys.exit(-1)
|
||||
stop_service(args.service)
|
||||
print("Service (%s) is stopped and unmanaged. "
|
||||
"Use 'sudo sm-start service %s' to start and manage again."
|
||||
% (args.service, args.service))
|
||||
elif 'start' == action:
|
||||
start_service(args.service)
|
||||
print("Service (%s) is started and managed." % args.service)
|
||||
|
||||
elif 'restart-safe' == action:
|
||||
restart_service_safe(args.service)
|
||||
print("Service (%s) is restarting." % args.service)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#
|
||||
# Copyright (c) 2016-2023 Wind River Systems, Inc.
|
||||
# Copyright (c) 2016-2023, 2026 Wind River Systems, Inc.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
@@ -19,6 +19,8 @@ SM_API_MSG_VERSION = "1"
|
||||
SM_API_MSG_REVISION = "1"
|
||||
|
||||
SM_API_MSG_TYPE_RESTART_SERVICE = "RESTART_SERVICE"
|
||||
SM_API_MSG_TYPE_STOP_SERVICE = "STOP_SERVICE"
|
||||
SM_API_MSG_TYPE_START_SERVICE = "START_SERVICE"
|
||||
SM_API_MSG_SKIP_DEP_CHECK = "skip-dep"
|
||||
|
||||
SM_API_MSG_TYPE_PROVISION_SERVICE = "PROVISION_SERVICE"
|
||||
@@ -138,3 +140,31 @@ def deprovision_service_domain_interface(service_domain,
|
||||
"sm-action", service_domain,
|
||||
service_domain_interface_name))
|
||||
_send_msg_to_sm(sm_api_msg)
|
||||
|
||||
|
||||
def stop_service(service_name):
|
||||
"""
|
||||
Message SM to stop a service.
|
||||
The service must be SM managed and the service will become
|
||||
unmanaged after stop.
|
||||
"""
|
||||
sm_api_msg = ("%s,%s,%i,%s,%s,%s"
|
||||
% (SM_API_MSG_VERSION, SM_API_MSG_REVISION, 1,
|
||||
SM_API_MSG_TYPE_STOP_SERVICE, "sm-action",
|
||||
service_name))
|
||||
|
||||
_send_msg_to_sm(sm_api_msg)
|
||||
|
||||
|
||||
def start_service(service_name):
|
||||
"""
|
||||
Message SM to start a service.
|
||||
The service does not need to be SM managed prior to calling.
|
||||
The service will become SM managed after start.
|
||||
"""
|
||||
sm_api_msg = ("%s,%s,%i,%s,%s,%s"
|
||||
% (SM_API_MSG_VERSION, SM_API_MSG_REVISION, 1,
|
||||
SM_API_MSG_TYPE_START_SERVICE, "sm-action",
|
||||
service_name))
|
||||
|
||||
_send_msg_to_sm(sm_api_msg)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// Copyright (c) 2014-2023 Wind River Systems, Inc.
|
||||
// Copyright (c) 2014-2023, 2026 Wind River Systems, Inc.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
@@ -29,6 +29,8 @@
|
||||
#define SM_API_MSG_TYPE_SET_NODE "SET_NODE"
|
||||
#define SM_API_MSG_TYPE_SET_NODE_ACK "SET_NODE_ACK"
|
||||
#define SM_API_MSG_TYPE_RESTART_SERVICE "RESTART_SERVICE"
|
||||
#define SM_API_MSG_TYPE_STOP_SERVICE "STOP_SERVICE"
|
||||
#define SM_API_MSG_TYPE_START_SERVICE "START_SERVICE"
|
||||
#define SM_API_MSG_SKIP_DEP_CHECK "skip-dep"
|
||||
#define SM_API_MSG_TYPE_PROVISION_SERVICE "PROVISION_SERVICE"
|
||||
#define SM_API_MSG_TYPE_DEPROVISION_SERVICE "DEPROVISION_SERVICE"
|
||||
@@ -541,6 +543,52 @@ static void sm_api_dispatch( int selobj, int64_t user_data )
|
||||
_callbacks.service_restart( service_name, seqno, action_flag);
|
||||
}
|
||||
}
|
||||
else if( 0 == strcmp( SM_API_MSG_TYPE_STOP_SERVICE,
|
||||
params[SM_API_MSG_TYPE_FIELD] ) )
|
||||
{
|
||||
if( params[SM_API_MSG_ORIGIN_FIELD] == NULL )
|
||||
{
|
||||
DPRINTFE( "Missing origin field in received message." );
|
||||
goto ERROR;
|
||||
}
|
||||
|
||||
if( params[SM_API_MSG_SERVICE_NAME_FIELD] == NULL )
|
||||
{
|
||||
DPRINTFE( "Missing service-name field in received message." );
|
||||
goto ERROR;
|
||||
}
|
||||
|
||||
service_name = (char*) params[SM_API_MSG_SERVICE_NAME_FIELD];
|
||||
action_flag = 0;
|
||||
|
||||
if( NULL != _callbacks.service_stop )
|
||||
{
|
||||
_callbacks.service_stop( service_name, seqno, action_flag );
|
||||
}
|
||||
}
|
||||
else if( 0 == strcmp( SM_API_MSG_TYPE_START_SERVICE,
|
||||
params[SM_API_MSG_TYPE_FIELD] ) )
|
||||
{
|
||||
if( params[SM_API_MSG_ORIGIN_FIELD] == NULL )
|
||||
{
|
||||
DPRINTFE( "Missing origin field in received message." );
|
||||
goto ERROR;
|
||||
}
|
||||
|
||||
if( params[SM_API_MSG_SERVICE_NAME_FIELD] == NULL )
|
||||
{
|
||||
DPRINTFE( "Missing service-name field in received message." );
|
||||
goto ERROR;
|
||||
}
|
||||
|
||||
service_name = (char*) params[SM_API_MSG_SERVICE_NAME_FIELD];
|
||||
action_flag = 0;
|
||||
|
||||
if( NULL != _callbacks.service_start )
|
||||
{
|
||||
_callbacks.service_start( service_name, seqno, action_flag );
|
||||
}
|
||||
}
|
||||
else if( 0 == strcmp( SM_API_MSG_TYPE_PROVISION_SERVICE,
|
||||
params[SM_API_MSG_TYPE_FIELD] ) )
|
||||
{
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// Copyright (c) 2014-2023 Wind River Systems, Inc.
|
||||
// Copyright (c) 2014-2023, 2026 Wind River Systems, Inc.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
@@ -31,6 +31,12 @@ typedef void (*SmApiNodeSetCallbackT) ( char node_name[],
|
||||
typedef void (*SmApiServiceRestartCallbackT) ( char service_name[],
|
||||
int seqno, int flag );
|
||||
|
||||
typedef void (*SmApiServiceStopCallbackT) ( char service_name[],
|
||||
int seqno, int flag );
|
||||
|
||||
typedef void (*SmApiServiceStartCallbackT) ( char service_name[],
|
||||
int seqno, int flag );
|
||||
|
||||
typedef void (*SmApiProvisionServiceCallbackT) (char service_group_name[],
|
||||
char service_name[], int seqno);
|
||||
|
||||
@@ -50,6 +56,8 @@ typedef struct
|
||||
{
|
||||
SmApiNodeSetCallbackT node_set;
|
||||
SmApiServiceRestartCallbackT service_restart;
|
||||
SmApiServiceStopCallbackT service_stop;
|
||||
SmApiServiceStartCallbackT service_start;
|
||||
SmApiProvisionServiceCallbackT provision_service;
|
||||
SmApiDeprovisionServiceCallbackT deprovision_service;
|
||||
SmApiProvisionServiceDomainInterfaceCallbackT provision_service_domain_interface;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// Copyright (c) 2014-2023 Wind River Systems, Inc.
|
||||
// Copyright (c) 2014-2023, 2026 Wind River Systems, Inc.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
@@ -238,6 +238,24 @@ static void sm_main_event_handler_api_service_restart_callback(
|
||||
|
||||
sm_service_api_restart( service_name, flag );
|
||||
}
|
||||
|
||||
static void sm_main_event_handler_api_service_stop_callback(
|
||||
char service_name[], int seqno, int flag )
|
||||
{
|
||||
DPRINTFI( "Service (%s) stop requested, seqno=%i, flag=%i.",
|
||||
service_name, seqno, flag );
|
||||
|
||||
sm_service_api_stop( service_name, flag );
|
||||
}
|
||||
|
||||
static void sm_main_event_handler_api_service_start_callback(
|
||||
char service_name[], int seqno, int flag )
|
||||
{
|
||||
DPRINTFI( "Service (%s) start requested, seqno=%i, flag=%i.",
|
||||
service_name, seqno, flag );
|
||||
|
||||
sm_service_api_start( service_name, flag );
|
||||
}
|
||||
// ****************************************************************************
|
||||
|
||||
static void sm_main_event_handler_api_provision_service_callback(
|
||||
@@ -398,6 +416,10 @@ SmErrorT sm_main_event_handler_initialize( void )
|
||||
= sm_main_event_handler_api_node_set_callback;
|
||||
_api_callbacks.service_restart
|
||||
= sm_main_event_handler_api_service_restart_callback;
|
||||
_api_callbacks.service_stop
|
||||
= sm_main_event_handler_api_service_stop_callback;
|
||||
_api_callbacks.service_start
|
||||
= sm_main_event_handler_api_service_start_callback;
|
||||
_api_callbacks.provision_service
|
||||
= sm_main_event_handler_api_provision_service_callback;
|
||||
_api_callbacks.deprovision_service
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// Copyright (c) 2014 Wind River Systems, Inc.
|
||||
// Copyright (c) 2014, 2026 Wind River Systems, Inc.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
@@ -7,8 +7,11 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include "sm_types.h"
|
||||
#include "sm_limits.h"
|
||||
#include "sm_debug.h"
|
||||
#include "sm_list.h"
|
||||
#include "sm_timer.h"
|
||||
@@ -353,6 +356,94 @@ SmErrorT sm_service_api_restart( char service_name[], int flag )
|
||||
// ****************************************************************************
|
||||
|
||||
|
||||
// ****************************************************************************
|
||||
// Service API - Stop
|
||||
// ==================
|
||||
SmErrorT sm_service_api_stop( char service_name[], int flag )
|
||||
{
|
||||
SmServiceT* service;
|
||||
SmServiceEventT event = SM_SERVICE_EVENT_DISABLE;
|
||||
char reason_text[SM_LOG_REASON_TEXT_MAX_CHAR];
|
||||
char unmanage_filepath[SM_SERVICE_ACTION_PLUGIN_EXEC_MAX_CHAR];
|
||||
SmErrorT error;
|
||||
FILE* fp;
|
||||
|
||||
service = sm_service_table_read( service_name );
|
||||
if( NULL == service )
|
||||
{
|
||||
DPRINTFE( "Failed to read service (%s), error=%s.",
|
||||
service_name, sm_error_str(SM_NOT_FOUND) );
|
||||
return( SM_NOT_FOUND );
|
||||
}
|
||||
|
||||
snprintf( reason_text, sizeof(reason_text), "stop requested" );
|
||||
|
||||
error = sm_service_fsm_event_handler( service->name, event, NULL,
|
||||
reason_text );
|
||||
if( SM_OKAY != error )
|
||||
{
|
||||
DPRINTFE( "Event (%s) not handled for service (%s).",
|
||||
sm_service_event_str( event ), service->name );
|
||||
return( error );
|
||||
}
|
||||
|
||||
// Ensure services directory exists
|
||||
mkdir(SM_RUN_SERVICES_DIRECTORY, 0700);
|
||||
|
||||
// Mark service as unmanaged after stop to prevent SM from restarting it
|
||||
snprintf( unmanage_filepath, sizeof(unmanage_filepath),
|
||||
"%s/%s.unmanaged", SM_RUN_SERVICES_DIRECTORY, service_name );
|
||||
fp = fopen( unmanage_filepath, "w" );
|
||||
if( NULL != fp )
|
||||
{
|
||||
fclose( fp );
|
||||
DPRINTFI( "Service (%s) marked as unmanaged.", service_name );
|
||||
}
|
||||
|
||||
return( SM_OKAY );
|
||||
}
|
||||
// ****************************************************************************
|
||||
|
||||
// ****************************************************************************
|
||||
// Service API - Start
|
||||
// ===================
|
||||
SmErrorT sm_service_api_start( char service_name[], int flag )
|
||||
{
|
||||
SmServiceT* service;
|
||||
SmServiceEventT event = SM_SERVICE_EVENT_ENABLE;
|
||||
char reason_text[SM_LOG_REASON_TEXT_MAX_CHAR];
|
||||
char unmanage_filepath[SM_SERVICE_ACTION_PLUGIN_EXEC_MAX_CHAR];
|
||||
SmErrorT error;
|
||||
|
||||
service = sm_service_table_read( service_name );
|
||||
if( NULL == service )
|
||||
{
|
||||
DPRINTFE( "Failed to read service (%s), error=%s.",
|
||||
service_name, sm_error_str(SM_NOT_FOUND) );
|
||||
return( SM_NOT_FOUND );
|
||||
}
|
||||
|
||||
// Remove unmanaged flag before starting so SM can manage the service
|
||||
snprintf( unmanage_filepath, sizeof(unmanage_filepath),
|
||||
"%s/%s.unmanaged", SM_RUN_SERVICES_DIRECTORY, service_name );
|
||||
unlink( unmanage_filepath );
|
||||
DPRINTFI( "Service (%s) unmanaged flag removed.", service_name );
|
||||
|
||||
snprintf( reason_text, sizeof(reason_text), "start requested" );
|
||||
|
||||
error = sm_service_fsm_event_handler( service->name, event, NULL,
|
||||
reason_text );
|
||||
if( SM_OKAY != error )
|
||||
{
|
||||
DPRINTFE( "Event (%s) not handled for service (%s).",
|
||||
sm_service_event_str( event ), service->name );
|
||||
return( error );
|
||||
}
|
||||
|
||||
return( SM_OKAY );
|
||||
}
|
||||
// ****************************************************************************
|
||||
|
||||
// ****************************************************************************
|
||||
// Service API - Audit
|
||||
// ===================
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
//
|
||||
// Copyright (c) 2014 Wind River Systems, Inc.
|
||||
// Copyright (c) 2014, 2026 Wind River Systems, Inc.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
@@ -65,6 +65,18 @@ extern SmErrorT sm_service_api_recover( char service_name[],
|
||||
extern SmErrorT sm_service_api_restart( char service_name[], int flag );
|
||||
// ****************************************************************************
|
||||
|
||||
// ****************************************************************************
|
||||
// Service API - Stop
|
||||
// ==================
|
||||
extern SmErrorT sm_service_api_stop( char service_name[], int flag );
|
||||
// ****************************************************************************
|
||||
|
||||
// ****************************************************************************
|
||||
// Service API - Start
|
||||
// ===================
|
||||
extern SmErrorT sm_service_api_start( char service_name[], int flag );
|
||||
// ****************************************************************************
|
||||
|
||||
// ****************************************************************************
|
||||
// Service API - Audit
|
||||
// ===================
|
||||
|
||||
Reference in New Issue
Block a user