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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 Copyright (c) 2005 Eliot Dresselhaus
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:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
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.
38 #include <vppinfra/error.h>
39 #include <vppinfra/os.h>
40 #include <vppinfra/unix.h>
43 #include <sys/types.h>
44 #include <sys/uio.h> /* writev */
46 #include <stdio.h> /* for sprintf */
48 clib_error_t * unix_file_n_bytes (char * file, uword * result)
52 if (stat (file, &s) < 0)
53 return clib_error_return_unix (0, "stat `%s'", file);
55 if (S_ISREG (s.st_mode))
60 return /* no error */ 0;
63 clib_error_t * unix_file_read_contents (char * file, u8 * result, uword n_bytes)
67 clib_error_t * error = 0;
70 if ((fd = open (file, 0)) < 0)
71 return clib_error_return_unix (0, "open `%s'", file);
78 if ((n_read = read (fd, v + n_done, n_left)) < 0)
80 error = clib_error_return_unix (0, "open `%s'", file);
94 error = clib_error_return (0, " `%s' expected to read %wd bytes; read only %wd",
95 file, n_bytes, n_bytes - n_left);
104 clib_error_t * unix_file_contents (char * file, u8 ** result)
107 clib_error_t * error = 0;
110 if ((error = unix_file_n_bytes (file, &n_bytes)))
114 vec_resize (v, n_bytes);
116 error = unix_file_read_contents (file, v, n_bytes);
126 clib_error_t * unix_proc_file_contents (char * file, u8 ** result)
132 /* Unfortunately, stat(/proc/XXX) returns zero... */
133 fd = open (file, O_RDONLY);
136 return clib_error_return_unix (0, "open `%s'", file);
138 vec_validate(rv, 4095);
142 bytes = read(fd, rv+pos, 4096);
147 return clib_error_return_unix (0, "read '%s'", file);
156 vec_validate(rv, pos+4095);
163 void os_panic (void) __attribute__ ((weak));
165 void os_panic (void) { abort (); }
167 void os_exit (int) __attribute__ ((weak));
169 void os_exit (int code)
172 void os_puts (u8 * string, uword string_length, uword is_error)
173 __attribute__ ((weak));
175 void os_puts (u8 * string, uword string_length, uword is_error)
177 clib_smp_main_t * m = &clib_smp_main;
178 int cpu = os_get_cpu_number ();
180 int fd = is_error ? 2 : 1;
181 struct iovec iovs[2];
186 snprintf (buf, sizeof(buf), "%d: ", cpu);
188 iovs[n_iovs].iov_base = buf;
189 iovs[n_iovs].iov_len = strlen (buf);
193 iovs[n_iovs].iov_base = string;
194 iovs[n_iovs].iov_len = string_length;
197 if (writev (fd, iovs, n_iovs) < 0)
201 void os_out_of_memory (void) __attribute__ ((weak));
202 void os_out_of_memory (void)
205 uword os_get_cpu_number (void) __attribute__ ((weak));
206 uword os_get_cpu_number (void)
207 { return os_get_cpu_number_inline(); }