FD.io VPP  v16.06
Vector Packet Processing
unix-misc.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  Copyright (c) 2005 Eliot Dresselhaus
17 
18  Permission is hereby granted, free of charge, to any person obtaining
19  a copy of this software and associated documentation files (the
20  "Software"), to deal in the Software without restriction, including
21  without limitation the rights to use, copy, modify, merge, publish,
22  distribute, sublicense, and/or sell copies of the Software, and to
23  permit persons to whom the Software is furnished to do so, subject to
24  the following conditions:
25 
26  The above copyright notice and this permission notice shall be
27  included in all copies or substantial portions of the Software.
28 
29  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37 
38 #include <vppinfra/error.h>
39 #include <vppinfra/os.h>
40 #include <vppinfra/unix.h>
41 
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <sys/uio.h> /* writev */
45 #include <fcntl.h>
46 #include <stdio.h> /* for sprintf */
47 
48 clib_error_t * unix_file_n_bytes (char * file, uword * result)
49 {
50  struct stat s;
51 
52  if (stat (file, &s) < 0)
53  return clib_error_return_unix (0, "stat `%s'", file);
54 
55  if (S_ISREG (s.st_mode))
56  *result = s.st_size;
57  else
58  *result = 0;
59 
60  return /* no error */ 0;
61 }
62 
63 clib_error_t * unix_file_read_contents (char * file, u8 * result, uword n_bytes)
64 {
65  int fd = -1;
66  uword n_done, n_left;
67  clib_error_t * error = 0;
68  u8 * v = result;
69 
70  if ((fd = open (file, 0)) < 0)
71  return clib_error_return_unix (0, "open `%s'", file);
72 
73  n_left = n_bytes;
74  n_done = 0;
75  while (n_left > 0)
76  {
77  int n_read;
78  if ((n_read = read (fd, v + n_done, n_left)) < 0)
79  {
80  error = clib_error_return_unix (0, "open `%s'", file);
81  goto done;
82  }
83 
84  /* End of file. */
85  if (n_read == 0)
86  break;
87 
88  n_left -= n_read;
89  n_done += n_read;
90  }
91 
92  if (n_left > 0)
93  {
94  error = clib_error_return (0, " `%s' expected to read %wd bytes; read only %wd",
95  file, n_bytes, n_bytes - n_left);
96  goto done;
97  }
98 
99  done:
100  close (fd);
101  return error;
102 }
103 
104 clib_error_t * unix_file_contents (char * file, u8 ** result)
105 {
106  uword n_bytes;
107  clib_error_t * error = 0;
108  u8 * v;
109 
110  if ((error = unix_file_n_bytes (file, &n_bytes)))
111  return error;
112 
113  v = 0;
114  vec_resize (v, n_bytes);
115 
116  error = unix_file_read_contents (file, v, n_bytes);
117 
118  if (error)
119  vec_free (v);
120  else
121  *result = v;
122 
123  return error;
124 }
125 
126 clib_error_t * unix_proc_file_contents (char * file, u8 ** result)
127 {
128  u8 *rv = 0;
129  uword pos;
130  int bytes, fd;
131 
132  /* Unfortunately, stat(/proc/XXX) returns zero... */
133  fd = open (file, O_RDONLY);
134 
135  if (fd < 0)
136  return clib_error_return_unix (0, "open `%s'", file);
137 
138  vec_validate(rv, 4095);
139  pos = 0;
140  while (1)
141  {
142  bytes = read(fd, rv+pos, 4096);
143  if (bytes < 0)
144  {
145  close (fd);
146  vec_free (rv);
147  return clib_error_return_unix (0, "read '%s'", file);
148  }
149 
150  if (bytes == 0)
151  {
152  _vec_len(rv) = pos;
153  break;
154  }
155  pos += bytes;
156  vec_validate(rv, pos+4095);
157  }
158  *result = rv;
159  close (fd);
160  return 0;
161 }
162 
163 void os_panic (void) __attribute__ ((weak));
164 
165 void os_panic (void) { abort (); }
166 
167 void os_exit (int) __attribute__ ((weak));
168 
169 void os_exit (int code)
170 { exit (code); }
171 
172 void os_puts (u8 * string, uword string_length, uword is_error)
173  __attribute__ ((weak));
174 
175 void os_puts (u8 * string, uword string_length, uword is_error)
176 {
178  int cpu = os_get_cpu_number ();
179  char buf[64];
180  int fd = is_error ? 2 : 1;
181  struct iovec iovs[2];
182  int n_iovs = 0;
183 
184  if (m->n_cpus > 1)
185  {
186  snprintf (buf, sizeof(buf), "%d: ", cpu);
187 
188  iovs[n_iovs].iov_base = buf;
189  iovs[n_iovs].iov_len = strlen (buf);
190  n_iovs++;
191  }
192 
193  iovs[n_iovs].iov_base = string;
194  iovs[n_iovs].iov_len = string_length;
195  n_iovs++;
196 
197  if (writev (fd, iovs, n_iovs) < 0)
198  ;
199 }
200 
201 void os_out_of_memory (void) __attribute__ ((weak));
202 void os_out_of_memory (void)
203 { os_panic (); }
204 
205 uword os_get_cpu_number (void) __attribute__ ((weak));
207 { return os_get_cpu_number_inline(); }
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:394
clib_error_t * unix_file_n_bytes(char *file, uword *result)
Definition: unix-misc.c:48
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:199
void os_exit(int)
Definition: unix-misc.c:169
clib_smp_main_t clib_smp_main
Definition: mem_mheap.c:46
u32 n_cpus
Definition: smp.h:54
#define clib_error_return_unix(e, args...)
Definition: error.h:115
always_inline uword os_get_cpu_number_inline(void)
Definition: smp.h:91
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
uword os_get_cpu_number(void)
Definition: unix-misc.c:206
clib_error_t * unix_file_contents(char *file, u8 **result)
Definition: unix-misc.c:104
void os_puts(u8 *string, uword string_length, uword is_error)
Definition: unix-misc.c:175
u64 uword
Definition: types.h:112
clib_error_t * unix_file_read_contents(char *file, u8 *result, uword n_bytes)
Definition: unix-misc.c:63
unsigned char u8
Definition: types.h:56
clib_error_t * unix_proc_file_contents(char *file, u8 **result)
Definition: unix-misc.c:126
#define clib_error_return(e, args...)
Definition: error.h:112
void os_panic(void)
Definition: unix-misc.c:165
void os_out_of_memory(void)
Definition: unix-misc.c:202