lib/util: let server_id_str() skip the task_id if 0 in the cluster case too
[metze/samba/wip.git] / lib / util / server_id.c
1 /*
2    Unix SMB/CIFS implementation.
3    Samba utility functions
4    Copyright (C) Andrew Bartlett 2011
5
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.
10
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.
15
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/>.
18 */
19
20 #include "includes.h"
21 #include "librpc/gen_ndr/server_id.h"
22
23 bool server_id_equal(const struct server_id *p1, const struct server_id *p2)
24 {
25         if (p1->pid != p2->pid) {
26                 return false;
27         }
28
29         if (p1->task_id != p2->task_id) {
30                 return false;
31         }
32
33         if (p1->vnn != p2->vnn) {
34                 return false;
35         }
36
37         if (p1->unique_id != p2->unique_id) {
38                 return false;
39         }
40
41         return true;
42 }
43
44 char *server_id_str(TALLOC_CTX *mem_ctx, const struct server_id *id)
45 {
46         if (id->vnn == NONCLUSTER_VNN && id->task_id == 0) {
47                 return talloc_asprintf(mem_ctx,
48                                        "%llu",
49                                        (unsigned long long)id->pid);
50         } else if (id->vnn == NONCLUSTER_VNN) {
51                 return talloc_asprintf(mem_ctx,
52                                        "%llu.%u",
53                                        (unsigned long long)id->pid,
54                                        (unsigned)id->task_id);
55         } else if (id->task_id == 0) {
56                 return talloc_asprintf(mem_ctx,
57                                        "%u:%llu",
58                                        (unsigned)id->vnn,
59                                        (unsigned long long)id->pid);
60         } else {
61                 return talloc_asprintf(mem_ctx,
62                                        "%u:%llu.%u",
63                                        (unsigned)id->vnn,
64                                        (unsigned long long)id->pid,
65                                        (unsigned)id->task_id);
66         }
67 }
68
69 struct server_id server_id_from_string(uint32_t local_vnn,
70                                        const char *pid_string)
71 {
72         struct server_id result;
73         unsigned long long pid;
74         unsigned int vnn, task_id = 0;
75
76         ZERO_STRUCT(result);
77
78         /*
79          * We accept various forms with 1, 2 or 3 component forms
80          * because the server_id_str() can print different forms, and
81          * we want backwards compatibility for scripts that may call
82          * smbclient.
83          */
84         if (sscanf(pid_string, "%u:%llu.%u", &vnn, &pid, &task_id) == 3) {
85                 result.vnn = vnn;
86                 result.pid = pid;
87                 result.task_id = task_id;
88         } else if (sscanf(pid_string, "%u:%llu", &vnn, &pid) == 2) {
89                 result.vnn = vnn;
90                 result.pid = pid;
91         } else if (sscanf(pid_string, "%llu.%u", &pid, &task_id) == 2) {
92                 result.vnn = local_vnn;
93                 result.pid = pid;
94                 result.task_id = task_id;
95         } else if (sscanf(pid_string, "%llu", &pid) == 1) {
96                 result.vnn = local_vnn;
97                 result.pid = pid;
98         } else {
99                 result.vnn = NONCLUSTER_VNN;
100                 result.pid = UINT64_MAX;
101         }
102         return result;
103 }