FD.io VPP  v16.06
Vector Packet Processing
util.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 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  * pci.c: Linux user space PCI bus management.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39 
40 #include <vlib/vlib.h>
41 #include <vlib/unix/unix.h>
42 
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <dirent.h>
47 
49 foreach_directory_file (char * dir_name,
50  clib_error_t * (* f) (void * arg, u8 * path_name, u8 * file_name),
51  void * arg,
52  int scan_dirs)
53 {
54  DIR * d;
55  struct dirent * e;
56  clib_error_t * error = 0;
57  u8 * s, * t;
58 
59  d = opendir (dir_name);
60  if (! d)
61  {
62  /* System has no PCI bus. */
63  if (errno == ENOENT)
64  return 0;
65  return clib_error_return_unix (0, "open `%s'", dir_name);
66  }
67 
68  s = t = 0;
69  while (1)
70  {
71  e = readdir (d);
72  if (! e)
73  break;
74  if (scan_dirs)
75  {
76  if (e->d_type == DT_DIR
77  && (! strcmp (e->d_name, ".")
78  || ! strcmp (e->d_name, "..")))
79  continue;
80  }
81  else
82  {
83  if (e->d_type == DT_DIR)
84  continue;
85  }
86 
87  s = format (s, "%s/%s", dir_name, e->d_name);
88  t = format (t, "%s", e->d_name);
89  error = f (arg, s, t);
90  _vec_len (s) = 0;
91  _vec_len (t) = 0;
92 
93  if (error)
94  break;
95  }
96 
97  vec_free (s);
98  closedir (d);
99 
100  return error;
101 }
102 
103 clib_error_t *
104 write_sys_fs (char * file_name, char * fmt, ...)
105 {
106  u8 * s;
107  int fd;
108 
109  fd = open (file_name, O_WRONLY);
110  if (fd < 0)
111  return clib_error_return_unix (0, "open `%s'", file_name);
112 
113  va_list va;
114  va_start (va, fmt);
115  s = va_format (0, fmt, &va);
116  va_end (va);
117 
118  if (write (fd, s, vec_len (s)) < 0)
119  return clib_error_return_unix (0, "write `%s'", file_name);
120 
121  vec_free (s);
122  close (fd);
123  return 0;
124 }
125 
126 clib_error_t *
127 read_sys_fs (char * file_name, char * fmt, ...)
128 {
129  unformat_input_t input;
130  u8 * s = 0;
131  int fd;
132  ssize_t sz;
133  uword result;
134 
135  fd = open (file_name, O_RDONLY);
136  if (fd < 0)
137  return clib_error_return_unix (0, "open `%s'", file_name);
138 
139  vec_validate(s, 4095);
140 
141  sz = read(fd, s, vec_len (s));
142  if (sz < 0)
143  {
144  close(fd);
145  vec_free(s);
146  return clib_error_return_unix (0, "read `%s'", file_name);
147  }
148 
149  _vec_len(s) = sz;
150  unformat_init_vector(&input, s);
151 
152  va_list va;
153  va_start (va, fmt);
154  result = va_unformat (&input, fmt, &va);
155  va_end (va);
156 
157  vec_free (s);
158  close (fd);
159 
160  if (result == 0)
161  return clib_error_return (0, "unformat error");
162 
163  return 0;
164 }
165 
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:394
void unformat_init_vector(unformat_input_t *input, u8 *vector_string)
Definition: unformat.c:1000
#define clib_error_return_unix(e, args...)
Definition: error.h:115
clib_error_t * read_sys_fs(char *file_name, char *fmt,...)
Definition: util.c:127
u8 * va_format(u8 *s, char *fmt, va_list *va)
Definition: format.c:374
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:405
clib_error_t * foreach_directory_file(char *dir_name, clib_error_t *(*f)(void *arg, u8 *path_name, u8 *file_name), void *arg, int scan_dirs)
Definition: util.c:49
u64 uword
Definition: types.h:112
clib_error_t * write_sys_fs(char *file_name, char *fmt,...)
Definition: util.c:104
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
uword va_unformat(unformat_input_t *i, char *fmt, va_list *args)
Definition: unformat.c:805
#define clib_error_return(e, args...)
Definition: error.h:112
struct _unformat_input_t unformat_input_t