ustream: prevent recursive calls to the read callback
[project/libubox.git] / utils.c
1 /*
2 * utils - misc libubox utility functions
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <sys/mman.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include "utils.h"
26
27 #define foreach_arg(_arg, _addr, _len, _first_addr, _first_len) \
28 for (_addr = (_first_addr), _len = (_first_len); \
29 _addr; \
30 _addr = va_arg(_arg, void **), _len = _addr ? va_arg(_arg, size_t) : 0)
31
32 #define C_PTR_ALIGN (sizeof(size_t))
33 #define C_PTR_MASK (-C_PTR_ALIGN)
34
35 void *__calloc_a(size_t len, ...)
36 {
37 va_list ap, ap1;
38 void *ret;
39 void **cur_addr;
40 size_t cur_len;
41 int alloc_len = 0;
42 char *ptr;
43
44 va_start(ap, len);
45
46 va_copy(ap1, ap);
47 foreach_arg(ap1, cur_addr, cur_len, &ret, len)
48 alloc_len += (cur_len + C_PTR_ALIGN - 1 ) & C_PTR_MASK;
49 va_end(ap1);
50
51 ptr = calloc(1, alloc_len);
52 if (!ptr) {
53 va_end(ap);
54 return NULL;
55 }
56
57 alloc_len = 0;
58 foreach_arg(ap, cur_addr, cur_len, &ret, len) {
59 *cur_addr = &ptr[alloc_len];
60 alloc_len += (cur_len + C_PTR_ALIGN - 1) & C_PTR_MASK;
61 }
62 va_end(ap);
63
64 return ret;
65 }
66
67 #ifdef LIBUBOX_COMPAT_CLOCK_GETTIME
68 #include <mach/mach_host.h> /* host_get_clock_service() */
69 #include <mach/mach_port.h> /* mach_port_deallocate() */
70 #include <mach/mach_init.h> /* mach_host_self(), mach_task_self() */
71 #include <mach/clock.h> /* clock_get_time() */
72
73 static clock_serv_t clock_realtime;
74 static clock_serv_t clock_monotonic;
75
76 static void __constructor clock_name_init(void)
77 {
78 mach_port_t host_self = mach_host_self();
79
80 host_get_clock_service(host_self, CLOCK_REALTIME, &clock_realtime);
81 host_get_clock_service(host_self, CLOCK_MONOTONIC, &clock_monotonic);
82 }
83
84 static void __destructor clock_name_dealloc(void)
85 {
86 mach_port_t self = mach_task_self();
87
88 mach_port_deallocate(self, clock_realtime);
89 mach_port_deallocate(self, clock_monotonic);
90 }
91
92 int clock_gettime(int type, struct timespec *tv)
93 {
94 int retval = -1;
95 mach_timespec_t mts;
96
97 switch (type) {
98 case CLOCK_REALTIME:
99 retval = clock_get_time(clock_realtime, &mts);
100 break;
101 case CLOCK_MONOTONIC:
102 retval = clock_get_time(clock_monotonic, &mts);
103 break;
104 default:
105 goto out;
106 }
107
108 tv->tv_sec = mts.tv_sec;
109 tv->tv_nsec = mts.tv_nsec;
110 out:
111 return retval;
112 }
113
114 #endif
115
116 void *cbuf_alloc(unsigned int order)
117 {
118 char path[] = "/tmp/cbuf-XXXXXX";
119 unsigned long size = cbuf_size(order);
120 void *ret = NULL;
121 int fd;
122
123 fd = mkstemp(path);
124 if (fd < 0)
125 return NULL;
126
127 if (unlink(path))
128 goto close;
129
130 if (ftruncate(fd, cbuf_size(order)))
131 goto close;
132
133 #ifndef MAP_ANONYMOUS
134 #define MAP_ANONYMOUS MAP_ANON
135 #endif
136
137 ret = mmap(NULL, size * 2, PROT_NONE, MAP_ANON | MAP_PRIVATE, -1, 0);
138 if (ret == MAP_FAILED) {
139 ret = NULL;
140 goto close;
141 }
142
143 if (mmap(ret, size, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_SHARED,
144 fd, 0) != ret ||
145 mmap(ret + size, size, PROT_READ | PROT_WRITE,
146 MAP_FIXED | MAP_SHARED, fd, 0) != ret + size) {
147 munmap(ret, size * 2);
148 ret = NULL;
149 }
150
151 close:
152 close(fd);
153 return ret;
154 }
155
156 void cbuf_free(void *ptr, unsigned int order)
157 {
158 munmap(ptr, cbuf_size(order) * 2);
159 }
160
161 int mkdir_p(char *dir, mode_t mask)
162 {
163 char *l;
164 int ret;
165
166 ret = mkdir(dir, mask);
167 if (!ret || errno == EEXIST)
168 return 0;
169
170 if (ret && (errno != ENOENT))
171 return -1;
172
173 l = strrchr(dir, '/');
174 if (!l || l == dir)
175 return -1;
176
177 *l = '\0';
178
179 if (mkdir_p(dir, mask))
180 return -1;
181
182 *l = '/';
183
184 ret = mkdir(dir, mask);
185 if (!ret || errno == EEXIST)
186 return 0;
187 else
188 return -1;
189 }