-
Notifications
You must be signed in to change notification settings - Fork 2
/
veosinfo.c
6627 lines (6146 loc) · 184 KB
/
veosinfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (C) 2020 NEC Corporation
* This file is part of the VEOS information library.
*
* The VEOS information library is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either version
* 2.1 of the License, or (at your option) any later version.
*
* The VEOS information library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the VEOS information library; if not, see
* <http://www.gnu.org/licenses/>.
*/
/**
* @file veosinfo.c
* @brief Handles and manages the RPM source request and get required
* information from VEOS and VE sysfs
*
* @internal
* @author RPM command
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <stdint.h>
#include <sys/types.h>
#include <libudev.h>
#include <elf.h>
#include <getopt.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include "veosinfo.h"
#include "ve_sock.h"
#include "veos_RPM.pb-c.h"
#include "veosinfo_log.h"
#include "veosinfo_internal.h"
#include "veosinfo_comm.h"
#include <productinfo.h>
/**
* @brief This function is used to compare the versions.
*
* @param veos_ver[in] version number received from VEOS
* @param cmd_ver[in] version number of command library
*
* @return,
* -1 if veos version is smallar than command version
* 1 if veos version is greater than command version
* 0 if both veos and command version is same.
*/
int cmd_version_compare(char *veos_ver, char *cmd_ver)
{
int veos_len = 0, cmd_len = 0;
int veos_num = 0, cmd_num = 0;
int vlv = 0, clv = 0;
veos_len = strlen(veos_ver);
VE_RPMLIB_DEBUG("VEOS version: %s length: %d", veos_ver, veos_len);
cmd_len = strlen(cmd_ver);
VE_RPMLIB_DEBUG("Command version: %s length: %d", cmd_ver, cmd_len);
for (vlv = 0, clv = 0; (vlv < veos_len || clv < cmd_len);) {
/* Store each digit of 'veos_ver' in 'veos_num' one by one */
while (vlv < veos_len && veos_ver[vlv] != '.') {
veos_num = veos_num * 10 + (veos_ver[vlv] - '0');
vlv++;
}
/* Store each digit of 'cmd_ver' in 'cmd_num' one by one */
while (clv < cmd_len && cmd_ver[clv] != '.') {
cmd_num = cmd_num * 10 + (cmd_ver[clv] - '0');
clv++;
}
/* Now compare each digit of veos version with command
* library version
*/
if (veos_num < cmd_num) {
VE_RPMLIB_DEBUG("veos version is older than veosinfo");
return -1;
}
if (cmd_num < veos_num) {
VE_RPMLIB_DEBUG("veos version is newer than veosinfo");
return 1;
}
veos_num = cmd_num = 0;
vlv++;
clv++;
}
VE_RPMLIB_DEBUG("veos version is equal to veosinfo");
return 0;
}
/**
* @brief This function is used to verify version compatibility
* between veos and command library (veosinfo).
*
* @param nodeid[in] VE node number
*
* @return 0 on success and -1 on failure
*/
int verify_version(int nodeid)
{
int retval = -1;
int version = 0;
int sock_fd = -1;
int pack_msg_len = -1;
void *pack_buf_send = NULL;
uint8_t *unpack_buf_recv = NULL;
char *ve_sock_name = NULL;
char *veos_version = NULL;
VelibConnect *res = NULL;
VelibConnect request = VELIB_CONNECT__INIT;
ProtobufCBinaryData subreq_ver = {0};
VE_RPMLIB_TRACE("Entering");
errno = 0;
/* Create the socket path corresponding to received VE node */
ve_sock_name = ve_create_sockpath(nodeid);
if (!ve_sock_name) {
VE_RPMLIB_ERR("Failed to create socket path for VE: %s",
strerror(errno));
goto hndl_return;
}
/* Create the socket connection corresponding to socket path */
sock_fd = velib_sock(ve_sock_name);
if (-1 == sock_fd) {
VE_RPMLIB_ERR("Failed to create socket: %s, error: %s",
ve_sock_name, strerror(errno));
goto hndl_return_sock;
} else if (-2 == sock_fd)
goto abort;
request.cmd_str = RPM_QUERY_COMPT;
request.has_rpm_pid = true;
request.rpm_pid = getpid();
request.has_subcmd_str = true;
request.subcmd_str = -1;
request.has_rpm_version = true;
subreq_ver.data = (uint8_t *)VERSION_STRING;
subreq_ver.len = strlen(VERSION_STRING);
request.rpm_version = subreq_ver;
/* Get the request message size to send to VEOS */
pack_msg_len = velib_connect__get_packed_size(&request);
if (0 >= pack_msg_len) {
VE_RPMLIB_ERR("Failed to get size to pack message");
fprintf(stderr, "Failed to get size to pack message\n");
goto abort;
}
pack_buf_send = malloc(pack_msg_len);
if (!pack_buf_send) {
VE_RPMLIB_ERR("Memory allocation failed: %s", strerror(errno));
goto hndl_return1;
}
VE_RPMLIB_DEBUG("pack_msg_len = %d", pack_msg_len);
memset(pack_buf_send, '\0', pack_msg_len);
/* Pack the message to send to VEOS */
retval = velib_connect__pack(&request, pack_buf_send);
if (retval != pack_msg_len) {
VE_RPMLIB_ERR("Failed to pack message");
fprintf(stderr, "Failed to pack message\n");
goto abort;
}
/* Send the IPC message to VEOS and wait for the acknowledgment
* from VEOS
*/
retval = velib_send_cmd(sock_fd, pack_buf_send, pack_msg_len);
if (retval != pack_msg_len) {
VE_RPMLIB_ERR("Failed to send message: %d bytes written",
retval);
goto hndl_return2;
}
VE_RPMLIB_DEBUG("Send data successfully to VEOS and" \
" waiting to receive....");
unpack_buf_recv = malloc(MAX_PROTO_MSG_SIZE);
if (!unpack_buf_recv) {
VE_RPMLIB_ERR("Memory allocation failed: %s",
strerror(errno));
goto hndl_return2;
}
memset(unpack_buf_recv, '\0', MAX_PROTO_MSG_SIZE);
/* Receive the IPC message from VEOS */
retval = velib_recv_cmd(sock_fd, unpack_buf_recv, MAX_PROTO_MSG_SIZE);
if (-1 == retval) {
VE_RPMLIB_ERR("Failed to receive message: %s",
strerror(errno));
goto hndl_return3;
}
VE_RPMLIB_DEBUG("Data received successfully from VEOS, now verify it.");
/* Unpack the data received from VEOS */
res = velib_connect__unpack(NULL, retval,
(const uint8_t *)(unpack_buf_recv));
if (!res) {
VE_RPMLIB_ERR("Failed to unpack message: %d", retval);
fprintf(stderr, "Failed to unpack message\n");
goto abort;
}
/* Check if version is received from veos */
if (res->has_rpm_version == false) {
VE_RPMLIB_ERR("veos is older than veosinfo (v%s)",
VERSION_STRING);
fprintf(stderr,
"Compatibility Error: veos (older than v2.6.0) and "
"veosinfo (v%s) are not compatible\n",
VERSION_STRING);
goto abort;
}
veos_version = malloc(res->rpm_version.len);
if (!veos_version) {
VE_RPMLIB_ERR("Memory allocation failed: %s",
strerror(errno));
goto hndl_return4;
}
memset(veos_version, '\0', res->rpm_version.len);
memcpy(veos_version, res->rpm_version.data, res->rpm_version.len);
/* Compare the veos version and command library version compatibility */
version = cmd_version_compare(veos_version, VERSION_STRING);
if (version == -1) {
VE_RPMLIB_ERR("veos (v%s) is older than veosinfo (v%s)",
veos_version, VERSION_STRING);
fprintf(stderr,
"Compatibility Error: veos (v%s) and "
"veosinfo (v%s) are not compatible\n",
veos_version, VERSION_STRING);
free(veos_version);
goto abort;
}
retval = res->rpm_retval;
if (VE_EINVAL_DEVICE == retval) {
VE_RPMLIB_ERR("Compatibility Error: veos (v%s) and "
"veosinfo (v%s) are not compatible due to "
"unsupported device",veos_version, VERSION_STRING);
fprintf(stderr,
"Compatibility Error: unsupported device\n");
free(veos_version);
goto abort;
}
retval = 0;
goto hndl_return4;
abort:
close(sock_fd);
abort();
hndl_return4:
if(veos_version)
free(veos_version);
velib_connect__free_unpacked(res, NULL);
hndl_return3:
free(unpack_buf_recv);
hndl_return2:
free(pack_buf_send);
hndl_return1:
close(sock_fd);
hndl_return_sock:
free(ve_sock_name);
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief This function will check the value of VE node passed as environment
* variable is a valid VE node or not
*
* @param envrn[in] VE node passed by user as environment variable in
* "VE_NODE_NUMBER" with command
*
* @return 0 on success and -1 of failure
*/
int ve_match_envrn(char *envrn)
{
FILE *fp = NULL;
int nodeid = -1;
int retval = -1;
int nread = 0;
int status = -1;
int sock_fd = -1;
char sock_name[VE_PATH_MAX] = {0};
char *node_status_path = NULL;
char *endptr = NULL;
const char ve_sysfs_path[PATH_MAX] = {0};
VE_RPMLIB_TRACE("Entering");
if (!envrn) {
VE_RPMLIB_ERR("Wrong argument received: envrn = %p", envrn);
errno = EINVAL;
goto hndl_return;
}
if(*envrn == '\0') {
nodeid = 0;
}
else {
errno = 0;
nodeid = strtol(envrn, &endptr, 10);
if ((errno != 0) || (endptr == envrn) || (*endptr != '\0') ||
(nodeid < 0)) {
VE_RPMLIB_ERR("Invalid node number = %p", envrn);
errno = EINVAL;
goto hndl_return;
}
}
/* Get sysfs path corresponding to given VE node */
retval = ve_sysfs_path_info(nodeid, ve_sysfs_path);
if (-1 == retval) {
VE_RPMLIB_ERR("Failed to get sysfs path: %s",
strerror(errno));
goto hndl_return;
}
retval = -1;
node_status_path =
(char *)malloc(strlen(ve_sysfs_path) + VE_FILE_NAME);
if (!node_status_path) {
VE_RPMLIB_ERR("Memory allocation to node status path failed: %s",
strerror(errno));
goto hndl_return;
}
/* Get sysfs path to read 'os_state' of given VE node
*/
sprintf(node_status_path, "%s/os_state", ve_sysfs_path);
/* Open the "os_state" file to get the state of VE node
*/
fp = fopen(node_status_path, "r");
if (!fp) {
VE_RPMLIB_ERR("Open file '%s' failed: %s",
node_status_path, strerror(errno));
goto hndl_return2;
}
VE_RPMLIB_DEBUG("Open node status file %s successfully.",
node_status_path);
/* Read the VE node status information
*/
nread = fscanf(fp, "%d", &status);
if (nread == EOF || nread != 1) {
VE_RPMLIB_ERR("Failed to read: %s", strerror(errno));
fclose(fp);
goto hndl_return2;
}
fclose(fp);
if (status) {
VE_RPMLIB_ERR("Given node %s is not online", envrn);
goto hndl_return2;
}
sprintf(sock_name, "%s/veos%d.sock", VE_SOC_PATH, nodeid);
VE_RPMLIB_DEBUG("Socket path for given VE node = %s", sock_name);
/* Create the socket connection corresponding to socket path
*/
sock_fd = velib_sock(sock_name);
if (-1 == sock_fd) {
VE_RPMLIB_ERR("Failed to create socket: %s, error: %s",
sock_name, strerror(errno));
} else if (-2 == sock_fd) {
VE_RPMLIB_ERR("Given node %s is offline", envrn);
close(sock_fd);
}
else {
close(sock_fd);
retval = 0;
VE_RPMLIB_DEBUG("Given node is online: %s", envrn);
}
errno = 0;
hndl_return2:
free(node_status_path);
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief This function will create socket path corresponding to given VE node
*
* @param nodeid[in] VE node number
*
* @return Socket path corresponding to given node on success and
* NULL on failure
*/
char *ve_create_sockpath(int nodeid)
{
char *sock_path = NULL;
VE_RPMLIB_TRACE("Entering");
sock_path = (char *) malloc(sizeof(char) * VE_PATH_MAX);
if (!sock_path) {
VE_RPMLIB_ERR("Socket path memory allocation failed: %s",
strerror(errno));
goto hndl_return;
}
sprintf(sock_path, "%s/veos%d.sock", VE_SOC_PATH, nodeid);
VE_RPMLIB_DEBUG("Socket path for given VE node = %s", sock_path);
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return sock_path;
}
/**
* @brief This function will populate total number of online VE nodes
* and node name
*
* @param online_node_count[out] Total number of nodes,
* on which VEOS is running
* @param nodeid[out] Node number of all online VE nodes
*
* @return 0 on success and -1 of failure
*/
int ve_get_nos(unsigned int *online_node_count, int *nodeid)
{
unsigned int node_count = 0;
unsigned int count = 0;
int retval = -1;
int sock_fd = -1;
char sock_name[VE_PATH_MAX] = {0};
struct ve_nodeinfo ve_nodeinfo_req = { {0} };
VE_RPMLIB_TRACE("Entering");
if (!nodeid || !online_node_count) {
VE_RPMLIB_ERR("Wrong argument received:node = %p," \
" online_node_count = %p", nodeid,
online_node_count);
errno = EINVAL;
goto hndl_return;
}
/* Get the installed VE nodes */
if (-1 == ve_node_info(&ve_nodeinfo_req)) {
VE_RPMLIB_ERR("Failed to get VE node information: %s",
strerror(errno));
goto hndl_return;
}
for (count = 0; count < ve_nodeinfo_req.total_node_count; count++) {
VE_RPMLIB_DEBUG("Check for node_count = %d and node = %d",
count, ve_nodeinfo_req.nodeid[count]);
/* Check for node status online and offline
*/
if (!ve_nodeinfo_req.status[count]) {
sprintf(sock_name, "%s/veos%d.sock",
VE_SOC_PATH, ve_nodeinfo_req.nodeid[count]);
VE_RPMLIB_DEBUG("Socket path for given VE node = %s", sock_name);
/* Create the socket connection corresponding to socket path
*/
sock_fd = velib_sock(sock_name);
if (-1 == sock_fd) {
VE_RPMLIB_DEBUG("Failed to create socket: %s, error: %s",
sock_name, strerror(errno));
errno = 0;
} else if (-2 == sock_fd) {
VE_RPMLIB_DEBUG("Node %d is offline",
ve_nodeinfo_req.nodeid[count]);
close(sock_fd);
errno = 0;
} else {
close(sock_fd);
nodeid[node_count] = ve_nodeinfo_req.nodeid[count];
VE_RPMLIB_DEBUG("%d node is online.",
nodeid[node_count]);
node_count++;
}
}
}
/* If, there is no online node on VE */
if (!node_count) {
errno = ENOENT;
goto hndl_return;
}
*online_node_count = node_count;
retval = 0;
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief This function populates the VE architecture details
*
* @param nodeid[in] VE node number, corresponding to which architecture
* information is required
* @param ve_archinfo_req[out] Structure to store architecture information
*
* @return 0 on success and -1 on error
*/
int ve_arch_info(int nodeid, struct ve_archinfo *ve_archinfo_req)
{
int retval = -1;
VE_RPMLIB_TRACE("Entering");
if (!ve_archinfo_req) {
VE_RPMLIB_ERR("Wrong argument received: ve_archinfo_req = %p",
ve_archinfo_req);
errno = EINVAL;
goto hndl_return;
}
/* Populate VE architecture details
*/
strncpy(ve_archinfo_req->machine, VE_MACHINE, VE_FILE_NAME);
strncpy(ve_archinfo_req->processor, VE_PROCESSOR, 257);
strncpy(ve_archinfo_req->hw_platform, VE_HW_PLATFORM, 257);
retval = 0;
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief This function populates online and offline VE nodes's details
*
* @param ve_nodeinfo_req [out] Structure to populate VE node information
*
* This field consists of total number of VE nodes, total
* cores per node and status corresponding to each VE node
*
* @return 0 on success and -1 on error
*/
int ve_node_info(struct ve_nodeinfo *ve_nodeinfo_req)
{
FILE *fp = NULL;
int numcore = 0;
int node_count = 0;
int retval = -1;
int nread = 0;
char *node_status_path = NULL;
const char ve_sysfs_path[PATH_MAX] = {0};
VE_RPMLIB_TRACE("Entering");
if (!ve_nodeinfo_req) {
VE_RPMLIB_ERR("Wrong argument received: ve_nodeinfo_req = %p",
ve_nodeinfo_req);
errno = EINVAL;
goto hndl_return;
}
/* Get the VE node number and total number of VE nodes in system */
if (0 > get_ve_node(ve_nodeinfo_req->nodeid,
&ve_nodeinfo_req->total_node_count)) {
VE_RPMLIB_ERR("Failed to get VE node number: %s",
strerror(errno));
goto hndl_return;
}
node_status_path =
(char *)malloc(VE_PATH_MAX + VE_FILE_NAME);
if (!node_status_path) {
VE_RPMLIB_ERR("Memory allocation to node status path failed: %s",
strerror(errno));
goto hndl_return;
}
for (node_count = 0; node_count < ve_nodeinfo_req->total_node_count;
node_count++) {
VE_RPMLIB_DEBUG("Check for node_count = %d and node = %d",
node_count, ve_nodeinfo_req->nodeid[node_count]);
memset(ve_sysfs_path, '\0', PATH_MAX);
/* Get sysfs path corresponding to given VE node */
retval = ve_sysfs_path_info(ve_nodeinfo_req->nodeid[node_count],
ve_sysfs_path);
if (-1 == retval) {
VE_RPMLIB_ERR("Failed to get VE sysfs path: %s",
strerror(errno));
goto hndl_return2;
}
retval = -1;
/* Open VE specific sysfs directory, to get the node specific
* information
*/
memset(node_status_path, '\0', (VE_PATH_MAX + VE_FILE_NAME));
sprintf(node_status_path, "%s/os_state", ve_sysfs_path);
/* Open the "os_state" file corresponding to node,
* to get the state information
*/
fp = fopen(node_status_path, "r");
if (!fp) {
VE_RPMLIB_ERR("Open file '%s' failed: %s",
node_status_path, strerror(errno));
goto hndl_return2;
}
VE_RPMLIB_DEBUG("Open node status file %s successfully.",
node_status_path);
/* Read the VE node status information
*/
nread = 0;
nread = fscanf(fp, "%d", &ve_nodeinfo_req->status[node_count]);
if (nread == EOF || nread != 1) {
VE_RPMLIB_ERR("Failed to read file (%s): %s",
node_status_path, strerror(errno));
fclose(fp);
goto hndl_return2;
}
fclose(fp);
VE_RPMLIB_DEBUG("Reading status = %d",
ve_nodeinfo_req->status[node_count]);
/* Get the cores corresponding to given node
*/
numcore = 0;
if (-1 == ve_core_info(ve_nodeinfo_req->nodeid[node_count],
&numcore)) {
VE_RPMLIB_ERR("Failed to get CPU cores: %s",
strerror(errno));
goto hndl_return;
}
/* Populate the structure with cores value
*/
ve_nodeinfo_req->cores[node_count] = numcore;
VE_RPMLIB_DEBUG("node = %d, node_status = %d, cores = %d",
ve_nodeinfo_req->nodeid[node_count],
ve_nodeinfo_req->status[node_count],
ve_nodeinfo_req->cores[node_count]);
}
retval = 0;
VE_RPMLIB_DEBUG("Total_node_count = %d",
ve_nodeinfo_req->total_node_count);
hndl_return2:
free(node_status_path);
hndl_return:
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief Convert 'string' to 'unsigned long long' and also handle
* out of range value of 'unsigned long long'
*
* @param limit_optarg [in] String value
* @param lim_val [out] Converted value in 'unsigned long long'
*
* @return 0 on success and -1 on failure
*/
int get_value(char *lim_optarg, unsigned long long *lim_val)
{
int retval = 0;
char *optstr = NULL;
VE_RPMLIB_TRACE("Entering");
if (!lim_optarg || !lim_val) {
VE_RPMLIB_ERR("Wrong argument received:lim_optarg = %p," \
" lim_val = %p", lim_optarg, lim_val);
retval = -1;
goto out;
}
optstr = lim_optarg;
if (strncmp(optstr, "unlimited", sizeof("unlimited")) == 0) {
*lim_val = RLIM_INFINITY;
goto out;
}
while (*optstr >= '0' && *optstr <= '9')
*lim_val = (*lim_val) * 10 + (*optstr++ - '0');
VE_RPMLIB_DEBUG("Limit value spcified: %llu", *lim_val);
if (*optstr != '\0') {
VE_RPMLIB_ERR("Invalid limit value in optarg: %s", optstr);
retval = -1;
}
VE_RPMLIB_TRACE("Exiting");
out:
return retval;
}
/**
* @brief Parse VE_LIMIT_OPT and fetch the resource limit
*
* @param limit_opt [in] Resource limit passed by user as environment
* variable in "VE_LIMIT_OPT"
* @param ve_rlim [out] To set the resource limit
*
* @return 0 on success and negative value on failure.
*/
int get_ve_limit_opt(char *limit_opt, struct rlimit *ve_rlim)
{
int arg_c = 0;
int opt = 0, limit = 0;
int len_optind_arg = 0, len_optarg = 0;
int retval = VE_EINVAL_LIMITOPT;
bool repeat_lim[VE_RLIM_CNT] = {0};
unsigned long long lim_val = 0;
char *arg_v[VE_BUF_LEN] = {0};
char *token = NULL;
static const struct option longopts[] = {
{ "hardc", required_argument, NULL, 1 },
{ "softc", required_argument, NULL, 2 },
{ "hardd", required_argument, NULL, 3 },
{ "softd", required_argument, NULL, 4 },
{ "hardi", required_argument, NULL, 5 },
{ "softi", required_argument, NULL, 6 },
{ "hardm", required_argument, NULL, 7 },
{ "softm", required_argument, NULL, 8 },
{ "hards", required_argument, NULL, 9 },
{ "softs", required_argument, NULL, 10 },
{ "hardt", required_argument, NULL, 11 },
{ "softt", required_argument, NULL, 12 },
{ "hardv", required_argument, NULL, 13 },
{ "softv", required_argument, NULL, 14 },
{ NULL, 0, NULL, 0 }
};
VE_RPMLIB_TRACE("Entering");
if (!limit_opt || !ve_rlim) {
VE_RPMLIB_ERR("Wrong argument received:limit_opt = %p," \
" ve_rlim = %p", limit_opt, ve_rlim);
goto out;
}
token = strtok(limit_opt, " ");
arg_v[arg_c] = strndup("error", strlen("error"));
if (!arg_v[arg_c]) {
VE_RPMLIB_ERR("Failed to allocate memory");
goto out;
}
arg_c++;
/* Tokenize the value of VE_LIMIT_OPT environment variable */
while (token != NULL) {
arg_v[arg_c] = strndup(token, strlen(token));
if (!arg_v[arg_c]) {
VE_RPMLIB_ERR("Failed to allocate memory");
goto out;
}
token = strtok(NULL, " ");
arg_c++;
}
arg_v[arg_c] = '\0';
optind = 1;
/* Check the option specified with VE_LIMIT_OPT */
while ((opt = getopt_long(arg_c, arg_v, "+:c:d:i:m:s:t:v:",
longopts, NULL)) != -1) {
/* If valid option is specified and no option
* argument is missing */
if (opt != '?' && opt != ':') {
lim_val = 0;
retval = get_value(optarg, &lim_val);
if (0 > retval) {
VE_RPMLIB_ERR("Error in value conversion");
retval = (VE_EINVAL_LIMITOPT);
goto out_err;
}
retval = VE_EINVAL_LIMITOPT;
}
/* Validate the resource limit values*/
if (opt == 'c' || opt == 'd' || opt == 'i' ||
opt == 'm' || opt == 's' ||
opt == 't' || opt == 'v') {
len_optind_arg = strlen(arg_v[optind - 1]);
len_optarg = strlen(optarg);
if (strncmp(arg_v[optind-1], optarg,
(len_optind_arg > len_optarg ?
len_optind_arg : len_optarg)))
goto out_err;
}
/* Validate RLIMIT_CPU resource limit's minimum value*/
if ((lim_val == 0) && ((opt == 't') || (opt == SOFTT)))
lim_val = 1;
/*Resource limit value should not be greater than
* than MAX_RESOURCE_LIMIT for c, d, m, s and
* v resources */
if (opt != HARDI && opt != SOFTI &&
opt != HARDT && opt != SOFTT &&
opt != 'i' && opt != 't' &&
optarg != NULL &&
strncmp(optarg, "unlimited", sizeof("unlimited"))) {
if (lim_val > MAX_RESOURCE_LIMIT) {
VE_RPMLIB_DEBUG("Resource limit out of range");
retval = VE_ERANGE_LIMITOPT;
goto out_err;
}
lim_val = lim_val * KB;
}
/* Only consider the first value if the same resource limit
* mentioned repeatedly */
if (opt < VE_RLIM_CNT) {
if (!repeat_lim[opt])
repeat_lim[opt] = 1;
else
continue;
}
switch (opt) {
case 1:
ve_rlim[RLIMIT_CORE].rlim_max = lim_val;
break;
case 2:
ve_rlim[RLIMIT_CORE].rlim_cur = lim_val;
break;
case 3:
ve_rlim[RLIMIT_DATA].rlim_max = lim_val;
break;
case 4:
ve_rlim[RLIMIT_DATA].rlim_cur = lim_val;
break;
case 5:
ve_rlim[RLIMIT_SIGPENDING].rlim_max = lim_val;
break;
case 6:
ve_rlim[RLIMIT_SIGPENDING].rlim_cur = lim_val;
break;
case 7:
ve_rlim[RLIMIT_RSS].rlim_max = lim_val;
break;
case 8:
ve_rlim[RLIMIT_RSS].rlim_cur = lim_val;
break;
case 9:
ve_rlim[RLIMIT_STACK].rlim_max = lim_val;
break;
case 10:
ve_rlim[RLIMIT_STACK].rlim_cur = lim_val;
break;
case 11:
ve_rlim[RLIMIT_CPU].rlim_max = lim_val;
break;
case 12:
ve_rlim[RLIMIT_CPU].rlim_cur = lim_val;
break;
case 13:
ve_rlim[RLIMIT_AS].rlim_max = lim_val;
break;
case 14:
ve_rlim[RLIMIT_AS].rlim_cur = lim_val;
break;
case 'c':
if (!repeat_lim[SOFTC]) {
ve_rlim[RLIMIT_CORE].rlim_cur = lim_val;
repeat_lim[SOFTC] = 1;
}
if (!repeat_lim[HARDC]) {
ve_rlim[RLIMIT_CORE].rlim_max = lim_val;
repeat_lim[HARDC] = 1;
}
break;
case 'd':
if (!repeat_lim[SOFTD]) {
ve_rlim[RLIMIT_DATA].rlim_cur = lim_val;
repeat_lim[SOFTD] = 1;
}
if (!repeat_lim[HARDD]) {
ve_rlim[RLIMIT_DATA].rlim_max = lim_val;
repeat_lim[HARDD] = 1;
}
break;
case 'i':
if (!repeat_lim[SOFTI]) {
ve_rlim[RLIMIT_SIGPENDING].rlim_cur = lim_val;
repeat_lim[SOFTI] = 1;
}
if (!repeat_lim[HARDI]) {
ve_rlim[RLIMIT_SIGPENDING].rlim_max = lim_val;
repeat_lim[HARDI] = 1;
}
break;
case 'm':
if (!repeat_lim[SOFTM]) {
ve_rlim[RLIMIT_RSS].rlim_cur = lim_val;
repeat_lim[SOFTM] = 1;
}
if (!repeat_lim[HARDM]) {
ve_rlim[RLIMIT_RSS].rlim_max = lim_val;
repeat_lim[HARDM] = 1;
}
break;
case 's':
if (!repeat_lim[SOFTS]) {
ve_rlim[RLIMIT_STACK].rlim_cur = lim_val;
repeat_lim[SOFTS] = 1;
}
if (!repeat_lim[HARDS]) {
ve_rlim[RLIMIT_STACK].rlim_max = lim_val;
repeat_lim[HARDS] = 1;
}
break;
case 't':
if (!repeat_lim[SOFTT]) {
ve_rlim[RLIMIT_CPU].rlim_cur = lim_val;
repeat_lim[SOFTT] = 1;
}
if (!repeat_lim[HARDT]) {
ve_rlim[RLIMIT_CPU].rlim_max = lim_val;
repeat_lim[HARDT] = 1;
}
break;
case 'v':
if (!repeat_lim[SOFTV]) {
ve_rlim[RLIMIT_AS].rlim_cur = lim_val;
repeat_lim[SOFTV] = 1;
}
if (!repeat_lim[HARDV]) {
ve_rlim[RLIMIT_AS].rlim_max = lim_val;
repeat_lim[HARDV] = 1;
}
break;
case '?':
VE_RPMLIB_ERR("Unrecognized option");
goto out_err;
case ':':
VE_RPMLIB_ERR("Missing option argument");
goto out_err;
}
}
/* For error checking, if any value is specified without any option */
if (arg_v[optind]) {
VE_RPMLIB_ERR("Invalid Value: %s", arg_v[optind]);
retval = VE_EINVAL_LIMITOPT;
goto out_err;
}
/* To validate that hard limit should be greater than its soft limit */
for (limit = 0; limit < RLIM_NLIMITS; limit++) {
if (ve_rlim[limit].rlim_cur > ve_rlim[limit].rlim_max) {
VE_RPMLIB_DEBUG("lim: %d, soft lim: %llu, hard lim:" \
" %llu", limit, ve_rlim[limit].rlim_cur,
ve_rlim[limit].rlim_max);
VE_RPMLIB_ERR("Soft limit is greater than hard limit");
goto out_err;
}
VE_RPMLIB_DEBUG("limit: %d, soft lim: %llu, hard lim: %llu",
limit, ve_rlim[limit].rlim_cur,
ve_rlim[limit].rlim_max);
}
retval = 0;
goto out;
out_err:
VE_RPMLIB_ERR("Invalid input in VE_LIMIT_OPT");
out:
arg_c--;
while (arg_c >= 0) {
free(arg_v[arg_c]);
arg_c--;
}
VE_RPMLIB_TRACE("Exiting");
return retval;
}
/**
* @brief Fetch the resource limit.
*
* @param ve_rlim[out] resource limit
*
* @return 0 on success and negative value on failure.
*/
int get_ve_rlimit(struct rlimit *ve_rlim)
{
int resource = 0;
int limit_opt_length = 0;
int retval = 0;
char *limit_opt = NULL;
struct rlimit *limit = ve_rlim;
VE_RPMLIB_TRACE("Entering");
while (resource < RLIM_NLIMITS) {
switch (resource) {
case RLIMIT_CPU:
case RLIMIT_AS:
case RLIMIT_CORE:
case RLIMIT_DATA:
case RLIMIT_SIGPENDING:
case RLIMIT_RSS:
case RLIMIT_FSIZE:
case RLIMIT_LOCKS:
case RLIMIT_MEMLOCK:
case RLIMIT_MSGQUEUE:
case RLIMIT_NOFILE:
case RLIMIT_NPROC: