示例#1
0
/*
 *===========================================================================
 *                    ipcom_cmd_ipd
 *===========================================================================
 * Description:
 * Parameters:
 * Returns:
 */
IP_PUBLIC int
ipcom_cmd_ipd(int argc, char **argv)
{
    Ipcom_getopt   opt;
    int            i, c, msgtype;
    Ip_err         err = IPCOM_SUCCESS;
#if IPCOM_VR_MAX > 1
    int            vr = ipcom_proc_vr_get();
    int            vr_new = vr;
#endif

    if (argc < 2)
    {
usage:
        ipcom_fprintf(ip_stderr,
                      "Interpeak daemon (IPD) command, version 1.2"IP_LF
                      "usage:  "IP_LF
                      "   ipd [-V <vr>] list"IP_LF
                      "   ipd [-V <vr>] start <service>"IP_LF
                      "   ipd [-V <vr>] kill <service>"IP_LF
                      "   ipd [-V <vr>] reconfigure <service>"IP_LF
                      "   ipd [-V <vr>] <#> <service>"IP_LF
                      IP_LF);
        return 0;
    }

    ipcom_getopt_clear_r(&opt);
    while ((c = ipcom_getopt_r(argc, argv, "V:", &opt)) != -1)
    {
        switch(c)
        {
        case 'V':
#if IPCOM_VR_MAX > 1
            vr_new = ipcom_atoi(opt.optarg);
#endif
            break;
        default:
            ipcom_printf("ipd: unknown option %c"IP_LF, (char)c);
            return -1;
        }
    }

    if (opt.optind >= argc)
    {
        ipcom_printf("ipd: missing <command> argument"IP_LF);
        goto usage;
    }


    if(ipcom_strcmp(argv[opt.optind], "list") == 0)
    {
#if IPCOM_VR_MAX > 1
        if (vr != vr_new)
            ipcom_proc_vr_set(vr_new);
#endif
        ipcom_printf("Services:"IP_LF);
        for (i = 0; ipcom_ipd_products[i].name != IP_NULL; i++)
        {
            if ((argc - (opt.optind + 1)) > 0)
            {
                int p;
                for (p = opt.optind + 1; p < argc; p++)
                {
                    if (ipcom_strcasecmp(ipcom_ipd_products[i].name, argv[p]) == 0)
                        goto print_service;
                }
                continue;
            }

print_service:
            if (IP_BIT_ISSET(ipcom_ipd_products[i].flags, IPCOM_IPD_FLAG_IPD_START))
            {
#ifdef IP_PORT_OSE5
                if (ipcom_ipd_products[i].start == IP_NULL
                    && ipcom_ipd_isinstalled_ose5(ipcom_ipd_products[i].name) != IPCOM_SUCCESS)
                    continue;
#endif
                err = ipcom_ipd_send(ipcom_ipd_products[i].name, IPCOM_IPD_MSGTYPE_PING);
                ipcom_printf("%-20s %-s"IP_LF,
                             ipcom_ipd_products[i].name,
                             err == IPCOM_SUCCESS ? "started" : "killed");
            }
            else if (ipcom_ipd_products[i].start)
                ipcom_printf("%-20s %-s"IP_LF,
                             ipcom_ipd_products[i].name, "started");

        }
        ipcom_printf(IP_LF);
#if IPCOM_VR_MAX > 1
        if (vr != vr_new)
            ipcom_proc_vr_set(vr);
#endif
        return 0;
    }

    if ((argc - opt.optind) < 2)
    {
        ipcom_printf("ipd: missing <service> argument"IP_LF);
        return -1;
    }

    for (i = 0; ipcom_cmd_ipd_messages[i].name; i++)
        if (ipcom_strcmp(argv[opt.optind], ipcom_cmd_ipd_messages[i].name) == 0)
        {
            msgtype = ipcom_cmd_ipd_messages[i].msgtype;
            goto sendmsg;
        }

    if (*argv[opt.optind] == '-' && ipcom_isdigit(argv[opt.optind][1]))
    {
        /* "UNIX" signal support (using negative numbers) */
        msgtype = -ipcom_atoi(argv[opt.optind] + 1);
        goto sendmsg;
    }

    if (ipcom_isdigit(argv[opt.optind][0]))
    {
        /* positive numbers */
        msgtype = ipcom_atoi(argv[1]);
        goto sendmsg;
    }

    /* unknown command. */
    ipcom_printf ("ipd: unknown command '%s'"IP_LF, argv[opt.optind]);
    return -1;

    /* send msg */
 sendmsg:
#if IPCOM_VR_MAX > 1
    if (vr != vr_new)
        ipcom_proc_vr_set(vr_new);
#endif


    if ((argc - opt.optind) < 3)
        err = ipcom_ipd_send(argv[opt.optind+1], msgtype);
    else
    {
        Ipcom_ipd_msg   *msg;
        int             sz  = ipcom_strlen(argv[opt.optind + 2]) + 1;

        msg = ipcom_calloc(1, sz + sizeof(*msg));
        if (msg != IP_NULL)
        {
            msg->msgtype = msgtype;
            ipcom_memcpy(msg + 1, argv[opt.optind + 2], sz);
            err = ipcom_ipd_sendmsg(argv[opt.optind+1], msg, sz + sizeof(*msg));
            ipcom_free(msg);
        }
    }

    if(err == IPCOM_SUCCESS)
        ipcom_printf("ipd: %s %s ok"IP_LF, argv[opt.optind], argv[opt.optind+1]);
    else
        ipcom_printf("ipd: %s %s failed: %s"IP_LF, argv[opt.optind], argv[opt.optind+1], ipcom_err_string(err));

#if IPCOM_VR_MAX > 1
    if (vr != vr_new)
        ipcom_proc_vr_set(vr);
#endif
    return 0;
}
IP_PUBLIC unsigned long
ipcom_strtoul(const char *nptr, char **endptr, int base)
{
    const char   *s = nptr;
    unsigned long cutoff;
    unsigned long acc;
    int           neg = 0;
    int           cutlim;
    int           any;
    int           c;

    /*
     * See strtol for comments as to the logic used.
     */
    do
    {
        c = *s++;
    }
    while (ipcom_isspace (c));

    if (c == '-')
    {
        neg = 1;
        c = *s++;
    }
    else if (c == '+')
    {
        c = *s++;
    }
    
    if (((base == 0) || (base == 16)) &&
        (c == '0') &&
        ((*s == 'x') || (*s == 'X')))
    {
        c = s[1];
        s += 2;
        base = 16;
    }

    if (base == 0)
    {
        base = (c == '0' ? 8 : 10);
    }
    
    cutoff = (unsigned long)IPCOM_ULONG_MAX / (unsigned long)base;
    cutlim = (unsigned long)IPCOM_ULONG_MAX % (unsigned long)base;

    for (acc = 0, any = 0;; c = *s++)
    {
        if (ipcom_isdigit (c))
        {
            c -= '0';
        }
        else if (ipcom_isalpha (c))
        {
            c -= (ipcom_isupper (c) ? 'A' - 10 : 'a' - 10);
        }
        else
        {
            break;
        }
        
        if (c >= base)
        {
            break;
        }
        
        if ((any < 0) || (acc > cutoff) || ((acc == cutoff) && (c > cutlim)))
        {
            any = -1;
        }
        else
        {
            any = 1;
            acc *= base;
            acc += c;
        }
    }

    if (any < 0)
    {
        acc = IPCOM_ULONG_MAX;
    }
    else if (neg)
    {
        acc = -acc;
    }
    
    if (endptr != 0)
    {
        *endptr = (char *)(any ? (s - 1) : nptr);
    }
    
    return acc;
}
IP_PUBLIC long
ipcom_strtol(const char *nptr, char **endptr, int base)
{
    const char   *s = nptr;
    unsigned long cutoff;
    unsigned long acc;
    int           neg = 0;
    int           cutlim;
    int           any;
    int           c;

    /*
     * Skip white space and pick up leading +/- sign if any.
     * If base is 0, allow 0x for hex and 0 for octal, else
     * assume decimal; if base is already 16, allow 0x.
     */
    do
    {
        c = *s++;
    }
    while (ipcom_isspace (c));

    if (c == '-')
    {
        neg = 1;
        c = *s++;
    }
    else if (c == '+')
    {
        c = *s++;
    }
    
    if (((base == 0) || (base == 16)) &&
        (c == '0') &&
        ((*s == 'x') || (*s == 'X')))
    {
        c = s[1];
        s += 2;
        base = 16;
    }

    if (base == 0)
    {
        base = (c == '0' ? 8 : 10);
    }
    
    /*
     * Compute the cutoff value between legal numbers and illegal
     * numbers.  That is the largest legal value, divided by the
     * base.  An input number that is greater than this value, if
     * followed by a legal input character, is too big.  One that
     * is equal to this value may be valid or not; the limit
     * between valid and invalid numbers is then based on the last
     * digit.  For instance, if the range for longs is
     * [-2147483648..2147483647] and the input base is 10,
     * cutoff will be set to 214748364 and cutlim to either
     * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
     * a value > 214748364, or equal but the next digit is > 7 (or 8),
     * the number is too big, and we will return a range error.
     *
     * Set any if any `digits' consumed; make it negative to indicate
     * overflow.
     */

    cutoff = (neg ? -(unsigned long)IPCOM_LONG_MIN : IPCOM_LONG_MAX);
    cutlim = cutoff % (unsigned long)base;
    cutoff /= (unsigned long)base;

    for (acc = 0, any = 0;; c = *s++)
    {
        if (ipcom_isdigit (c))
        {
            c -= '0';
        }
        else if (ipcom_isalpha (c))
        {
            c -= (ipcom_isupper(c) ? 'A' - 10 : 'a' - 10);
        }
        else
        {
            break;
        }
        
        if (c >= base)
        {
            break;
        }
        
        if ((any < 0) || (acc > cutoff) || ((acc == cutoff) && (c > cutlim)))
        {
            any = -1;
        }
        else
        {
            any = 1;
            acc *= base;
            acc += c;
        }
    }

    if (any < 0)
    {
        acc = (neg ? IPCOM_LONG_MIN : IPCOM_LONG_MAX);
    }
    else if (neg)
    {
        acc = -acc;
    }
    
    if (endptr != 0)
    {
        *endptr = (char *)(any ? (s - 1) : nptr);
    }
    
    return acc;
}
示例#4
0
/*
 *===========================================================================
 *                    ipcom_shellalias_substitute
 *===========================================================================
 * Description: Determines what substitutins should be made
 * Parameters:  argc    - Old argc
 *              argv    - Old argv
 *              alias   - The alias we're working with
 *              nargv   - The new alias argv string
 * Returns:     >= 0 The total number of arguments, < 0 error code
 *
 */
