Skip to content

Commit bd5af31

Browse files
committed
src: handle nameless user @ ProcessMetrics::Update
If nsolid is running as a user without a name (e.g. system user), uv_os_get_passwd may fail. This change ensures ProcessMetrics::Update still fills metrics and does not return early in that case, leaving the user field empty instead of failing the update. PR-URL: #364 Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent a296caf commit bd5af31

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

src/nsolid.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ int ProcessMetrics::Update() {
211211
char title_buf[512];
212212
size_t rss;
213213
int er;
214+
// uv_os_get_passwd() may fail (e.g., nameless system user). Leave `user`
215+
// empty and continue.
216+
std::string user;
214217

215218
uv_loadavg(load_avgs);
216219
er = uv_get_process_title(title_buf, sizeof(title_buf));
@@ -231,17 +234,18 @@ int ProcessMetrics::Update() {
231234
if (er)
232235
return er;
233236
er = uv_os_get_passwd(&pwd);
234-
if (er)
235-
return er;
236-
auto free_passwd = OnScopeLeave([&]() { uv_os_free_passwd(&pwd); });
237+
if (er == 0) {
238+
auto free_passwd = OnScopeLeave([&]() { uv_os_free_passwd(&pwd); });
239+
user = pwd.username;
240+
}
237241

238242
cpu_percent[0] = (cpu[0] - cpu_prev_[0]) * 100.0 * 1000.0 / elapsed;
239243
cpu_percent[1] = (cpu[1] - cpu_prev_[1]) * 100.0 * 1000.0 / elapsed;
240244
cpu_percent[2] = (cpu[2] - cpu_prev_[2]) * 100.0 * 1000.0 / elapsed;
241245

242246
uv_mutex_lock(&stor_lock_);
243247
stor_.title = title_buf;
244-
stor_.user = pwd.username;
248+
stor_.user = user;
245249
stor_.timestamp = duration_cast<milliseconds>(
246250
system_clock::now().time_since_epoch()).count();
247251
stor_.uptime =

0 commit comments

Comments
 (0)