Motr  M0
ut.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 #include "ut/ut_internal.h"
24 #include "ut/ut.h" /* m0_ut_redirect */
25 #include "lib/finject.h" /* m0_fi_fpoint_data */
26 #include "lib/finject_internal.h" /* m0_fi_fpoint_type_from_str */
27 #include "lib/string.h" /* m0_strdup */
28 #include "lib/errno.h" /* EINVAL */
29 #include "lib/memory.h" /* m0_free */
30 #include <yaml.h> /* yaml_parser_t */
31 #include <stdlib.h> /* system */
32 #include <err.h> /* warn */
33 #include <sys/stat.h> /* mkdir */
34 #include <unistd.h> /* dup */
35 
36 static int sandbox_remove(const char *sandbox)
37 {
38  char *cmd;
39  int rc;
40 
41  if (sandbox == NULL)
42  return 0;
43 
44  rc = asprintf(&cmd, "rm -fr '%s'", sandbox);
45  M0_ASSERT(rc > 0);
46 
47  rc = system(cmd);
48  if (rc != 0)
49  warn("*WARNING* sandbox cleanup at \"%s\" failed: %i\n",
50  sandbox, rc);
51 
52  free(cmd);
53  return rc;
54 }
55 
56 int m0_ut_sandbox_init(const char *dir)
57 {
58  int rc;
59 
60  setbuf(stdout, NULL);
61  setbuf(stderr, NULL);
62 
63  if (dir == NULL)
64  return 0;
65 
67  if (rc != 0)
68  return rc;
69 
70  rc = mkdir(dir, 0700) ?: chdir(dir);
71  if (rc != 0)
72  (void)sandbox_remove(dir);
73  return rc;
74 }
75 
76 void m0_ut_sandbox_fini(const char *dir, bool keep)
77 {
78  int rc;
79 
80  rc = chdir("..");
81  M0_ASSERT(rc == 0);
82 
83  if (!keep)
85 }
86 
87 M0_INTERNAL void m0_stream_redirect(FILE * stream, const char *path,
88  struct m0_ut_redirect *redir)
89 {
90  FILE *result;
91 
92  /*
93  * This solution is not portable and will only work on systems which
94  * supports dup(2) and dup2(2) system calls (these are supported
95  * in Linux).
96  */
97  redir->ur_stream = stream;
98  fflush(stream);
99  fgetpos(stream, &redir->ur_pos);
100  redir->ur_oldfd = fileno(stream);
101  redir->ur_fd = dup(redir->ur_oldfd);
102  M0_ASSERT(redir->ur_fd != -1);
103  result = freopen(path, "a+", stream);
104  M0_ASSERT(result != NULL);
105 }
106 
107 M0_INTERNAL void m0_stream_restore(const struct m0_ut_redirect *redir)
108 {
109  int result;
110 
111  /*
112  * see comment in m0_stream_redirect() for detailed information
113  * about how to redirect and restore standard streams
114  */
115  fflush(redir->ur_stream);
116  result = dup2(redir->ur_fd, redir->ur_oldfd);
117  M0_ASSERT(result != -1);
118  close(redir->ur_fd);
119  clearerr(redir->ur_stream);
120  fsetpos(redir->ur_stream, &redir->ur_pos);
121 }
122 
123 M0_INTERNAL bool m0_error_mesg_match(FILE * fp, const char *mesg)
124 {
125  enum {
126  MAXLINE = 1025,
127  };
128 
129  char line[MAXLINE];
130 
131  M0_PRE(fp != NULL);
132  M0_PRE(mesg != NULL);
133 
134  fseek(fp, 0L, SEEK_SET);
135  memset(line, '\0', MAXLINE);
136  while (fgets(line, MAXLINE, fp) != NULL) {
137  if (strncmp(mesg, line, strlen(mesg)) == 0)
138  return true;
139  }
140  return false;
141 }
142 
145 /*
146  * Local variables:
147  * c-indentation-style: "K&R"
148  * c-basic-offset: 8
149  * tab-width: 8
150  * fill-column: 80
151  * scroll-step: 1
152  * End:
153  */
154 /*
155  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
156  */
fpos_t ur_pos
Definition: ut.h:234
#define M0_PRE(cond)
M0_INTERNAL bool m0_error_mesg_match(FILE *fp, const char *mesg)
Definition: ut.c:123
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_stream_restore(const struct m0_ut_redirect *redir)
Definition: ut.c:107
int ur_fd
Definition: ut.h:233
int m0_ut_sandbox_init(const char *dir)
Definition: ut.c:25
#define M0_ASSERT(cond)
static int keep
Definition: base.c:303
FILE * ur_stream
Definition: ut.h:231
static int sandbox_remove(const char *sandbox)
Definition: ut.c:36
int ur_oldfd
Definition: ut.h:232
void m0_ut_sandbox_fini(const char *dir, bool keep)
Definition: ut.c:30
M0_INTERNAL void m0_stream_redirect(FILE *stream, const char *path, struct m0_ut_redirect *redir)
Definition: ut.c:87
struct inode * dir
Definition: dir.c:1028
Definition: rcv_session.c:58
int32_t rc
Definition: trigger_fop.h:47