IP_STATIC int
ipcom_shellalias_substitute(int                             argc,
                            char                            **argv,
                            struct Ipcom_shellalias_alias   *alias,
                            char                            **nargv)
{
    int                             nargc   = 0;
    int                             i;
    Ip_bool                         subst   = IP_FALSE;

    /* Do hunt for substitutions */
    for (i = 0; i < alias->argc; i++)
    {
        char            *ptr = alias->argv[i];
        long            start;
        long            end;
        Ip_bool         wildcard = IP_FALSE;

        /* Substitution? */
        if (*ptr++ == '!')
        {
            /* See what kind of substitution we're dealing with */
            switch (*ptr)
            {
            case '\0':
                /* Just a simple '!' */
                break;
            case '^':
                start = 1;
                ptr++;
                goto parse_range;
            case '$':
                start   = argc - 1;
                end     = argc - 1;
                goto substitute_range;
            case '*':
                wildcard = IP_TRUE;
                start   = 1;
                end     = argc - 1;
                goto substitute_range;
            case '-':
                /* This is a -number subtitution, which indicates 0 - num substitution. */
                start = 0;
                goto parse_range;
            default:
                if (ipcom_isdigit(*ptr))
                {
                    /* Resolve */
                    start = ipcom_strtol(ptr, &ptr, 10);

parse_range:
                    /* Is it a range or a single value? */
                    if (*ptr == '-')
                    {
                        ptr++;

                        /* It's a range */
                        if (*ptr == '\0')
                            end = argc - 2;
                        else if (*ptr == '$')
                            end = argc - 1;
                        else if (ipcom_isdigit(*ptr))
                        {
                            /* Resolve */
                            end = ipcom_strtol(ptr, &ptr, 10);
                        }
                        else
                        {
                            ipcom_printf("alias: [%s] !x- - substitution requested, but argument following '-' is erronous"IP_LF,
                                         alias->name);
                            return -IPCOM_ERR_INVALID_ARG;
                        }
                    }
                    else if (*ptr == '*')
                    {
                        wildcard = IP_TRUE;
                        end = argc - 1;
                    }
                    else
                        end = start;

substitute_range:
                    /* OK */
                    subst = IP_TRUE;

                    /* Verify if we've gotten the required arguments */
                    if (start < 0 || start >= argc)
                    {
                        /* We've gotten a wildcard match, but we aint got the arguments. Continue */
                        if (wildcard)
                            continue;

                        ipcom_printf("alias: [%s] !x - substitution requested, but insufficient arguments supplied"IP_LF,
                                     alias->name);
                        return -IPCOM_ERR_INVALID_ARG;
                    }

                    /* Verify that we have the required number of arguments */
                    if (end >= argc)
                    {
                        ipcom_printf("alias: [%s] !x-y - substitution requested, but insufficient arguments supplied"IP_LF,
                                     alias->name);
                        return -IPCOM_ERR_INVALID_ARG;
                    }

                    /* Sanity that start <= end */
                    if (end < start)
                    {
                        ipcom_printf("alias: [%s] !x-y - substitution requested, but x > y"IP_LF,
                                     alias->name);
                        return -IPCOM_ERR_INVALID_ARG;
                    }


                    /* Go through the arugments */
                    for (; start <= end; start++)
                    {
                        if (nargv != IP_NULL)
                            nargv[nargc] = argv[start];

                        nargc++;
                    }

                    continue;
                }
            }
        }

        if (nargv != IP_NULL)
            nargv[nargc] = alias->argv[i];
        nargc++;
    }

    /* No substitutions? */
    if (subst == IP_FALSE)
    {
        /* Add all arguments to the end of the command */
        for (i = 1; i < argc; i++)
        {
            if (nargv != IP_NULL)
                nargv[nargc] = argv[i];

            nargc++;
        }
    }


    /* Return the number of arguments */
    return nargc;
}