summaryrefslogtreecommitdiff
path: root/src/irmd/reg/prog.c
blob: 660a76744c54537073c78f6dbf3df6d87f4b4327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Ouroboros - Copyright (C) 2016 - 2024
 *
 * The IPC Resource Manager - Registry - Programs
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#if defined(__linux__) || defined(__CYGWIN__)
#define _DEFAULT_SOURCE
#else
#define _POSIX_C_SOURCE 200809L
#endif

#include <ouroboros/errno.h>
#include <ouroboros/irm.h>
#include <ouroboros/utils.h>

#include "prog.h"
#include "utils.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>


static char ** create_argv(const char *  prog,
                           size_t        argc,
                           char **       argv)
{
        char ** argv2;
        size_t  i;

        argv2 = malloc((argc + 2) * sizeof(*argv2)); /* prog + args + NULL */
        if (argv2 == 0)
                goto fail_malloc;

        argv2[0] = strdup(prog);
        if (argv2[0] == NULL)
                goto fail_prog;

        for (i = 1; i <= argc; ++i) {
                argv2[i] = strdup(argv[i - 1]);
                if (argv2[i] == NULL)
                        goto fail_arg;
        }

        argv2[argc + 1] = NULL;

        return argv2;

 fail_arg:
        argvfree(argv2);
 fail_prog:
        free(argv2);
 fail_malloc:
        return NULL;
}

struct reg_prog * reg_prog_create(const char * prog,
                                  uint32_t     flags,
                                  int          argc,
                                  char **      argv)
{
        struct reg_prog * p;

        assert(prog);

        p = malloc(sizeof(*p));
        if (p == NULL)
                goto fail_malloc;

        memset(p, 0, sizeof(*p));

        p->prog  = strdup(path_strip(prog));
        if (p->prog == NULL)
                goto fail_prog;

        if (argc > 0 && flags & BIND_AUTO) {
                p->argv = create_argv(prog, argc, argv);
                if (p->argv == NULL)
                        goto fail_argv;
        }

        list_head_init(&p->next);
        list_head_init(&p->names);

        p->flags = flags;

        return p;

 fail_argv:
        free(p->prog);
 fail_prog:
        free(p);
 fail_malloc:
        return NULL;
}

void reg_prog_destroy(struct reg_prog * prog)
{
        struct list_head * p;
        struct list_head * h;

        if (prog == NULL)
                return;

        list_for_each_safe(p, h, &prog->names) {
                struct str_el * s = list_entry(p, struct str_el, next);
                list_del(&s->next);
                free(s->str);
                free(s);
        }

        argvfree(prog->argv);
        free(prog->prog);
        free(prog);
}

int reg_prog_add_name(struct reg_prog * prog,
                      const char *      name)
{
        struct str_el * s;

        if (prog == NULL || name == NULL)
                return -EINVAL;

        s = malloc(sizeof(*s));
        if (s == NULL)
                goto fail_malloc;

        s->str = strdup(name);
        if(s->str == NULL)
                goto fail_name;

        list_add(&s->next, &prog->names);

        return 0;

 fail_name:
        free(s);
 fail_malloc:
        return -ENOMEM;
}

void reg_prog_del_name(struct reg_prog * prog,
                       const char *      name)
{
        struct list_head * p;
        struct list_head * h;

        list_for_each_safe(p, h, &prog->names) {
                struct str_el * s = list_entry(p, struct str_el, next);
                if (!strcmp(name, s->str)) {
                        list_del(&s->next);
                        free(s->str);
                        free(s);
                }
        }
}