~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/ipe/polgen/polgen.c

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /*
  3  * Copyright (C) 2020-2024 Microsoft Corporation. All rights reserved.
  4  */
  5 
  6 #include <stdlib.h>
  7 #include <stddef.h>
  8 #include <stdio.h>
  9 #include <unistd.h>
 10 #include <errno.h>
 11 
 12 static void usage(const char *const name)
 13 {
 14         printf("Usage: %s OutputFile (PolicyFile)\n", name);
 15         exit(EINVAL);
 16 }
 17 
 18 static int policy_to_buffer(const char *pathname, char **buffer, size_t *size)
 19 {
 20         size_t fsize;
 21         size_t read;
 22         char *lbuf;
 23         int rc = 0;
 24         FILE *fd;
 25 
 26         fd = fopen(pathname, "r");
 27         if (!fd) {
 28                 rc = errno;
 29                 goto out;
 30         }
 31 
 32         fseek(fd, 0, SEEK_END);
 33         fsize = ftell(fd);
 34         rewind(fd);
 35 
 36         lbuf = malloc(fsize);
 37         if (!lbuf) {
 38                 rc = ENOMEM;
 39                 goto out_close;
 40         }
 41 
 42         read = fread((void *)lbuf, sizeof(*lbuf), fsize, fd);
 43         if (read != fsize) {
 44                 rc = -1;
 45                 goto out_free;
 46         }
 47 
 48         *buffer = lbuf;
 49         *size = fsize;
 50         fclose(fd);
 51 
 52         return rc;
 53 
 54 out_free:
 55         free(lbuf);
 56 out_close:
 57         fclose(fd);
 58 out:
 59         return rc;
 60 }
 61 
 62 static int write_boot_policy(const char *pathname, const char *buf, size_t size)
 63 {
 64         int rc = 0;
 65         FILE *fd;
 66         size_t i;
 67 
 68         fd = fopen(pathname, "w");
 69         if (!fd) {
 70                 rc = errno;
 71                 goto err;
 72         }
 73 
 74         fprintf(fd, "/* This file is automatically generated.");
 75         fprintf(fd, " Do not edit. */\n");
 76         fprintf(fd, "#include <linux/stddef.h>\n");
 77         fprintf(fd, "\nextern const char *const ipe_boot_policy;\n\n");
 78         fprintf(fd, "const char *const ipe_boot_policy =\n");
 79 
 80         if (!buf || size == 0) {
 81                 fprintf(fd, "\tNULL;\n");
 82                 fclose(fd);
 83                 return 0;
 84         }
 85 
 86         fprintf(fd, "\t\"");
 87 
 88         for (i = 0; i < size; ++i) {
 89                 switch (buf[i]) {
 90                 case '"':
 91                         fprintf(fd, "\\\"");
 92                         break;
 93                 case '\'':
 94                         fprintf(fd, "'");
 95                         break;
 96                 case '\n':
 97                         fprintf(fd, "\\n\"\n\t\"");
 98                         break;
 99                 case '\\':
100                         fprintf(fd, "\\\\");
101                         break;
102                 case '\t':
103                         fprintf(fd, "\\t");
104                         break;
105                 case '\?':
106                         fprintf(fd, "\\?");
107                         break;
108                 default:
109                         fprintf(fd, "%c", buf[i]);
110                 }
111         }
112         fprintf(fd, "\";\n");
113         fclose(fd);
114 
115         return 0;
116 
117 err:
118         if (fd)
119                 fclose(fd);
120         return rc;
121 }
122 
123 int main(int argc, const char *const argv[])
124 {
125         char *policy = NULL;
126         size_t len = 0;
127         int rc = 0;
128 
129         if (argc < 2)
130                 usage(argv[0]);
131 
132         if (argc > 2) {
133                 rc = policy_to_buffer(argv[2], &policy, &len);
134                 if (rc != 0)
135                         goto cleanup;
136         }
137 
138         rc = write_boot_policy(argv[1], policy, len);
139 cleanup:
140         if (policy)
141                 free(policy);
142         if (rc != 0)
143                 perror("An error occurred during policy conversion: ");
144         return rc;
145 }
146 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php