From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752685AbdBGBoJ (ORCPT ); Mon, 6 Feb 2017 20:44:09 -0500 Received: from mail.kernel.org ([198.145.29.136]:41018 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752471AbdBGBoD (ORCPT ); Mon, 6 Feb 2017 20:44:03 -0500 Date: Mon, 6 Feb 2017 22:43:56 -0300 From: Arnaldo Carvalho de Melo To: Borislav Petkov Cc: Ingo Molnar , Peter Zijlstra , Robert Richter , Vince Weaver , lkml Subject: Re: [PATCH 1/2] tools/lib/api/fs: Add procfs int read/write helpers Message-ID: <20170207014356.GE24988@kernel.org> References: <20170206121506.mtknwusus4djp2sx@pd.tnic> <20170206122231.GA9404@gmail.com> <20170206124116.7jmrmufthqtnd3dc@pd.tnic> <20170206124447.GA12772@gmail.com> <20170206124937.zlowglr5gpq35piy@pd.tnic> <20170207010813.buovxbku5rqhnhzg@pd.tnic> <20170207010917.36ysrh6p7yktxwhc@pd.tnic> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20170207010917.36ysrh6p7yktxwhc@pd.tnic> X-Url: http://acmel.wordpress.com User-Agent: Mutt/1.7.1 (2016-10-04) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Em Tue, Feb 07, 2017 at 02:09:17AM +0100, Borislav Petkov escreveu: > From: Borislav Petkov > > Add helper functions to be able to read/write ints into proc files. > > Signed-off-by: Borislav Petkov > --- > tools/lib/api/fs/fs.c | 42 ++++++++++++++++++++++++++++++++++++++++++ > tools/lib/api/fs/fs.h | 3 +++ > 2 files changed, 45 insertions(+) > > diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c > index 4b6bfc43cccf..8e4b9fe18b75 100644 > --- a/tools/lib/api/fs/fs.c > +++ b/tools/lib/api/fs/fs.c > @@ -314,6 +314,22 @@ int filename__read_int(const char *filename, int *value) > return err; > } > > +int filename__write_int(const char *filename, int value) > +{ > + char line[64]; > + int fd = open(filename, O_WRONLY), err = -1; > + > + if (fd < 0) > + return -1; > + > + snprintf(line, sizeof(int), "%d", value); > + > + err = write(fd, line, strnlen(line, 64)); > + > + close(fd); > + return err; > +} > + > /* > * Parses @value out of @filename with strtoull. > * By using 0 for base, the strtoull detects the > @@ -400,6 +416,32 @@ int procfs__read_str(const char *entry, char **buf, size_t *sizep) > return filename__read_str(path, buf, sizep); > } > > +int procfs__read_int(const char *entry, int *value) > +{ > + char path[PATH_MAX]; > + const char *procfs = procfs__mountpoint(); > + > + if (!procfs) > + return -1; > + > + snprintf(path, sizeof(path), "%s/%s", procfs, entry); > + > + return filename__read_int(path, value); > +} > + > +int procfs__write_int(const char *entry, int value) > +{ > + char path[PATH_MAX]; > + const char *procfs = procfs__mountpoint(); > + > + if (!procfs) > + return -1; > + > + snprintf(path, sizeof(path), "%s/%s", procfs, entry); > + > + return filename__write_int(path, value); > +} > + > int sysfs__read_ull(const char *entry, unsigned long long *value) > { > char path[PATH_MAX]; > diff --git a/tools/lib/api/fs/fs.h b/tools/lib/api/fs/fs.h > index 6b332dc74498..095f25d4c70f 100644 > --- a/tools/lib/api/fs/fs.h > +++ b/tools/lib/api/fs/fs.h > @@ -28,9 +28,12 @@ FS(bpf_fs) > > > int filename__read_int(const char *filename, int *value); > +int filename__write_int(const char *filename, int value); > int filename__read_ull(const char *filename, unsigned long long *value); > int filename__read_str(const char *filename, char **buf, size_t *sizep); > > +int procfs__read_int(const char *entry, int *value); > +int procfs__write_int(const char *entry, int value); > int procfs__read_str(const char *entry, char **buf, size_t *sizep); > > int sysctl__read_int(const char *sysctl, int *value); Isn't sysctl__read_int() what you want? See next patch...