PERFORCE change 21052 for review

Brian Feldman green at freebsd.org
Wed Nov 13 23:10:57 GMT 2002


http://perforce.freebsd.org/chv.cgi?CH=21052

Change 21052 by green at green_laptop_2 on 2002/11/13 15:10:13

	Optimization: treat labeling operations as a transaction so that
	setfsmac(8) will no label an entire filesystem more quickly.

Affected files ...

.. //depot/projects/trustedbsd/mac/sbin/setfsmac/setfsmac.c#4 edit

Differences ...

==== //depot/projects/trustedbsd/mac/sbin/setfsmac/setfsmac.c#4 (text+ko) ====

@@ -19,10 +19,11 @@
 		char *regexstr;	/* uncompiled regular expression */
 		mode_t mode;	/* mode to possibly match */
 		char *modestr;	/* print-worthy ",-?" mode string */
-		mac_t mac;	/* MAC label to apply */
+		char *mactext;	/* MAC label to apply */
 		int flags;	/* miscellaneous flags */
 #define		F_DONTLABEL	0x01
-	} *entries;
+	} *entries,		/* entries[0..nentries] */
+	  *match;		/* cached decision for MAC label to apply */
 	size_t nentries;	/* size of entries list */
 	STAILQ_ENTRY(label_spec) link;
 };
@@ -211,7 +212,7 @@
 add_spec_line(const char *file, int is_sebsd, struct label_spec_entry *entry,
     char *line)
 {
-	char *regexstr, *modestr, *macstr, *regerrorstr, *sebsdstr;
+	char *regexstr, *modestr, *macstr, *regerrorstr;
 	size_t size;
 	int error;
 
@@ -242,17 +243,14 @@
 		errx(1, "%s: %s: %s", file, entry->regexstr, regerrorstr);
 	}
 	if (!is_sebsd) {
-		if (mac_from_text(&entry->mac, macstr))
-			err(1, "%s: mac_from_text(%s)", file, macstr);
+		entry->mactext = strdup(macstr);
+		if (entry->mactext == NULL)
+			err(1, "strdup");
 	} else {
-		if (asprintf(&sebsdstr, "sebsd/%s", macstr) == -1)
+		if (asprintf(&entry->mactext, "sebsd/%s", macstr) == -1)
 			err(1, "asprintf");
-		if (mac_from_text(&entry->mac, sebsdstr))
-			err(1, "%s: mac_from_text(%s)", file, sebsdstr);
-		if (strcmp(macstr, "<<none>>") == 0)
+		if (strcmp(entry->mactext, "<<none>>") == 0)
 			entry->flags |= F_DONTLABEL;
-		free(sebsdstr);
-
 	}
 	if (modestr != NULL) {
 		if (strlen(modestr) != 2 || modestr[0] != '-')
@@ -300,8 +298,9 @@
 	regmatch_t pmatch;
 	struct label_spec *ls;
 	struct label_spec_entry *ent;
-	char *regerrorstr, *mactext;
+	char *regerrorstr, *macstr;
 	size_t size;
+	mac_t mac;
 	int error, matchedby;
 
 	/*
@@ -311,8 +310,8 @@
 	 */
 	matchedby = 0;
 	STAILQ_FOREACH(ls, &specs->head, link) {
-		for (ent = ls->entries; ent < &ls->entries[ls->nentries];
-		    ent++) {
+		for (ls->match = NULL, ent = ls->entries;
+		    ent < &ls->entries[ls->nentries]; ent++) {
 			if (ent->mode != 0 &&
 			    (ftsent->fts_statp->st_mode & S_IFMT) != ent->mode)
 				continue;
@@ -324,6 +323,7 @@
 			case REG_NOMATCH:
 				continue;
 			case 0:
+				ls->match = ent;
 				break;
 			default:
 				size = regerror(error, &ent->regex, NULL, 0);
@@ -341,28 +341,48 @@
 					    ftsent->fts_path);
 					matchedby = 1;
 				}
-				if (mac_to_text(ent->mac, &mactext) != 0)
-					err(1, "mac_to_text");
 				printf("%s(%s%s,%s)", matchedby == 2 ? "," : "",
-				    ent->regexstr, ent->modestr, mactext);
+				    ent->regexstr, ent->modestr, ent->mactext);
 				if (matchedby == 1)
 					matchedby = 2;
-				free(mactext);
-			}
-			if ((ent->flags & F_DONTLABEL) == 0 &&
-			    mac_set_link(ftsent->fts_accpath, ent->mac) != 0) {
-				if (errno == EOPNOTSUPP)
-					return (1);
-				if (vflag)
-					printf("\n");
-				err(1, "mac_set_link(%.*s)",
-				    ftsent->fts_pathlen, ftsent->fts_path);
 			}
 			break;
 		}
 	}
 	if (vflag && matchedby)
 		printf("\n");
+	size = 0;
+	STAILQ_FOREACH(ls, &specs->head, link) {
+		/* cached match decision */
+		if (ls->match && (ls->match->flags & F_DONTLABEL) == 0)
+			 /* add length of "x\0"/"y," */
+			size += strlen(ls->match->mactext) + 1;
+	}
+	macstr = malloc(size);
+	if (macstr == NULL)
+		err(1, "malloc");
+	*macstr = '\0';
+	STAILQ_FOREACH(ls, &specs->head, link) {
+		/* cached match decision */
+		if (ls->match && (ls->match->flags & F_DONTLABEL) == 0) {
+			if (*macstr != '\0')
+				strcat(macstr, ",");
+			strcat(macstr, ls->match->mactext);
+		}
+	}
+	if (mac_from_text(&mac, macstr))
+		err(1, "mac_from_text(%s)", macstr);
+	if (mac_set_link(ftsent->fts_accpath, mac) != 0) {
+		if (errno == EOPNOTSUPP) {
+			mac_free(mac);
+			free(macstr);
+			return (1);
+		}
+		err(1, "mac_set_link(%.*s, %s)", ftsent->fts_pathlen,
+		    ftsent->fts_path, macstr);
+	}
+	mac_free(mac);
+	free(macstr);
 	return (0);
 }
 
To Unsubscribe: send mail to majordomo at trustedbsd.org
with "unsubscribe trustedbsd-cvs" in the body of the message



More information about the trustedbsd-cvs mailing list