Section 40. Whenever the department is of opinion that a common carrier subject to its supervision is failing or omitting or about to fail or omit to do anything required of it by law or by order of the department, or is doing anything or about to do anything or permitting anything or about to permit anything to be done, contrary to or in violation of the law or of any order of the department, it shall direct its counsel to begin, subject to the supervision of the attorney general, an action or proceeding in the supreme judicial court in the name of the department for the purpose of having such violations or threatened violations stopped and prevented either by mandamus or injunction.