summaryrefslogtreecommitdiff
path: root/arch/linux/common/os.h
blob: 0f7b59e38f8a16aec02450f5736f62746b4be1cd (plain)
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
/*
 *    HardInfo - Displays System Information
 *    Copyright (C) 2003-2006 Leandro A. F. Pereira <leandro@linuxmag.com.br>
 *
 *    This program 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, version 2.
 *
 *    This program 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 General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

static gchar *
get_libc_version(void)
{
    FILE *libc;
    gchar buf[256], *tmp, *p;
    
    libc = popen("/lib/libc.so.6", "r");
    if (!libc) goto err;
    
    fgets(buf, 256, libc);
    if (pclose(libc)) goto err;
    
    tmp = strstr(buf, "version ");
    if (!tmp) goto err;
    
    p = strchr(tmp, ',');
    if (p) *p = '\0';
    else goto err;
    
    return g_strdup_printf("GNU C Library version %s (%sstable)",
                           strchr(tmp, ' ') + 1,
                           strstr(buf, " stable ") ? "" : "un");
  err:
    return g_strdup("Unknown");
}

static gchar *
get_os_compiled_date(void)
{
    FILE *procversion;
    gchar buf[512];

    procversion = fopen("/proc/sys/kernel/version", "r");
    if (!procversion)
	return g_strdup("Unknown");

    fgets(buf, 512, procversion);
    fclose(procversion);

    return g_strdup(buf);
}


#include <gdk/gdkx.h>

void
detect_desktop_environment(OperatingSystem * os)
{
    const gchar *tmp = g_getenv("GNOME_DESKTOP_SESSION_ID");
    FILE *version;
    int maj, min;

    if (tmp) {
	/* FIXME: this might not be true, as the gnome-panel in path
	   may not be the one that's running.
	   see where the user's running panel is and run *that* to
	   obtain the version. */
	version = popen("gnome-panel --version", "r");
	if (version) {
	    fscanf(version, "Gnome gnome-panel %d.%d", &maj, &min);
	    if (pclose(version))
	        goto unknown;
	} else {
	    goto unknown;
	}

	os->desktop =
	    g_strdup_printf("GNOME %d.%d (session name: %s)", maj, min,
			    tmp);
    } else if (g_getenv("KDE_FULL_SESSION")) {
	version = popen("kcontrol --version", "r");
	if (version) {
	    char buf[32];

	    fgets(buf, 32, version);

	    fscanf(version, "KDE: %d.%d", &maj, &min);
	    if (pclose(version))
	        goto unknown;
	} else {
	    goto unknown;
	}

	os->desktop = g_strdup_printf("KDE %d.%d", maj, min);
    } else {
      unknown:
	if (!g_getenv("DISPLAY")) {
	    os->desktop = g_strdup("Terminal");
	} else {
            const gchar *windowman;
            GdkScreen *screen = gdk_screen_get_default();

            windowman = gdk_x11_screen_get_window_manager_name(screen);
            
            if (g_str_equal(windowman, "Xfwm4")) {
                /* FIXME: check if xprop -root | grep XFCE_DESKTOP_WINDOW
                   is defined */
                os->desktop = g_strdup("XFCE 4");
            } else {
  	        os->desktop = g_strdup_printf("Unknown (Window Manager: %s)",
  	                                      windowman);
            }
	}
    }
}

static OperatingSystem *
computer_get_os(void)
{
    struct utsname utsbuf;
    OperatingSystem *os;
    int i;

    os = g_new0(OperatingSystem, 1);

    os->compiled_date = get_os_compiled_date();

    /* Attempt to get the Distribution name; try using /etc/lsb-release first,
       then doing the legacy method (checking for /etc/$DISTRO-release files) */
    if (g_file_test("/etc/lsb-release", G_FILE_TEST_EXISTS)) {
	FILE *release;
	gchar buffer[128];

	release = popen("lsb_release -d", "r");
	fgets(buffer, 128, release);
	pclose(release);

	os->distro = buffer;
	os->distro = g_strdup(os->distro + strlen("Description:\t"));
    }

    for (i = 0;; i++) {
	if (distro_db[i].file == NULL) {
	    os->distrocode = g_strdup("unk");
	    os->distro = g_strdup("Unknown distribution");
	    break;
	}

	if (g_file_test(distro_db[i].file, G_FILE_TEST_EXISTS)) {


	    FILE *distro_ver;
	    char buf[128];

	    distro_ver = fopen(distro_db[i].file, "r");
	    fgets(buf, 128, distro_ver);
	    fclose(distro_ver);

	    buf[strlen(buf) - 1] = 0;

	    if (!os->distro) {
		/*
		 * HACK: Some Debian systems doesn't include
		 * the distribuition name in /etc/debian_release,
		 * so add them here. 
		 */
		if (!strncmp(distro_db[i].codename, "deb", 3) &&
		    ((buf[0] >= '0' && buf[0] <= '9') || buf[0] != 'D')) {
		    os->distro = g_strdup_printf
			("Debian GNU/Linux %s", buf);
		} else {
		    os->distro = g_strdup(buf);
		}
	    }
	    os->distrocode = g_strdup(distro_db[i].codename);

	    break;
	}
    }

    /* Kernel and hostname info */
    uname(&utsbuf);
    os->kernel = g_strdup_printf("%s %s (%s)", utsbuf.sysname,
				 utsbuf.release, utsbuf.machine);
    os->hostname = g_strdup(utsbuf.nodename);
    os->language = g_strdup(g_getenv("LC_MESSAGES"));
    os->homedir = g_strdup(g_get_home_dir());
    os->username = g_strdup_printf("%s (%s)",
				   g_get_user_name(), g_get_real_name());
    os->libc = get_libc_version();
    scan_languages(os);
    detect_desktop_environment(os);

    return os;
}