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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
/*
* Copyright (C) 2023 Patrick McDermott
*
* This file is part of opkg-opk.
*
* opkg-opk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* opkg-opk 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 opkg-opk. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include "defs.h"
#include "mode.h"
#ifdef ENABLE_NLS
#include <locale.h>
#include <libintl.h>
#define _(msgid) gettext(msgid)
#else
#define _(msgid) (msgid)
#endif
#ifdef HAVE_GETOPT_LONG
#include <getopt.h>
#endif
static const char *_OPTSTRING = "m:";
#ifdef HAVE_GETOPT_LONG
static const struct option _LONGOPTS[] = {
{
.name = "mode",
.has_arg = 1,
.flag = NULL,
.val = 'm',
},
};
#endif
static const unsigned int _TIMEOUT = 10U;
static char *_program_name;
static void __attribute__((format(__printf__, 1, 2)))
_err(const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "%s: ", _program_name);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fputc('\n', stderr);
}
static void __attribute__((format(__printf__, 1, 2)))
_errno(const char *fmt, ...)
{
va_list ap;
fprintf(stderr, "%s: ", _program_name);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, ": %s\n", strerror(errno));
}
static int
_get_work_area(int argc, char *argv[], char **work_area)
{
char **exec_argv;
int arg;
*work_area = getenv("OPK_WORK_AREA");
if (*work_area == NULL || *work_area[0] == '\0') {
_err(_("OPK_WORK_AREA environment variable not set"));
_err(_("Possibly building package dependent on opkg-opk with "
"opkbuild too old to use it"));
_err(_("Executing system mknod instead"));
exec_argv = calloc(argc + 1, sizeof(*exec_argv));
if (exec_argv == NULL) {
_err(_("Failed to allocate memory"));
return OPKG_OPK_ERROR;
}
exec_argv[0] = malloc(strlen(MKNOD) + 1);
if (exec_argv[0] == NULL) {
_err(_("Failed to allocate memory"));
free(exec_argv);
return OPKG_OPK_ERROR;
}
strcpy(exec_argv[0], MKNOD);
for (arg = 1; arg < argc; ++arg) {
exec_argv[arg] = argv[arg];
}
exec_argv[argc] = NULL;
execve(MKNOD, exec_argv, NULL); /* Shouldn't return */
_err(_("Failed to execute system mknod"));
free(exec_argv[0]);
free(exec_argv);
return OPKG_OPK_ERROR;
}
return OPKG_OPK_OK;
}
static int
_strtol_or_err(const char *str, long int *l)
{
char *end;
*l = strtol(str, &end, 10);
if (*end != '\0') {
_err(_("Invalid number \"%s\""), str);
return OPKG_OPK_ERROR;
}
return OPKG_OPK_OK;
}
int
main(int argc, char *argv[])
{
char *work_area;
mode_t mode;
int opt;
char *name;
char type;
long int major;
long int minor;
size_t work_area_len;
size_t specials_file_name_len;
char *specials_file_name;
char *specials_lock_name;
int fd;
unsigned int timeout;
int specials_fd;
FILE *specials_file;
struct stat stat_buf;
#ifdef ENABLE_NLS
bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
bind_textdomain_codeset(PACKAGE, "UTF-8");
#endif
textdomain(PACKAGE);
setlocale(LC_ALL, "");
#endif
_program_name = argv[0];
if (_get_work_area(argc, argv, &work_area) != OPKG_OPK_OK) {
return EXIT_FAILURE;
}
mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
#ifdef HAVE_GETOPT_LONG
while ((opt = getopt_long(argc, argv, _OPTSTRING, _LONGOPTS, NULL))
!= -1) {
#else
while ((opt = getopt(argc, argv, _OPTSTRING)) != -1) {
#endif
switch(opt) {
case 'm':
mode = umask(0);
umask(mode);
mode = 0666 ^ mode;
if (opkg_opk_helper_mode_parse(mode, optarg,
&mode) != OPKG_OPK_OK) {
_err(_("Invalid mode \"%s\""), optarg);
return EXIT_FAILURE;
}
break;
default:
_err(_("Warning: Unknown option: \"%c\""),
optopt);
}
}
argc -= optind;
argv += optind;
if (argc < 2) {
_err(_("Wrong number of operands"));
return EXIT_FAILURE;
}
name = argv[0];
switch (argv[1][0]) {
case 'c':
case 'u':
type = 'c';
break;
case 'b':
type = 'b';
break;
case 'p':
if (argc != 2) {
_err(_("Wrong number of operands"));
return EXIT_FAILURE;
}
if (mkfifo(name, mode) != 0) {
_errno(_("Failed to create link \"%s\""), name);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
default:
_err(_("Invalid node type \"%c\""), argv[1][0]);
return EXIT_FAILURE;
}
if (argc != 4) {
_err(_("Wrong number of operands"));
return EXIT_FAILURE;
}
if (_strtol_or_err(argv[2], &major) != OPKG_OPK_OK ||
_strtol_or_err(argv[3], &minor) != OPKG_OPK_OK) {
return EXIT_FAILURE;
}
/* Find path of file relative to binary package data directory within
* build work area. */
work_area_len = strlen(work_area);
if (work_area[work_area_len - 1] == '/') {
--work_area_len;
}
if (strncmp(name, work_area, work_area_len) != 0 ||
name[work_area_len] != '/') {
_err(_("Node \"%s\" not within work area \"%s\""),
name, work_area);
return EXIT_FAILURE;
}
do {
++work_area_len;
if (name[work_area_len] == '\0') {
_err(_("Node \"%s\" not within a binary package data "
"directory"), name);
return EXIT_FAILURE;
}
} while (name[work_area_len] != '/');
specials_file_name_len = strlen(work_area) + strlen("/specials") + 1;
specials_file_name = malloc(specials_file_name_len * 2 + 1);
if (specials_file_name == NULL) {
_err(_("Failed to allocate memory"));
return EXIT_FAILURE;
}
specials_lock_name = specials_file_name + specials_file_name_len ;
sprintf(specials_file_name, "%s/specials", work_area);
sprintf(specials_lock_name, "%s/specials~", work_area);
/* Create the named node. This should be done before appending to the
* specials file, in case a file by the specified name already exists.
*/
fd = open(name, O_CREAT | O_EXCL, mode);
if (fd < 0 || close(fd) < 0) {
_errno(_("Failed to create node"));
goto err0;
}
/* Acquire lock ("specials~"). */
for (timeout = _TIMEOUT; timeout > 0; --timeout) {
specials_fd = open(specials_lock_name,
O_CREAT | O_EXCL | O_WRONLY, 0644);
if (specials_fd >= 0) {
goto opened;
}
}
_errno(_("Failed to lock specials file"));
goto err1;
opened:
close(specials_fd);
specials_file = fopen(specials_file_name, "a");
if (specials_file == NULL) {
_errno(_("Failed to open specials file stream"));
goto err2;
}
/* If "specials" is empty, write header. */
if (fstat(fileno(specials_file), &stat_buf) != 0) {
_errno(_("Failed to stat specials file"));
goto err3;
}
if (stat_buf.st_size == 0) {
if (fputs("version=1\n", specials_file) < 0 ||
fputs("type major minor name\n",
specials_file) < 0) {
_errno(_("Failed to write specials file"));
goto err3;
}
}
if (fprintf(specials_file, "%c %8ld %8ld %s\n",
type, major, minor, name + work_area_len) < 0) {
_errno(_("Failed to write specials file"));
goto err3;
}
fclose(specials_file);
unlink(specials_lock_name);
free(specials_file_name);
return EXIT_SUCCESS;
err3:
fclose(specials_file);
err2:
unlink(specials_lock_name);
err1:
unlink(name);
err0:
free(specials_file_name);
return EXIT_FAILURE;
}
|