From patchwork Wed Jun 19 12:57:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Couder X-Patchwork-Id: 13703806 Received: from mail-ed1-f42.google.com (mail-ed1-f42.google.com [209.85.208.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 56E08132129 for ; Wed, 19 Jun 2024 12:57:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718801860; cv=none; b=nrgEnB2PLqx6+28Brj6FwqUPOA5gwLQU3u6tZi8FLA7qDf0Jnc8NqhgS2kqzspNMk2Efbb6bxKXUU1ds41csC1AYbRIkpZr/GtRe/vXpSbxbOF0YEsNvFCUdu4dJkHh1NcT9MKhXYUJrBOIg6985jNNd+e4j9+ivUrZ9cWU3t3k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718801860; c=relaxed/simple; bh=8WmvwybqYAbskP9YlCbCM7P80VtOaiWupd8OWeRXmOI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CN9kBa5EX5caujEwCGmJklUmg8Eh8iT7G9168XzCX0EHJ7r3pcdSqyFO7pZGQOChZSEEdbgI2UeSQOHsLK3WW9fh5cRdU7afQkjPGu3nNmMDI+G6eubbS/60QL8K44X07FeJOFi/7M/+tyCkGYULQUTysFgHABi43qBGauWgsTM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=e+qFkx8p; arc=none smtp.client-ip=209.85.208.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="e+qFkx8p" Received: by mail-ed1-f42.google.com with SMTP id 4fb4d7f45d1cf-57c7ec8f1fcso7880125a12.0 for ; Wed, 19 Jun 2024 05:57:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718801856; x=1719406656; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=KC3SsKkoUtFWyZxkr2s5yCHUIcdml+ol4m/HJG/PNwM=; b=e+qFkx8pZB2gUlLdg17o3/I9RZZ9jj6AXMje8qnwd8H3DSI4Amfy2lGeVeCkYQb5Qf X+PomIBBC2SOoAmXa9yfZb+pPiOsRBYiE0uueb0La8ziSy0V0R4EZwb0TF9SlnkIPGJC l9+UBJ86qwubEXU6lTrhwzxrxDUVhXurtzNSDktFPztEQGlpbkyhtj9ruFLlnrqnUtck MItHw9khBYWI6LwCPZmabQLQHZ7dtkIDfOa4jROBIns/QbVBJHoSE5NUmHugVZCoIWpG ZBaFItjkvvqeOFGuBjI7vmIYtPpYevSpQr0omQc8WGnhMc2XzBHXGQeVq27op8JIuU8f 9QYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718801856; x=1719406656; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=KC3SsKkoUtFWyZxkr2s5yCHUIcdml+ol4m/HJG/PNwM=; b=PcRvxeXoUvW1/4Cg5RbVzxzW6lTDz9Y1PobGubXtWVvptbtPa9O8Bvw1J9Xo6uAcW8 LiNHYGJs73btL3LLFZYgWoFQgZlKIjC4V4i15GFX8FokomcbO5RLBgJNJ+8VOuy0Wto3 ECpdenhIL16tGcWnUp3I6xFfGhF5ID2RzT4jUCqY7eLZGRU7kOof53i+yWypnt6cUCKG GTdNmjTARuFgq1/5ZkHKymq/4lFsNknYVVHnDBn3yclMn2x8Zv3GwHKVbyP7AO+wEz2F hGzaddNcke8mLxQb8Pb5bihJN3586Bgd2b8/CKFhan/uWDNDQOpoCrkrgAHA4SChUcvL N6xA== X-Gm-Message-State: AOJu0YywpnjbhmNNiwE7KQbZ5gp8GwjG4TQOIZq246hqRX4vrR7PVu4Y yLxg9Uk47fF72N3DvhaNIuehLf4yrNKgmO7Ra+Xfx/bvYYov0KsUjos+Uw== X-Google-Smtp-Source: AGHT+IFkmOJZVyznSWwX3q11pT8sXjx6iNC3nJSqjSyoJU+B8QvhnpnvOxr6IHpYjXwQr5TN57RyNQ== X-Received: by 2002:aa7:df8a:0:b0:57c:c019:a9f6 with SMTP id 4fb4d7f45d1cf-57d07ed27d7mr1379625a12.32.1718801855690; Wed, 19 Jun 2024 05:57:35 -0700 (PDT) Received: from christian-Precision-5550.. (176-138-135-207.abo.bbox.fr. [176.138.135.207]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57cb72e95d7sm8334237a12.47.2024.06.19.05.57.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jun 2024 05:57:35 -0700 (PDT) From: Christian Couder To: git@vger.kernel.org Cc: Junio C Hamano , Patrick Steinhardt , John Cai , Johannes Schindelin , Christian Couder , Christian Couder Subject: [PATCH 1/3] version: refactor strbuf_sanitize() Date: Wed, 19 Jun 2024 14:57:06 +0200 Message-ID: <20240619125708.3719150-2-christian.couder@gmail.com> X-Mailer: git-send-email 2.45.2.564.g3a7d533bde In-Reply-To: <20240619125708.3719150-1-christian.couder@gmail.com> References: <20240619125708.3719150-1-christian.couder@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The git_user_agent_sanitized() function performs some sanitizing to avoid special characters being sent over the line and possibly messing up with the protocol or with the parsing on the other side. Let's extract this sanitizing into a new strbuf_sanitize() function, as we will want to reuse it in a following patch. For now the new strbuf_sanitize() function is still static as it's only needed locally. While at it, let's also make a few small improvements: - use 'size_t' for 'i' instead of 'int', - move the declaration of 'i' inside the 'for ( ... )', - use strbuf_detach() to explicitely detach the string contained by the 'buf' strbuf. Signed-off-by: Christian Couder --- version.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/version.c b/version.c index 41b718c29e..331ee6c372 100644 --- a/version.c +++ b/version.c @@ -5,6 +5,15 @@ const char git_version_string[] = GIT_VERSION; const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT; +static void strbuf_sanitize(struct strbuf *buf) +{ + strbuf_trim(buf); + for (size_t i = 0; i < buf->len; i++) { + if (buf->buf[i] <= 32 || buf->buf[i] >= 127) + buf->buf[i] = '.'; + } +} + const char *git_user_agent(void) { static const char *agent = NULL; @@ -24,15 +33,10 @@ const char *git_user_agent_sanitized(void) if (!agent) { struct strbuf buf = STRBUF_INIT; - int i; strbuf_addstr(&buf, git_user_agent()); - strbuf_trim(&buf); - for (i = 0; i < buf.len; i++) { - if (buf.buf[i] <= 32 || buf.buf[i] >= 127) - buf.buf[i] = '.'; - } - agent = buf.buf; + strbuf_sanitize(&buf); + agent = strbuf_detach(&buf, NULL); } return agent; From patchwork Wed Jun 19 12:57:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Couder X-Patchwork-Id: 13703807 Received: from mail-ed1-f48.google.com (mail-ed1-f48.google.com [209.85.208.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 40EE1FC1F for ; Wed, 19 Jun 2024 12:57:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718801863; cv=none; b=YwA8JeyEbf91M7Zp3Fx1XVk5/Hd0jATpOse2D0+RxrI7xVBWwZpPsIbedIrp91geJyV2+p+6I1HLvAC1YKAUaTja4Gsvi9w+8ObFnTb+5nh13q53vNVk1YQFTUfRbWizkg92cBLGP6iaYE2NsKSygl2LJXL55Fepfu0JLsiXWeM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718801863; c=relaxed/simple; bh=mhxrpmfvvpEVNK2ly9rHMfBtK6wjQ6392IqNZrbo+Fw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pysFlCWUmljt24040bJfzwrQigkuxyH6k9bUtAXvzTC3cXoOmigaXk9/e2jpnPcRRatKya7xJ7KQIXA62gersfRBA3aG3xxHWGLdwOn8DqjcvPaZXhKZZehWKwlAuy5tlNLD+ez+wKTo2SFF6sOQ/YarQW1HGprmCToJCZQhCjM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JuHM7Vup; arc=none smtp.client-ip=209.85.208.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JuHM7Vup" Received: by mail-ed1-f48.google.com with SMTP id 4fb4d7f45d1cf-57d0699fd02so749781a12.0 for ; Wed, 19 Jun 2024 05:57:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718801858; x=1719406658; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UbHsfHHqoIRRDeIgQmITeHRt7jqfUAprlb53kaHMGPA=; b=JuHM7VuphTV1/QsqXORy3EAbD5pjfNbAaXoI0UN/Ckv/JCzWhS7gnDzAISg4/5CsOr iBegAknt4HqxFyBlQyH+vH3PJZ9AE6K1u77uz/4E1Zn8VMrETJvXOcwhx48jlRqAm7A2 AtZ1fC2XFlf/eUNOlSKh/5/ICct6fuRr3MoMJVbSYV3HUTXSh81IslsBDmhBtlCIWpIA f1SqndvkqGDrS3+UikTrEbyftoWVPFjo2hj5uZraoEfeF1wGHz5KrH/2T2BBwE48VdZq U/VVBnZK5dxrOJ2LzfyXHL7ABy+Dmcmy21OzN7BnP0twW3aeETJkP74E745VRrktVIJW H6rQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718801858; x=1719406658; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UbHsfHHqoIRRDeIgQmITeHRt7jqfUAprlb53kaHMGPA=; b=WfjMv8FOzCbXEZwg8miRsoXSGHhjcwp6CTFH/+Z48bwLPfodY+r0F6wDJLH1cYPkkO Ui9ExIJqo09JH/ByqtKpgrgY5vuvRZSmSLSJqmHWlJwPV0nbqQ37BZLQOZpKltoWGdrd hG/q7kt5dptwLBMTiFRJRJNKhx/m+32+x8rkekWj3RmD/+xZ86IxhV5bxaSSiOQDfg1O OuZ6MhX/KzeZFzmy01CjMRJDTSrHEp5CRblfYxyhsqnU4FsNuSKKznSzpA9Xm1GQjFWQ c6AuQwNAlktH6oQqPo2g7bJZkNe7YRbcrFJwbAd8lt91TjHTLrdY+YIHtPSHrzmRn+Hm FhFA== X-Gm-Message-State: AOJu0Yx4VRNk8eo0Gl7Ky8c60w2RNe7CserOQDm41dTBKxXSsn0AAn6y a2pO1Nt5KQbylnlDlFhTr/J1Z6rKymS3YzK9UislTiVLyofPobWVIfamOQ== X-Google-Smtp-Source: AGHT+IH9K/EExKMt8vrTrJEpvwZUFnWsqBv9HRivcEp3GIXr6xKwAp80vVzBAue5BVzdoVEi/kZvSw== X-Received: by 2002:a50:d717:0:b0:57c:b810:1062 with SMTP id 4fb4d7f45d1cf-57d07e421admr1552960a12.13.1718801858058; Wed, 19 Jun 2024 05:57:38 -0700 (PDT) Received: from christian-Precision-5550.. (176-138-135-207.abo.bbox.fr. [176.138.135.207]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57cb72e95d7sm8334237a12.47.2024.06.19.05.57.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jun 2024 05:57:36 -0700 (PDT) From: Christian Couder To: git@vger.kernel.org Cc: Junio C Hamano , Patrick Steinhardt , John Cai , Johannes Schindelin , Christian Couder , Christian Couder Subject: [PATCH 2/3] version: refactor get_uname_info() Date: Wed, 19 Jun 2024 14:57:07 +0200 Message-ID: <20240619125708.3719150-3-christian.couder@gmail.com> X-Mailer: git-send-email 2.45.2.564.g3a7d533bde In-Reply-To: <20240619125708.3719150-1-christian.couder@gmail.com> References: <20240619125708.3719150-1-christian.couder@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Some code from "builtin/bugreport.c" uses uname(2) to get system information. Let's refactor this code into a new get_uname_info() function, so that we can reuse it in a following commit. Signed-off-by: Christian Couder --- builtin/bugreport.c | 13 ++----------- version.c | 20 ++++++++++++++++++++ version.h | 7 +++++++ 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/builtin/bugreport.c b/builtin/bugreport.c index b3cc77af53..b24f876c41 100644 --- a/builtin/bugreport.c +++ b/builtin/bugreport.c @@ -11,10 +11,10 @@ #include "diagnose.h" #include "object-file.h" #include "setup.h" +#include "version.h" static void get_system_info(struct strbuf *sys_info) { - struct utsname uname_info; char *shell = NULL; /* get git version from native cmd */ @@ -23,16 +23,7 @@ static void get_system_info(struct strbuf *sys_info) /* system call for other version info */ strbuf_addstr(sys_info, "uname: "); - if (uname(&uname_info)) - strbuf_addf(sys_info, _("uname() failed with error '%s' (%d)\n"), - strerror(errno), - errno); - else - strbuf_addf(sys_info, "%s %s %s %s\n", - uname_info.sysname, - uname_info.release, - uname_info.version, - uname_info.machine); + get_uname_info(sys_info); strbuf_addstr(sys_info, _("compiler info: ")); get_compiler_info(sys_info); diff --git a/version.c b/version.c index 331ee6c372..10b9fa77d1 100644 --- a/version.c +++ b/version.c @@ -1,6 +1,7 @@ #include "git-compat-util.h" #include "version.h" #include "strbuf.h" +#include "gettext.h" const char git_version_string[] = GIT_VERSION; const char git_built_from_commit_string[] = GIT_BUILT_FROM_COMMIT; @@ -41,3 +42,22 @@ const char *git_user_agent_sanitized(void) return agent; } + +int get_uname_info(struct strbuf *buf) +{ + struct utsname uname_info; + + if (uname(&uname_info)) { + strbuf_addf(buf, _("uname() failed with error '%s' (%d)\n"), + strerror(errno), + errno); + return -1; + } + + strbuf_addf(buf, "%s %s %s %s\n", + uname_info.sysname, + uname_info.release, + uname_info.version, + uname_info.machine); + return 0; +} diff --git a/version.h b/version.h index 7c62e80577..afe3dbbab7 100644 --- a/version.h +++ b/version.h @@ -7,4 +7,11 @@ extern const char git_built_from_commit_string[]; const char *git_user_agent(void); const char *git_user_agent_sanitized(void); +/* + Try to get information about the system using uname(2). + Return -1 and put an error message into 'buf' in case of uname() + error. Return 0 and put uname info into 'buf' otherwise. +*/ +int get_uname_info(struct strbuf *buf); + #endif /* VERSION_H */ From patchwork Wed Jun 19 12:57:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Couder X-Patchwork-Id: 13703808 Received: from mail-ed1-f51.google.com (mail-ed1-f51.google.com [209.85.208.51]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B803C132129 for ; Wed, 19 Jun 2024 12:57:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.51 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718801863; cv=none; b=axtG8SvKR98cYK34aSkHgoiDol2JfzYPvypPmoz6mWPB/8mWgq7Y3D3+Ety3jXWhz/X8NNgcnh4UOFTLI5TDAnMgTRfeTEFXV5aZsBygu6Q+zp4aNrtKIcxqKbrKn1S189BKY2iljyNRZCQ5fm/iHLSoZ+/sJBHtqVBZ5R8RhPk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1718801863; c=relaxed/simple; bh=T94oAFeaIShInpAiaP8NoYqdNWoPQdNMAEfOHedoiEg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=B0yoaKFeGbmuxvg5jmGgb4OELqQyE0f0Z/7h7fCF54IGudJWOTw7ybzOieSyPkfOjP/ubH49hwwIM/od85gw3eKJHF/d+Qt6Kdd/ZIiNxxLD+sKN6WgFf+vkp51rhZRPgFGAMdwwpA7nmG3IvSekTzYfdbaHg0QAG2kfTJigSpY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=V6iA4E3k; arc=none smtp.client-ip=209.85.208.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="V6iA4E3k" Received: by mail-ed1-f51.google.com with SMTP id 4fb4d7f45d1cf-57d1012e52fso764630a12.3 for ; Wed, 19 Jun 2024 05:57:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1718801859; x=1719406659; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tJu7sWV1L1yI/qyDA1EYDBBiXOi+diiR8oNs5nUQX1E=; b=V6iA4E3kvBgjBhvw24pM5qvL/WICCeSXUmcKBilII9JnGtawjW20VJf609iXZjilaS ChBD5qscI6YkeIPGjCQ3ibhl+7JHHpWBAf0wxUoQz/eEBwzhmIYOZFYCqcdPmLHYrmI7 o69xH2Wkyve01ktM+8pXAkgi0s6vSLmBq64eWVNgOQq0mylcosmH0PM4QDXUxhgbZC77 bJQp/ccWkny7LWXjug9R4AJMvAdwbU2qBlyRoTmVF37OKT6O1zJ4LobGRSARCSpvIgpR cdf0/AnD+gYTcH6ACmxqWOkN0N8tP9SAXZbqNNx36/3L6hk+DHLBbUqm3dpHeBpdU1rI GMrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718801859; x=1719406659; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tJu7sWV1L1yI/qyDA1EYDBBiXOi+diiR8oNs5nUQX1E=; b=cc/yCiLHEhjZGV7Ii9X9GZRjgEcyyyIE3T2mTbpOaWZD8KlctSj2YeXO7DqGxjKwhX SAk0n39kAFgZpKS07wsnJ3g56GYXeTXOUbK8g8ZP7iKMeJLw0XLtWCL9rLOiOS1djJD+ MLiV124qcohHLPWGpv2hT8msGHDZ/tYpTzgwzaK6SYZe1oohhkSqfFXq1/AgHvOMKL5+ LZwma0ukDfshCIZkbGuK8klR5JdUuz00hZWDyV7X0dM7x7pD1EuSMcy/6NNFJyJl5XWw eg9Ped1xFPWII4C+E2Qi4L5bZZ62Dt1WeuundbXpaHitBDTXAWtzI/DhVE0IqzNkyfim k9MQ== X-Gm-Message-State: AOJu0YwxviNa3sg7xRv2+AonxnLilRa/sy3bGAvR4C74lfFJ3GcE8jK3 cRni5AalmvziRO2j4/CDTbbkldXTds6CffzaTfBWPymvrN1xfkSwJuxc/w== X-Google-Smtp-Source: AGHT+IEsLigic3W6b7wDg7CeP8KG5HeO/Vhn48RhQYtYUs5566/q/BYIUlMFO5EeSfrVH3fEDnRDeg== X-Received: by 2002:a50:c309:0:b0:57a:3424:b36e with SMTP id 4fb4d7f45d1cf-57d07e5aac1mr1550737a12.13.1718801858989; Wed, 19 Jun 2024 05:57:38 -0700 (PDT) Received: from christian-Precision-5550.. (176-138-135-207.abo.bbox.fr. [176.138.135.207]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-57cb72e95d7sm8334237a12.47.2024.06.19.05.57.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jun 2024 05:57:38 -0700 (PDT) From: Christian Couder To: git@vger.kernel.org Cc: Junio C Hamano , Patrick Steinhardt , John Cai , Johannes Schindelin , Christian Couder , Christian Couder Subject: [PATCH 3/3] connect: advertise OS version Date: Wed, 19 Jun 2024 14:57:08 +0200 Message-ID: <20240619125708.3719150-4-christian.couder@gmail.com> X-Mailer: git-send-email 2.45.2.564.g3a7d533bde In-Reply-To: <20240619125708.3719150-1-christian.couder@gmail.com> References: <20240619125708.3719150-1-christian.couder@gmail.com> Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 As some issues that can happen with a Git client can be operating system specific, it can be useful for a server to know which OS a client is using. In the same way it can be useful for a client to know which OS a server is using. Let's add OS information exchange to the protocol in the same way some git version exchange is performed. Signed-off-by: Christian Couder --- Documentation/gitprotocol-v2.txt | 18 ++++++++++++++++++ connect.c | 3 +++ serve.c | 12 ++++++++++++ t/t5555-http-smart-common.sh | 3 +++ t/t5701-git-serve.sh | 3 +++ version.c | 29 +++++++++++++++++++++++++++++ version.h | 3 +++ 7 files changed, 71 insertions(+) diff --git a/Documentation/gitprotocol-v2.txt b/Documentation/gitprotocol-v2.txt index 414bc625d5..f676b2dc7a 100644 --- a/Documentation/gitprotocol-v2.txt +++ b/Documentation/gitprotocol-v2.txt @@ -190,6 +190,24 @@ printable ASCII characters except space (i.e., the byte range 32 < x < and debugging purposes, and MUST NOT be used to programmatically assume the presence or absence of particular features. +os-version +~~~~~~~~~~ + +In the same way as the `agent` capability above, the server can +advertise the `os-version` capability with a value `X` (in the form +`os-version=X`) to notify the client that the server is running an +operating system that can be identified by `X`. The client may +optionally send its own `os-version` string by including the +`os-version` capability with a value `Y` (in the form `os-version=Y`) +in its request to the server (but it MUST NOT do so if the server did +not advertise the os-version capability). The `X` and `Y` strings may +contain any printable ASCII characters except space (i.e., the byte +range 32 < x < 127), and are typically made from the result of +`uname -srvm`. The os-version strings are purely informative for +statistics and debugging purposes, and MUST NOT be used to +programmatically assume the presence or absence of particular +features. + ls-refs ~~~~~~~ diff --git a/connect.c b/connect.c index 0d77737a53..3a48806ddc 100644 --- a/connect.c +++ b/connect.c @@ -489,6 +489,9 @@ static void send_capabilities(int fd_out, struct packet_reader *reader) if (server_supports_v2("agent")) packet_write_fmt(fd_out, "agent=%s", git_user_agent_sanitized()); + if (server_supports_v2("os-version")) + packet_write_fmt(fd_out, "os-version=%s", os_version_sanitized()); + if (server_feature_v2("object-format", &hash_name)) { int hash_algo = hash_algo_by_name(hash_name); if (hash_algo == GIT_HASH_UNKNOWN) diff --git a/serve.c b/serve.c index aa651b73e9..77eb5ebdaa 100644 --- a/serve.c +++ b/serve.c @@ -29,6 +29,14 @@ static int agent_advertise(struct repository *r UNUSED, return 1; } +static int os_version_advertise(struct repository *r UNUSED, + struct strbuf *value) +{ + if (value) + strbuf_addstr(value, os_version_sanitized()); + return 1; +} + static int object_format_advertise(struct repository *r, struct strbuf *value) { @@ -121,6 +129,10 @@ static struct protocol_capability capabilities[] = { .name = "agent", .advertise = agent_advertise, }, + { + .name = "os-version", + .advertise = os_version_advertise, + }, { .name = "ls-refs", .advertise = ls_refs_advertise, diff --git a/t/t5555-http-smart-common.sh b/t/t5555-http-smart-common.sh index 3dcb3340a3..c67739236f 100755 --- a/t/t5555-http-smart-common.sh +++ b/t/t5555-http-smart-common.sh @@ -124,9 +124,12 @@ test_expect_success 'git receive-pack --advertise-refs: v1' ' ' test_expect_success 'git upload-pack --advertise-refs: v2' ' + # Octal intervals \001-\040 and \177-\377 + # corresponds to decimal intervals 1-32 and 127-255 cat >expect <<-EOF && version 2 agent=FAKE + os-version=$(uname -srvm | tr -d "\n" | tr "[\001-\040][\177-\377]" ".") ls-refs=unborn fetch=shallow wait-for-done server-option diff --git a/t/t5701-git-serve.sh b/t/t5701-git-serve.sh index c48830de8f..9c9a707e6a 100755 --- a/t/t5701-git-serve.sh +++ b/t/t5701-git-serve.sh @@ -13,9 +13,12 @@ test_expect_success 'test capability advertisement' ' wrong_algo sha1:sha256 wrong_algo sha256:sha1 EOF + # Octal intervals \001-\040 and \177-\377 + # corresponds to decimal intervals 1-32 and 127-255 cat >expect.base <<-EOF && version 2 agent=git/$(git version | cut -d" " -f3) + os-version=$(uname -srvm | tr -d "\n" | tr "[\001-\040][\177-\377]" ".") ls-refs=unborn fetch=shallow wait-for-done server-option diff --git a/version.c b/version.c index 10b9fa77d1..5b20ea0d7c 100644 --- a/version.c +++ b/version.c @@ -61,3 +61,32 @@ int get_uname_info(struct strbuf *buf) uname_info.machine); return 0; } + +const char *os_version(void) +{ + static const char *os = NULL; + + if (!os) { + struct strbuf buf = STRBUF_INIT; + + get_uname_info(&buf); + os = strbuf_detach(&buf, NULL); + } + + return os; +} + +const char *os_version_sanitized(void) +{ + static const char *os_sanitized = NULL; + + if (!os_sanitized) { + struct strbuf buf = STRBUF_INIT; + + strbuf_addstr(&buf, os_version()); + strbuf_sanitize(&buf); + os_sanitized = strbuf_detach(&buf, NULL); + } + + return os_sanitized; +} diff --git a/version.h b/version.h index afe3dbbab7..349952c8f2 100644 --- a/version.h +++ b/version.h @@ -14,4 +14,7 @@ const char *git_user_agent_sanitized(void); */ int get_uname_info(struct strbuf *buf); +const char *os_version(void); +const char *os_version_sanitized(void); + #endif /* VERSION_H */