Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Command `pe', `set', `unset' backend
0003  *
0004  * @file cmd_env.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <utlist.h>
0039 #include <errno.h>
0040 
0041 #include <ndrstandard.h>
0042 
0043 #include <ndebug.h>
0044 #include <userlog.h>
0045 #include <ndrxd.h>
0046 #include <ndrxdcmn.h>
0047 
0048 #include "cmd_processor.h"
0049 #include <atmi_shm.h>
0050 /*---------------------------Externs------------------------------------*/
0051 extern char **environ;
0052 /*---------------------------Macros-------------------------------------*/
0053 /*---------------------------Enums--------------------------------------*/
0054 /*---------------------------Typedefs-----------------------------------*/
0055 /*---------------------------Globals------------------------------------*/
0056 /*---------------------------Statics------------------------------------*/
0057 /*---------------------------Prototypes---------------------------------*/
0058 
0059 /**
0060  * Modify reply according the data.
0061  * @param call
0062  * @param pm
0063  */
0064 expublic void pe_reply_mod(command_reply_t *reply, size_t *send_size, mod_param_t *params)
0065 {
0066     command_reply_pe_t * pe_info = (command_reply_pe_t *)reply;
0067     char *env = (char *)params->mod_param1;
0068     pm_node_svc_t *elt;
0069     
0070     reply->msg_type = NDRXD_CALL_TYPE_PE;
0071     /* calculate new send size */
0072     *send_size += (sizeof(command_reply_pe_t) - sizeof(command_reply_t));
0073 
0074     /* Copy data to reply structure */
0075     NDRX_STRCPY_SAFE(pe_info->env, env);
0076     
0077     NDRX_LOG(log_debug, "magic: %ld", pe_info->rply.magic);
0078 }
0079 
0080 /**
0081  * Callback to report startup progress
0082  * @param call
0083  * @param pm
0084  * @return
0085  */
0086 exprivate void pe_progress(command_call_t * call, char *env)
0087 {
0088     int ret=EXSUCCEED;
0089     mod_param_t params;
0090 
0091     NDRX_LOG(log_debug, "startup_progress enter");
0092     memset(&params, 0, sizeof(mod_param_t));
0093 
0094     /* pass to reply process model node */
0095     params.mod_param1 = (void *)env;
0096 
0097     if (EXSUCCEED!=simple_command_reply(call, ret, NDRXD_CALL_FLAGS_RSPHAVE_MORE,
0098                             /* hook up the reply */
0099                             &params, pe_reply_mod, 0L, 0, NULL))
0100     {
0101         userlog("Failed to send progress back to [%s]", call->reply_queue);
0102     }
0103 
0104     NDRX_LOG(log_debug, "startup_progress exit");
0105 }
0106 
0107 /**
0108  * Call to pe command
0109  * @param args
0110  * @return
0111  */
0112 expublic int cmd_pe (command_call_t * call, char *data, size_t len, int context)
0113 {
0114     int ret=EXSUCCEED;
0115     int i = 1;
0116     char *s = *environ;
0117     
0118     for (; s; i++)
0119     {
0120         pe_progress(call, s);
0121         
0122         s = *(environ+i);
0123     }
0124 
0125     if (EXSUCCEED!=simple_command_reply(call, ret, 0L, NULL, NULL, 0L, 0, NULL))
0126     {
0127         userlog("Failed to send reply back to [%s]", call->reply_queue);
0128     }
0129     NDRX_LOG(log_warn, "cmd_ppm returns with status %d", ret);
0130     
0131 out:
0132     return ret;
0133 }
0134 
0135 /**
0136  * Call to set command
0137  * @param args
0138  * @return
0139  */
0140 static int cmd_set_common (command_call_t * call, char *data, size_t len, int context)
0141 {
0142     int ret=EXSUCCEED;
0143     command_setenv_t *envcall = (command_setenv_t *)call;
0144     char *name;
0145     char *value;
0146     int err_ret = 0;
0147     char errmsg[RPLY_ERR_MSG_MAX] = {EXEOS};
0148     
0149     name = envcall->env;
0150     if (NULL==(value=strchr(name, '=')))
0151     {
0152         userlog("No = in env value [%s]", envcall->env);
0153         NDRX_LOG(log_error, "No = in env value [%s]", envcall->env);
0154         
0155         snprintf(errmsg, sizeof(errmsg), "Invalid argument");
0156         err_ret = NDRXD_EINVAL;
0157         
0158         EXFAIL_OUT(ret);
0159     }
0160 
0161     *value=EXEOS;
0162     value++;
0163     
0164     NDRX_LOG(log_debug, "Setting env: name: [%s] value: [%s]", envcall->env, value);
0165     userlog("Setting env: name: [%s] value: [%s]", envcall->env, value);
0166      
0167     if (EXEOS==*value)
0168     {
0169         NDRX_LOG(log_debug, "Unsetting....");
0170         if (EXSUCCEED!=unsetenv(name))
0171         {
0172             int err = errno;
0173             err_ret = NDRXD_EENVFAIL;
0174             
0175             userlog("unsetenv failed for [%s]: %s", envcall->env, strerror(err));
0176             NDRX_LOG(log_error, "unsetenv failed for [%s]: %s", envcall->env, strerror(err));
0177             
0178             
0179             snprintf(errmsg, sizeof(errmsg), "unsetenv() failed: %.200s", strerror(err));
0180             
0181             EXFAIL_OUT(ret);
0182         }
0183     }
0184     else if (EXSUCCEED!=setenv(name, value, 1))
0185     {
0186         int err = errno;
0187         err_ret = NDRXD_EENVFAIL;
0188         userlog("setenv failed for [%s]: %s", envcall->env, strerror(err));   
0189         NDRX_LOG(log_error, "setenv failed for [%s]: %s", envcall->env, strerror(err));
0190         
0191         snprintf(errmsg, sizeof(errmsg), "setenv() failed: %.200s", strerror(err));
0192         EXFAIL_OUT(ret);
0193     }
0194     
0195 out:
0196     if (EXSUCCEED!=simple_command_reply(call, ret, 0L, NULL, NULL, 0L, err_ret, errmsg))
0197     {
0198         userlog("Failed to send reply back to [%s]", call->reply_queue);
0199     }
0200 
0201     NDRX_LOG(log_warn, "cmd_set returns with status %d", ret);
0202     
0203     return EXSUCCEED; /* Do not want to break the system! */
0204 }
0205 
0206 /**
0207  * Call to set command
0208  * @param args
0209  * @return
0210  */
0211 expublic int cmd_set (command_call_t * call, char *data, size_t len, int context)
0212 {
0213     return cmd_set_common (call, data, len, context);
0214 }
0215 
0216 /**
0217  * Call to unset command
0218  * @param args
0219  * @return
0220  */
0221 expublic int cmd_unset (command_call_t * call, char *data, size_t len, int context)
0222 {
0223     return cmd_set_common (call, data, len, context);
0224 }
0225 
0226 /* vim: set ts=4 sw=4 et smartindent: */