|
8 | 8 | * Sai Praneeth Prakhya <sai.praneeth.prakhya@intel.com>,
|
9 | 9 | * Fenghua Yu <fenghua.yu@intel.com>
|
10 | 10 | */
|
| 11 | +#include <fcntl.h> |
11 | 12 | #include <limits.h>
|
12 | 13 |
|
13 | 14 | #include "resctrl.h"
|
@@ -490,9 +491,8 @@ int write_bm_pid_to_resctrl(pid_t bm_pid, char *ctrlgrp, char *mongrp,
|
490 | 491 | */
|
491 | 492 | int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
|
492 | 493 | {
|
493 |
| - char controlgroup[1024], schema[1024], reason[64]; |
494 |
| - int resource_id, ret = 0; |
495 |
| - FILE *fp; |
| 494 | + char controlgroup[1024], reason[128], schema[1024] = {}; |
| 495 | + int resource_id, fd, schema_len = -1, ret = 0; |
496 | 496 |
|
497 | 497 | if (strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) &&
|
498 | 498 | strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) &&
|
@@ -520,28 +520,39 @@ int write_schemata(char *ctrlgrp, char *schemata, int cpu_no, char *resctrl_val)
|
520 | 520 |
|
521 | 521 | if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR)) ||
|
522 | 522 | !strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR)))
|
523 |
| - sprintf(schema, "%s%d%c%s", "L3:", resource_id, '=', schemata); |
| 523 | + schema_len = snprintf(schema, sizeof(schema), "%s%d%c%s\n", |
| 524 | + "L3:", resource_id, '=', schemata); |
524 | 525 | if (!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR)) ||
|
525 | 526 | !strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)))
|
526 |
| - sprintf(schema, "%s%d%c%s", "MB:", resource_id, '=', schemata); |
527 |
| - |
528 |
| - fp = fopen(controlgroup, "w"); |
529 |
| - if (!fp) { |
530 |
| - sprintf(reason, "Failed to open control group"); |
| 527 | + schema_len = snprintf(schema, sizeof(schema), "%s%d%c%s\n", |
| 528 | + "MB:", resource_id, '=', schemata); |
| 529 | + if (schema_len < 0 || schema_len >= sizeof(schema)) { |
| 530 | + snprintf(reason, sizeof(reason), |
| 531 | + "snprintf() failed with return value : %d", schema_len); |
531 | 532 | ret = -1;
|
532 |
| - |
533 | 533 | goto out;
|
534 | 534 | }
|
535 | 535 |
|
536 |
| - if (fprintf(fp, "%s\n", schema) < 0) { |
537 |
| - sprintf(reason, "Failed to write schemata in control group"); |
538 |
| - fclose(fp); |
| 536 | + fd = open(controlgroup, O_WRONLY); |
| 537 | + if (fd < 0) { |
| 538 | + snprintf(reason, sizeof(reason), |
| 539 | + "open() failed : %s", strerror(errno)); |
539 | 540 | ret = -1;
|
540 | 541 |
|
541 |
| - goto out; |
| 542 | + goto err_schema_not_empty; |
542 | 543 | }
|
543 |
| - fclose(fp); |
| 544 | + if (write(fd, schema, schema_len) < 0) { |
| 545 | + snprintf(reason, sizeof(reason), |
| 546 | + "write() failed : %s", strerror(errno)); |
| 547 | + close(fd); |
| 548 | + ret = -1; |
| 549 | + |
| 550 | + goto err_schema_not_empty; |
| 551 | + } |
| 552 | + close(fd); |
544 | 553 |
|
| 554 | +err_schema_not_empty: |
| 555 | + schema[schema_len - 1] = 0; |
545 | 556 | out:
|
546 | 557 | ksft_print_msg("Write schema \"%s\" to resctrl FS%s%s\n",
|
547 | 558 | schema, ret ? " # " : "",
|
|
0 commit comments