Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug on Ubuntu 19.04 #14

Closed
vrossum opened this issue Oct 12, 2019 · 5 comments · Fixed by #17
Closed

Bug on Ubuntu 19.04 #14

vrossum opened this issue Oct 12, 2019 · 5 comments · Fixed by #17
Labels

Comments

@vrossum
Copy link

vrossum commented Oct 12, 2019

On Ubuntu 19.04, I get:
"(xsuspender:15598): GLib-WARNING **: 14:29:02.244: GError set over the top of a previous GError or uninitialized memory.
This indicates a bug in someone's code. You must ensure an error is NULL before it's set.
The overwriting error message was: Failed to open file ?/sys/class/power_supply/BAT0/online?: No such file or directory"

THere is no
/sys/class/power_supply/BAT0/online
there is however
/sys/class/power_supply/AC/online

@kernc
Copy link
Owner

kernc commented Oct 12, 2019

Thanks. IIUC, err should be reset to NULL in each while loop iteration before this line:

if (! g_file_get_contents (filename, &contents, NULL, &err)) {

@kernc kernc added the bug label Oct 12, 2019
@vrossum
Copy link
Author

vrossum commented Feb 18, 2020

So how to do that?

(PS your thumbnail looks like a swastika!)

@vrossum
Copy link
Author

vrossum commented Feb 18, 2020

Ok this works:
-const char *DIRNAME = "/sys/class/power_supply";
+const char *DIRNAME = "/sys/class/power_supply/AC";

-g_autofree char *filename = g_build_filename (DIRNAME, basename, "online", NULL);
+g_autofree char *filename = g_build_filename (DIRNAME, "online", NULL);

@vrossum
Copy link
Author

vrossum commented Feb 25, 2020

This works:
(only check sys/class/power_supply/AC/online)

static
gboolean
is_on_ac_power ()
{
#ifdef __linux__
    // Read AC power state from /sys/class/power_supply/AC/online (== 1 on AC).
    // Should work in most cases. See: https://bugs.debian.org/473629

    const char *DIRNAME = "/sys/class/power_supply/AC";
    g_autoptr (GError) err = NULL;

	g_autofree char *filename = g_build_filename (DIRNAME, "online", NULL);
	g_autofree char *contents = NULL;

	g_debug ("Reading '%s'", filename);
	if (! g_file_get_contents (filename, &contents, NULL, &err)) {
	    g_debug ("Cannot read '%s': %s", filename, err->message);
	}

	if (g_strcmp0 (g_strstrip (contents), "1") == 0)
	    return TRUE;
    
    #warning "No battery / AC status support for your platform."
    #warning "Defaulting to as if 'always on battery' behavior. Patches welcome!"
    return FALSE;

#elif defined(__unix__) && defined(BSD) && !defined(__APPLE__)
    // On *BSD, run `apm -a` which returns '1' when AC is online
    g_autoptr (GError) err = NULL;
    g_autofree char *standard_output = NULL;
    char *argv[] = {"apm", "-a", NULL};

    g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL,
                  NULL, NULL, &standard_output, NULL, NULL, &err);
    if (err)
        g_warning ("Unexpected `apm -a` execution error: %s", err->message);

    return standard_output && 0 == g_strcmp0 (g_strstrip (standard_output), "1");
#endif
}

@smeijer
Copy link

smeijer commented May 4, 2020

I'm having a similar error on 19.10. When running xsuspender in debug mode, I'm seeing this log over and over:

Reading '/sys/class/power_supply/AC/online'
Reading '/sys/class/power_supply/BAT0/online'
Cannot read '/sys/class/power_supply/BAT0/online': Failed to open file ?/sys/class/power_supply/BAT0/online?: No such file or directory

kernc added a commit that referenced this issue May 10, 2020
The status is already reported by `on_check_battery_powered()`.
Should be ok for newer kernels and should avoid scaring users.
Closes #14
@kernc kernc closed this as completed in #17 May 10, 2020
kernc added a commit that referenced this issue May 10, 2020
The status is already reported by `on_check_battery_powered()`.
Should be ok for newer kernels and should avoid scaring users.
Closes #14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants