lib: Add server_id_str_buf
[Samba.git] / lib / util / server_id.c
blob4a844330ef394675073cb7296263f85e0c825ec5
1 /*
2 Unix SMB/CIFS implementation.
3 Samba utility functions
4 Copyright (C) Andrew Bartlett 2011
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "librpc/gen_ndr/server_id.h"
23 bool server_id_equal(const struct server_id *p1, const struct server_id *p2)
25 if (p1->pid != p2->pid) {
26 return false;
29 if (p1->task_id != p2->task_id) {
30 return false;
33 if (p1->vnn != p2->vnn) {
34 return false;
37 if (p1->unique_id != p2->unique_id) {
38 return false;
41 return true;
44 char *server_id_str_buf(struct server_id id, struct server_id_buf *dst)
46 if (server_id_is_disconnected(&id)) {
47 strlcpy(dst->buf, "disconnected", sizeof(dst->buf));
48 } else if ((id.vnn == NONCLUSTER_VNN) && (id.task_id == 0)) {
49 snprintf(dst->buf, sizeof(dst->buf), "%llu",
50 (unsigned long long)id.pid);
51 } else if (id.vnn == NONCLUSTER_VNN) {
52 snprintf(dst->buf, sizeof(dst->buf), "%llu.%u",
53 (unsigned long long)id.pid, (unsigned)id.task_id);
54 } else if (id.task_id == 0) {
55 snprintf(dst->buf, sizeof(dst->buf), "%u:%llu",
56 (unsigned)id.vnn, (unsigned long long)id.pid);
57 } else {
58 snprintf(dst->buf, sizeof(dst->buf), "%u:%llu.%u",
59 (unsigned)id.vnn,
60 (unsigned long long)id.pid,
61 (unsigned)id.task_id);
63 return dst->buf;
66 char *server_id_str(TALLOC_CTX *mem_ctx, const struct server_id *id)
68 if (server_id_is_disconnected(id)) {
69 return talloc_strdup(mem_ctx, "disconnected");
70 } else if (id->vnn == NONCLUSTER_VNN && id->task_id == 0) {
71 return talloc_asprintf(mem_ctx,
72 "%llu",
73 (unsigned long long)id->pid);
74 } else if (id->vnn == NONCLUSTER_VNN) {
75 return talloc_asprintf(mem_ctx,
76 "%llu.%u",
77 (unsigned long long)id->pid,
78 (unsigned)id->task_id);
79 } else if (id->task_id == 0) {
80 return talloc_asprintf(mem_ctx,
81 "%u:%llu",
82 (unsigned)id->vnn,
83 (unsigned long long)id->pid);
84 } else {
85 return talloc_asprintf(mem_ctx,
86 "%u:%llu.%u",
87 (unsigned)id->vnn,
88 (unsigned long long)id->pid,
89 (unsigned)id->task_id);
93 struct server_id server_id_from_string(uint32_t local_vnn,
94 const char *pid_string)
96 struct server_id result;
97 unsigned long long pid;
98 unsigned int vnn, task_id = 0;
100 ZERO_STRUCT(result);
103 * We accept various forms with 1, 2 or 3 component forms
104 * because the server_id_str() can print different forms, and
105 * we want backwards compatibility for scripts that may call
106 * smbclient.
108 if (sscanf(pid_string, "%u:%llu.%u", &vnn, &pid, &task_id) == 3) {
109 result.vnn = vnn;
110 result.pid = pid;
111 result.task_id = task_id;
112 } else if (sscanf(pid_string, "%u:%llu", &vnn, &pid) == 2) {
113 result.vnn = vnn;
114 result.pid = pid;
115 } else if (sscanf(pid_string, "%llu.%u", &pid, &task_id) == 2) {
116 result.vnn = local_vnn;
117 result.pid = pid;
118 result.task_id = task_id;
119 } else if (sscanf(pid_string, "%llu", &pid) == 1) {
120 result.vnn = local_vnn;
121 result.pid = pid;
122 } else if (strcmp(pid_string, "disconnected") ==0) {
123 server_id_set_disconnected(&result);
124 } else {
125 result.vnn = NONCLUSTER_VNN;
126 result.pid = UINT64_MAX;
128 return result;
132 * Set the serverid to the special value that represents a disconnected
133 * client for (e.g.) durable handles.
135 void server_id_set_disconnected(struct server_id *id)
137 SMB_ASSERT(id != NULL);
139 id->pid = UINT64_MAX;
140 id->task_id = UINT32_MAX;
141 id->vnn = NONCLUSTER_VNN;
142 id->unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
144 return;
148 * check whether a serverid is the special placeholder for
149 * a disconnected client
151 bool server_id_is_disconnected(const struct server_id *id)
153 struct server_id dis;
155 SMB_ASSERT(id != NULL);
157 server_id_set_disconnected(&dis);
159 return server_id_equal(id, &dis);