Skip to content

Commit 03ada7b

Browse files
PJHsiehvincentchiang-ec
authored andcommitted
Set the eeprom cache permissions to 755
Issue and Root Cause: The issue arises because, after a system reboot, the permissions of /var/cache/sonic/decode-syseeprom/ or its file /var/cache/sonic/decode-syseeprom/syseeprom_cache may be incorrectly set. This misconfiguration can cause the Platform API to require root privileges for access. This problem has been observed in various platforms, such as the AS7326_56X, where running show platform syseeprom results in errors due to permission issues. Solution: To address this, it's essential to ensure that the cache directory and its files have the correct permissions set upon system startup. Implementing a fix that adjusts these permissions appropriately can prevent the Platform API from requiring root privileges, thereby resolving the issue.
1 parent 43c3984 commit 03ada7b

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

sonic_platform_base/sonic_eeprom/eeprom_base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,21 @@ def write_cache(self, e):
295295
if self.cache_name:
296296
F = None
297297
try:
298+
# Ensure the directory exists
299+
directory = os.path.dirname(self.cache_name)
300+
301+
if not os.path.exists(directory):
302+
os.makedirs(directory)
303+
304+
# Set directory permissions to 755
305+
os.chmod(directory, 0o755)
306+
298307
F = open(self.cache_name, "wb")
299308
F.seek(self.s)
300309
F.write(e)
310+
311+
# Set file permissions to 755
312+
os.chmod(self.cache_name, 0o755)
301313
except IOError as e:
302314
raise IOError("Failed to write cache : %s" % (str(e)))
303315
finally:

tests/eeprom_base_test.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33
import subprocess
44
from unittest import mock
5-
from unittest.mock import patch, MagicMock
5+
from unittest.mock import patch, MagicMock, mock_open
66
from sonic_platform_base.sonic_eeprom import eeprom_base, eeprom_tlvinfo
77
EEPROM_SYMLINK = "vpd_info"
88
EEPROM_HEX_FILE = "syseeprom.hex"
@@ -200,6 +200,40 @@ def test_set_eeprom_invalid_field(self, mock_print):
200200
mock_print.assert_called_with("Error: invalid field '0x20'")
201201
assert e.value.code == 1
202202

203+
@mock.patch("builtins.open", new_callable=mock.mock_open)
204+
@mock.patch("os.makedirs")
205+
@mock.patch("os.path.exists", return_value=False)
206+
@mock.patch("os.chmod")
207+
def test_write_cache(self, mock_chmod, mock_exists, mock_makedirs, mock_open):
208+
# Create an instance of TlvInfoDecoder with a mock cache_name
209+
eeprom_decoder = eeprom_tlvinfo.TlvInfoDecoder("/dev/null", 0, "", True)
210+
eeprom_decoder.cache_name = "/tmp/mock_cache"
211+
212+
# Mock EEPROM data to write
213+
mock_eeprom_data = b"mock_eeprom_data"
214+
215+
# Call the write_cache method
216+
eeprom_decoder.write_cache(mock_eeprom_data)
217+
218+
# Verify that os.makedirs was called with the correct directory
219+
mock_makedirs.assert_called_once_with("/tmp")
220+
221+
# Verify that os.chmod was called to set directory permissions
222+
mock_chmod.assert_any_call("/tmp", 0o755)
223+
224+
# Verify that the file was opened in write-binary mode
225+
mock_open.assert_called_once_with("/tmp/mock_cache", "wb")
226+
227+
# Verify that the file's seek method was called with the correct offset
228+
mock_open().seek.assert_called_once_with(eeprom_decoder.s)
229+
230+
# Verify that the file's write method was called with the EEPROM data
231+
mock_open().write.assert_called_once_with(mock_eeprom_data)
232+
233+
# Verify that os.chmod was called to set file permissions
234+
mock_chmod.assert_any_call("/tmp/mock_cache", 0o755)
235+
236+
203237
def teardown(self):
204238
print("TEAR DOWN")
205239

0 commit comments

Comments
 (0)