Motr  M0
uuuid.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2015-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 
30 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LIB
31 #include "lib/trace.h"
32 #include "lib/uuid.h"
33 #include "lib/errno.h" /* EINVAL */
34 
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 
39 enum {
41 };
46 static const char *uuid_file = "/etc/machine-id";
54  "00000000-0000-0000-0000-000000000000"; /* nil UUID */
55 
57 static bool use_default_node_uuid = false;
58 
59 void m0_kmod_uuid_file_set(const char *path)
60 {
61  uuid_file = path;
62 }
63 
64 void m0_node_uuid_string_set(const char *uuid)
65 {
66  use_default_node_uuid = true;
67  if (uuid != NULL) {
70  }
71 }
72 
78 {
79  int fd;
80  int rc = 0;
81  char mid[MACHINE_ID_LEN + 1];
82 
84 
86  strncpy(buf, default_node_uuid, M0_UUID_STRLEN + 1);
87  buf[M0_UUID_STRLEN] = '\0';
88  } else {
89  fd = open(uuid_file, O_RDONLY);
90  if (fd < 0) {
91  return M0_ERR_INFO(-EINVAL,
92  "open(\"%s\", O_RDONLY)=%d "
93  "errno=%d: ",
94  uuid_file, fd, errno);
95  }
96  if (read(fd, mid, MACHINE_ID_LEN) == MACHINE_ID_LEN) {
97  rc = 0;
98  snprintf(buf, M0_UUID_STRLEN + 1,
99  "%.8s-%.4s-%.4s-%.4s-%.12s",
100  mid, mid + 8, mid + 8 + 4, mid + 8 + 4 + 4,
101  mid + 8 + 4 + 4 + 4);
102  buf[M0_UUID_STRLEN] = '\0';
103  } else {
104  rc = M0_ERR_INFO(-EINVAL, "Incorrect data in %s",
105  uuid_file);
106  }
107  close(fd);
108  }
109 
110  return M0_RC(rc);
111 }
112 
113 #undef M0_TRACE_SUBSYSTEM
114 
117 /*
118  * Local variables:
119  * c-indentation-style: "K&R"
120  * c-basic-offset: 8
121  * tab-width: 8
122  * fill-column: 80
123  * scroll-step: 1
124  * End:
125  */
126 /*
127  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
128  */
struct m0_uint128 uuid[1000]
Definition: uuid.c:73
#define NULL
Definition: misc.h:38
Definition: sock.c:887
static bool use_default_node_uuid
Definition: uuuid.c:57
return M0_RC(rc)
#define M0_ERR_INFO(rc, fmt,...)
Definition: trace.h:215
static const char * uuid_file
Definition: uuuid.c:46
int m0_node_uuid_string_get(char buf[M0_UUID_STRLEN+1])
Definition: kuuid.c:51
void m0_node_uuid_string_set(const char *uuid)
Definition: uuuid.c:64
static char default_node_uuid[M0_UUID_STRLEN+1]
Definition: uuuid.c:53
struct m0t1fs_filedata * fd
Definition: dir.c:1030
void m0_kmod_uuid_file_set(const char *path)
Definition: uuuid.c:59
int32_t rc
Definition: trigger_fop.h:47
#define M0_BASSERT(cond)