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

Implement -U for Linux #2049

Merged
merged 6 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/man.md.j2
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ file.

: Update interval.

**-U \| \--unique**

: Conky won't start if another Conky process is already running. Implemented
only for Linux.

**-v \| -V \| \--version**

: Prints version, build information and general info. Exits after
Expand Down
10 changes: 9 additions & 1 deletion src/conky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2107,6 +2107,9 @@ void set_current_config() {
/* : means that character before that takes an argument */
const char *getopt_string =
"vVqdDSs:t:u:i:hc:p:"
#if defined(__linux__)
"U"
#endif
#ifdef BUILD_X11
"x:y:w:a:X:m:f:"
#ifdef OWN_WINDOW
Expand Down Expand Up @@ -2136,7 +2139,12 @@ const struct option longopts[] = {
{"double-buffer", 0, nullptr, 'b'}, {"window-id", 1, nullptr, 'w'},
#endif /* BUILD_X11 */
{"text", 1, nullptr, 't'}, {"interval", 1, nullptr, 'u'},
{"pause", 1, nullptr, 'p'}, {nullptr, 0, nullptr, 0}};
{"pause", 1, nullptr, 'p'},
#if defined(__linux__)
{"unique", 0, nullptr, 'U'},
#endif /* Linux */
{nullptr, 0, nullptr, 0}
};

void setup_inotify() {
#ifdef HAVE_SYS_INOTIFY_H
Expand Down
42 changes: 42 additions & 0 deletions src/linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3147,3 +3147,45 @@ void get_top_info(void) {
calc_io_each(); /* percentage of I/O for each task */
#endif /* BUILD_IOSTATS */
}

/******************************************
* Check if more than one conky process *
* is running *
******************************************/

bool is_conky_already_running(void) {
DIR *dir;
struct dirent *ent;
char buf[512];
int instances = 0;

if (!(dir = opendir("/proc"))) {
NORM_ERR("can't open /proc: %s\n", strerror(errno));
return false;
}

while ((ent = readdir(dir)) != NULL) {
char *endptr = ent->d_name;
long lpid = strtol(ent->d_name, &endptr, 10);
if (*endptr != '\0') {
continue;
}

snprintf(buf, sizeof(buf), "/proc/%ld/stat", lpid);
FILE *fp = fopen(buf, "r");
if (!fp) {
continue;
}

if (fgets(buf, sizeof(buf), fp) != NULL) {
char *conky = strstr(buf, "(conky)");
if (conky) {
instances++;
}
}
fclose(fp);
}

closedir(dir);
return instances > 1;
}
2 changes: 2 additions & 0 deletions src/linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ int update_stat(void);

void print_distribution(struct text_object *, char *, unsigned int);

bool is_conky_already_running(void);

extern char e_iface[64];
extern char interfaces_arr[MAX_NET_INTERFACES][64];

Expand Down
26 changes: 23 additions & 3 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
#include "ccurl_thread.h"
#endif /* BUILD_CURL */

#if defined(__linux__)
#include "linux.h"
#endif /* Linux */

#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#include "freebsd.h"
#endif /* FreeBSD */
Expand Down Expand Up @@ -268,8 +272,11 @@ static void print_help(const char *prog_name) {
" -i COUNT number of times to update " PACKAGE_NAME
" (and quit)\n"
" -p, --pause=SECS pause for SECS seconds at startup "
"before doing anything\n",
prog_name);
"before doing anything\n"
#if defined(__linux__)
" -U, --unique only one conky process can be created\n"
#endif /* Linux */
, prog_name);
}

inline void reset_optind() {
Expand All @@ -293,6 +300,8 @@ int main(int argc, char **argv) {
g_sighup_pending = 0;
g_sigusr2_pending = 0;

bool unique_process = false;

#ifdef BUILD_CURL
struct curl_global_initializer {
curl_global_initializer() {
Expand Down Expand Up @@ -349,12 +358,23 @@ int main(int argc, char **argv) {
window.window = strtol(optarg, nullptr, 0);
break;
#endif /* BUILD_X11 */

#if defined(__linux__)
case 'U':
unique_process = true;
break;
#endif /* Linux */
case '?':
return EXIT_FAILURE;
}
}

#if defined(__linux__)
if (unique_process && is_conky_already_running()) {
NORM_ERR("already running");
return 0;
}
#endif /* Linux */

try {
set_current_config();

Expand Down
Loading