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
|
/*
* 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 <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;
int
main(int argc, char *argv[])
{
char *program_name;
char *work_area;
char **exec_argv;
int arg;
mode_t mode;
int opt;
char *name;
char type;
char *end;
long int major;
long int minor;
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];
work_area = getenv("OPK_WORK_AREA");
if (work_area == NULL || work_area[0] == '\0') {
fprintf(stderr, _("%s: OPK_WORK_AREA environment variable not "
"set\n"), program_name);
fprintf(stderr, _("%s: Possibly building package dependent on "
"opkg-opk with opkbuild too old to use "
"it\n"), program_name);
fprintf(stderr, _("%s: Executing system mknod instead\n"),
program_name);
exec_argv = calloc(argc + 1, sizeof(*exec_argv));
if (exec_argv == NULL) {
fprintf(stderr, _("%s: Failed to allocate memory\n"),
program_name);
return EXIT_FAILURE;
}
exec_argv[0] = malloc(strlen(MKNOD) + 1);
if (exec_argv[0] == NULL) {
fprintf(stderr, _("%s: Failed to allocate memory\n"),
program_name);
free(exec_argv);
return EXIT_FAILURE;
}
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 */
fprintf(stderr, _("%s: Failed to execute system mknod\n"),
program_name);
free(exec_argv[0]);
free(exec_argv);
return EXIT_FAILURE;
}
mode = 0;
#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) {
fprintf(stderr, _("%s: Invalid mode "
"\"%s\"\n"),
program_name, optarg);
return EXIT_FAILURE;
}
break;
default:
fprintf(stderr, _("%s: Warning: Unknown option:"
" \"%c\"\n"),
program_name, optopt);
return EXIT_FAILURE;
}
}
argc -= optind;
argv += optind;
if (argc < 2) {
fprintf(stderr, _("%s: Wrong number of operands\n"),
program_name);
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) {
fprintf(stderr, _("%s: Wrong number of "
"operands\n"),
program_name);
return EXIT_FAILURE;
}
if (mkfifo(name, mode) != 0) {
fprintf(stderr, _("%s: Failed to create link "
"\"%s\"\n"),
program_name, name);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
default:
fprintf(stderr, _("%s: Invalid node type \"%c\"\n"),
program_name, argv[1][0]);
return EXIT_FAILURE;
}
if (argc != 4) {
fprintf(stderr, _("%s: Wrong number of operands\n"),
program_name);
return EXIT_FAILURE;
}
major = strtol(argv[2], &end, 10);
if (*end != '\0') {
fprintf(stderr, _("%s: Invalid number \"%s\"\n"),
program_name, argv[2]);
return EXIT_FAILURE;
}
minor = strtol(argv[3], &end, 10);
if (*end != '\0') {
fprintf(stderr, _("%s: Invalid number \"%s\"\n"),
program_name, argv[3]);
return EXIT_FAILURE;
}
specials_file_name = malloc(strlen(work_area) + strlen("/specials")
+ 1 /* "\0" */);
if (specials_file_name == NULL) {
fprintf(stderr, _("%s: Failed to allocate memory\n"),
program_name);
return EXIT_FAILURE;
}
specials_lock_name = malloc(strlen(work_area) + strlen("/specials~")
+ 1 /* "\0" */);
if (specials_lock_name == NULL) {
fprintf(stderr, _("%s: Failed to allocate memory\n"),
program_name);
free(specials_file_name);
return EXIT_FAILURE;
}
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) {
fprintf(stderr, _("%s: Failed to create node\n"), program_name);
free(specials_file_name);
free(specials_lock_name);
return EXIT_FAILURE;
}
/* Acquire lock ("specials~"). */
for (timeout = _TIMEOUT; timeout > 0; --timeout) {
specials_fd = open(specials_lock_name, O_CREAT | O_EXCL, 0644);
if (specials_fd >= 0) {
goto opened;
}
}
fprintf(stderr, _("%s: Failed to lock specials file\n"), program_name);
free(specials_file_name);
free(specials_lock_name);
unlink(name);
return EXIT_FAILURE;
opened:
specials_file = fdopen(specials_fd, "a");
if (specials_file == NULL) {
fprintf(stderr, _("%s: Failed to open specials file stream\n"),
program_name);
close(specials_fd);
free(specials_file_name);
free(specials_lock_name);
unlink(name);
return EXIT_FAILURE;
}
/* If "specials" doesn't exist, write header. */
if (stat(specials_file_name, &stat_buf) != 0) {
if (errno != ENOENT) {
fprintf(stderr, _("%s: Failed to stat specials file\n"),
program_name);
fclose(specials_file);
free(specials_file_name);
free(specials_lock_name);
unlink(name);
return EXIT_FAILURE;
}
if (fputs("version=1\n", specials_file) < 0 ||
fputs("type major minor name\n",
specials_file) < 0) {
fprintf(stderr, _("%s: Failed to write specials file\n")
, program_name);
fclose(specials_file);
free(specials_file_name);
free(specials_lock_name);
unlink(name);
return EXIT_FAILURE;
}
}
if (fprintf(specials_file, "%c %8ld %8ld %s\n", type, major, minor,
name) < 0) {
fprintf(stderr, _("%s: Failed to write specials file\n"),
program_name);
fclose(specials_file);
free(specials_file_name);
free(specials_lock_name);
unlink(name);
return EXIT_FAILURE;
}
fclose(specials_file);
if (rename(specials_lock_name, specials_file_name) != 0) {
fprintf(stderr, _("%s: Failed to move specials file\n"),
program_name);
free(specials_file_name);
free(specials_lock_name);
unlink(name);
return EXIT_FAILURE;
}
free(specials_file_name);
free(specials_lock_name);
return EXIT_SUCCESS;
}
|