annotate dpid/misc_new.c @ 779:49735e578039

[mq]: add-dpid-copyright
author Jeremy Henty <onepoint@starurchin.org>
date Sat, 17 Jan 2009 10:59:15 +0000
parents f175ca0f22be
children 7bec6cfe6b6b
rev   line source
779
49735e578039 [mq]: add-dpid-copyright
Jeremy Henty <onepoint@starurchin.org>
parents: 153
diff changeset
1 /*
49735e578039 [mq]: add-dpid-copyright
Jeremy Henty <onepoint@starurchin.org>
parents: 153
diff changeset
2 * File: misc_new.c
49735e578039 [mq]: add-dpid-copyright
Jeremy Henty <onepoint@starurchin.org>
parents: 153
diff changeset
3 *
49735e578039 [mq]: add-dpid-copyright
Jeremy Henty <onepoint@starurchin.org>
parents: 153
diff changeset
4 * Copyright 2008 Jorge Arellano Cid <jcid@dillo.org>
49735e578039 [mq]: add-dpid-copyright
Jeremy Henty <onepoint@starurchin.org>
parents: 153
diff changeset
5 *
49735e578039 [mq]: add-dpid-copyright
Jeremy Henty <onepoint@starurchin.org>
parents: 153
diff changeset
6 * This program is free software; you can redistribute it and/or modify
49735e578039 [mq]: add-dpid-copyright
Jeremy Henty <onepoint@starurchin.org>
parents: 153
diff changeset
7 * it under the terms of the GNU General Public License as published by
49735e578039 [mq]: add-dpid-copyright
Jeremy Henty <onepoint@starurchin.org>
parents: 153
diff changeset
8 * the Free Software Foundation; either version 3 of the License, or
49735e578039 [mq]: add-dpid-copyright
Jeremy Henty <onepoint@starurchin.org>
parents: 153
diff changeset
9 * (at your option) any later version.
49735e578039 [mq]: add-dpid-copyright
Jeremy Henty <onepoint@starurchin.org>
parents: 153
diff changeset
10 */
49735e578039 [mq]: add-dpid-copyright
Jeremy Henty <onepoint@starurchin.org>
parents: 153
diff changeset
11
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
12 #include <stdio.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
13 #include <time.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
14 #include <stdlib.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
15 #include <string.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
16 #include <unistd.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
17 #include <sys/stat.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
18 #include <sys/types.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
19 #include <errno.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
20 #include <fcntl.h>
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
21 #include "d_size.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
22 #include "misc_new.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
23 #include "dpid_common.h"
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
24
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
25 #include "misc_new.h" /* for function prototypes */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
26
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
27
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
28 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
29 * Close a FD handling EINTR.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
30 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
31 int a_Misc_close_fd(int fd)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
32 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
33 int st;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
34
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
35 do {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
36 st = close(fd);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
37 } while (st < 0 && errno == EINTR);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
38 return st;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
39 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
40
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
41 /*! Reads a dpi tag from a socket
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
42 * \li Continues after a signal interrupt
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
43 * \Return
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
44 * Dstr pointer to tag on success, NULL on failure
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
45 * \important Caller is responsible for freeing the returned Dstr *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
46 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
47 Dstr *a_Misc_rdtag(int socket)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
48 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
49 char c = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
50 ssize_t rdlen;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
51 Dstr *tag;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
52
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
53 tag = dStr_sized_new(64);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
54
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
55 errno = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
56
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
57 do {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
58 rdlen = read(socket, &c, 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
59 if (rdlen == -1 && errno != EINTR)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
60 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
61 dStr_append_c(tag, c);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
62 } while (c != '>');
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
63
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
64 if (rdlen == -1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
65 perror("a_Misc_rdtag");
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
66 dStr_free(tag, TRUE);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
67 return (NULL);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
68 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
69 return (tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
70 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
71
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
72 /*!
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
73 * Read a dpi tag from sock
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
74 * \return
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
75 * pointer to dynamically allocated request tag
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
76 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
77 char *a_Misc_readtag(int sock)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
78 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
79 char *tag, c, buf[10];
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
80 size_t buflen, i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
81 size_t taglen = 0, tagmem = 10;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
82 ssize_t rdln = 1;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
83
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
84 tag = NULL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
85 buf[0] = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
86 buflen = sizeof(buf) / sizeof(buf[0]);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
87 // new start
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
88 tag = (char *) dMalloc(tagmem + 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
89 for (i = 0; (rdln = read(sock, &c, 1)) != 0; i++) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
90 if (i == tagmem) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
91 tagmem += tagmem;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
92 tag = (char *) dRealloc(tag, tagmem + 1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
93 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
94 tag[i] = c;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
95 taglen += rdln;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
96 if (c == '>') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
97 tag[i + 1] = '\0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
98 break;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
99 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
100 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
101 // new end
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
102 if (rdln == -1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
103 ERRMSG("a_Misc_readtag", "read", errno);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
104 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
105
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
106 return (tag);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
107 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
108
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
109 /*! Reads a dpi tag from a socket without hanging on read.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
110 * \li Continues after a signal interrupt
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
111 * \Return
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
112 * \li 1 on success
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
113 * \li 0 if input is not available within timeout microseconds.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
114 * \li -1 on failure
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
115 * \important Caller is responsible for freeing the returned Dstr *
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
116 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
117 /* Is this useful?
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
118 int a_Misc_nohang_rdtag(int socket, int timeout, Dstr **tag)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
119 {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
120 int n_fd;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
121 fd_set sock_set, select_set;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
122 struct timeval tout;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
123
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
124 FD_ZERO(&sock_set);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
125 FD_SET(socket, &sock_set);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
126
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
127 errno = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
128 do {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
129 select_set = sock_set;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
130 tout.tv_sec = 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
131 tout.tv_usec = timeout;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
132 n_fd = select(socket + 1, &select_set, NULL, NULL, &tout);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
133 } while (n_fd == -1 && errno == EINTR);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
134
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
135 if (n_fd == -1) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
136 MSG_ERR("%s:%d: a_Misc_nohang_rdtag: %s\n",
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
137 __FILE__, __LINE__, dStrerror(errno));
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
138 return(-1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
139 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
140 if (n_fd == 0) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
141 return(0);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
142 } else {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
143 *tag = a_Misc_rdtag(socket);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
144 return(1);
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
145 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
146 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
147 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
148
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
149 /*
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
150 * Alternative to mkdtemp().
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
151 * Not as strong as mkdtemp, but enough for creating a directory.
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
152 */
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
153 char *a_Misc_mkdtemp(char *template)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
154 {
88
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
155 for (;;) {
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
156 if (a_Misc_mkfname(template) && mkdir(template, 0700) == 0)
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
157 break;
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
158 if (errno == EEXIST)
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
159 continue;
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
160 return 0;
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
161 }
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
162 return template;
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
163 }
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
164
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
165 /*
153
f175ca0f22be - Typo correction.
jcid
parents: 88
diff changeset
166 * Return a new, nonexistent file name from a template
88
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
167 * (adapted from dietlibc; alternative to mkdtemp())
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
168 */
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
169 char *a_Misc_mkfname(char *template)
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
170 {
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
171 char *tmp = template + strlen(template) - 6;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
172 int i;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
173 unsigned int random;
88
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
174 struct stat stat_buf;
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
175
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
176 if (tmp < template)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
177 goto error;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
178 for (i = 0; i < 6; ++i)
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
179 if (tmp[i] != 'X') {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
180 error:
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
181 errno = EINVAL;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
182 return 0;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
183 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
184 srand((uint_t)(time(0) ^ getpid()));
88
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
185
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
186 for (;;) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
187 random = (unsigned) rand();
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
188 for (i = 0; i < 6; ++i) {
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
189 int hexdigit = (random >> (i * 5)) & 0x1f;
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
190
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
191 tmp[i] = hexdigit > 9 ? hexdigit + 'a' - 10 : hexdigit + '0';
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
192 }
88
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
193 if (stat(template, &stat_buf) == -1 && errno == ENOENT)
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
194 return template;
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
195
f1eca1aba94a - Removed a warning in dpi by adding a mkfname function.
jcid
parents: 0
diff changeset
196 MSG_ERR("a_Misc_mkfname: another round for %s \n", template);
0
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
197 }
6ee11bf9e3ea Initial revision
jcid
parents:
diff changeset
198 }