github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/libsnap-confine-private/classic.c (about)

     1  #include "config.h"
     2  #include "classic.h"
     3  #include "../libsnap-confine-private/cleanup-funcs.h"
     4  #include "../libsnap-confine-private/string-utils.h"
     5  
     6  #include <stdbool.h>
     7  #include <stdio.h>
     8  #include <string.h>
     9  #include <unistd.h>
    10  
    11  static const char *os_release = "/etc/os-release";
    12  static const char *meta_snap_yaml = "/meta/snap.yaml";
    13  
    14  sc_distro sc_classify_distro(void)
    15  {
    16  	FILE *f SC_CLEANUP(sc_cleanup_file) = fopen(os_release, "r");
    17  	if (f == NULL) {
    18  		return SC_DISTRO_CLASSIC;
    19  	}
    20  
    21  	bool is_core = false;
    22  	int core_version = 0;
    23  	char buf[255] = { 0 };
    24  
    25  	while (fgets(buf, sizeof buf, f) != NULL) {
    26  		size_t len = strlen(buf);
    27  		if (len > 0 && buf[len - 1] == '\n') {
    28  			buf[len - 1] = '\0';
    29  		}
    30  		if (sc_streq(buf, "ID=\"ubuntu-core\"")
    31  		    || sc_streq(buf, "ID=ubuntu-core")) {
    32  			is_core = true;
    33  		} else if (sc_streq(buf, "VERSION_ID=\"16\"")
    34  			   || sc_streq(buf, "VERSION_ID=16")) {
    35  			core_version = 16;
    36  		} else if (sc_streq(buf, "VARIANT_ID=\"snappy\"")
    37  			   || sc_streq(buf, "VARIANT_ID=snappy")) {
    38  			is_core = true;
    39  		}
    40  	}
    41  
    42  	if (!is_core) {
    43  		/* Since classic systems don't have a /meta/snap.yaml file the simple
    44  		   presence of that file qualifies as SC_DISTRO_CORE_OTHER. */
    45  		if (access(meta_snap_yaml, F_OK) == 0) {
    46  			is_core = true;
    47  		}
    48  	}
    49  
    50  	if (is_core) {
    51  		if (core_version == 16) {
    52  			return SC_DISTRO_CORE16;
    53  		}
    54  		return SC_DISTRO_CORE_OTHER;
    55  	} else {
    56  		return SC_DISTRO_CLASSIC;
    57  	}
    58  }